Author: thomas.heller
Date: Thu Jul 12 17:41:51 2007
New Revision: 56319
Modified:
python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py
python/branches/py3k-struni/Modules/_ctypes/cfield.c
Log:
Accept bytes in c_char_p and c_wchar_p types.
Modified: python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py (original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_bytes.py Thu Jul 12
17:41:51 2007
@@ -14,5 +14,13 @@
c_wchar.from_param(b"x")
(c_wchar * 3)(b"a", b"b", b"c")
+ def test_c_char_p(self):
+ c_char_p("foo bar")
+ c_char_p(b"foo bar")
+
+ def test_c_wchar_p(self):
+ c_wchar_p("foo bar")
+ c_wchar_p(b"foo bar")
+
if __name__ == '__main__':
unittest.main()
Modified: python/branches/py3k-struni/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k-struni/Modules/_ctypes/cfield.c (original)
+++ python/branches/py3k-struni/Modules/_ctypes/cfield.c Thu Jul 12
17:41:51 2007
@@ -1354,8 +1354,8 @@
Py_INCREF(value);
return value;
}
- if (PyString_Check(value)) {
- *(char **)ptr = PyString_AS_STRING(value);
+ if (PyBytes_Check(value)) {
+ *(char **)ptr = PyBytes_AsString(value);
Py_INCREF(value);
return value;
} else if (PyUnicode_Check(value)) {
@@ -1410,13 +1410,7 @@
Py_INCREF(value);
return value;
}
- if (PyString_Check(value)) {
- value = PyUnicode_FromEncodedObject(value,
- conversion_mode_encoding,
- conversion_mode_errors);
- if (!value)
- return NULL;
- } else if (PyInt_Check(value) || PyLong_Check(value)) {
+ if (PyInt_Check(value) || PyLong_Check(value)) {
#if SIZEOF_VOID_P == SIZEOF_LONG_LONG
*(wchar_t **)ptr = (wchar_t
*)PyInt_AsUnsignedLongLongMask(value);
#else
@@ -1424,6 +1418,13 @@
#endif
Py_INCREF(Py_None);
return Py_None;
+ }
+ if (PyBytes_Check(value)) {
+ value = PyUnicode_FromEncodedObject(value,
+ conversion_mode_encoding,
+ conversion_mode_errors);
+ if (!value)
+ return NULL;
} else if (!PyUnicode_Check(value)) {
PyErr_Format(PyExc_TypeError,
"unicode string or integer address expected
instead of %s instance",
_______________________________________________
Python-3000-checkins mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000-checkins