diff options
author | Guido van Rossum <guido@python.org> | 2007-10-24 18:41:19 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-10-24 18:41:19 +0000 |
commit | f1c6c9e69b185485f8c4b885026ec10e07f20ec1 (patch) | |
tree | cc3599315163050b902df925548c36977e580d99 | |
parent | 37befa29ebd1589792bd6765d8c24a94e075dcd6 (diff) | |
download | cpython-f1c6c9e69b185485f8c4b885026ec10e07f20ec1.tar.gz |
Patch #1318 by Amaury Forgeot d'Arc.
Updates to ctypes for python 3.0 to make the tests pass.
Notable changes are:
- return bytes instead of str8
- integers in range(256) are accepted as "one char string":
libc.strchr("abcdef", 98) is now valid.
- directly use the wide-char version of the win32 function LoadLibrary.
-rw-r--r-- | Lib/ctypes/__init__.py | 4 | ||||
-rw-r--r-- | Lib/ctypes/test/test_arrays.py | 12 | ||||
-rw-r--r-- | Lib/ctypes/test/test_buffers.py | 20 | ||||
-rw-r--r-- | Lib/ctypes/test/test_libc.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_memfunctions.py | 22 | ||||
-rw-r--r-- | Lib/ctypes/test/test_python_api.py | 8 | ||||
-rw-r--r-- | Lib/ctypes/test/test_returnfuncptrs.py | 6 | ||||
-rw-r--r-- | Lib/ctypes/test/test_slicing.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_strings.py | 22 | ||||
-rw-r--r-- | Lib/ctypes/test/test_unicode.py | 16 | ||||
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 52 | ||||
-rw-r--r-- | Modules/_ctypes/callbacks.c | 4 | ||||
-rw-r--r-- | Modules/_ctypes/callproc.c | 36 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 8 |
14 files changed, 106 insertions, 108 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index c4131277e9..cc05e97c8a 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -52,11 +52,11 @@ STDAPICALLTYPE """ def create_string_buffer(init, size=None): - """create_string_buffer(aString) -> character array + """create_string_buffer(aBytes) -> character array create_string_buffer(anInteger) -> character array create_string_buffer(aString, anInteger) -> character array """ - if isinstance(init, str): + if isinstance(init, (str, bytes)): if size is None: size = len(init)+1 buftype = c_char * size diff --git a/Lib/ctypes/test/test_arrays.py b/Lib/ctypes/test/test_arrays.py index f8bce5a976..dde77c129f 100644 --- a/Lib/ctypes/test/test_arrays.py +++ b/Lib/ctypes/test/test_arrays.py @@ -94,12 +94,12 @@ class ArrayTestCase(unittest.TestCase): # Failed with 0.9.8, reported by JUrner p = create_string_buffer("foo") sz = (c_char * 3).from_address(addressof(p)) - self.failUnlessEqual(sz[:], "foo") - self.failUnlessEqual(sz[::], "foo") - self.failUnlessEqual(sz[::-1], "oof") - self.failUnlessEqual(sz[::3], "f") - self.failUnlessEqual(sz[1:4:2], "o") - self.failUnlessEqual(sz.value, "foo") + self.failUnlessEqual(sz[:], b"foo") + self.failUnlessEqual(sz[::], b"foo") + self.failUnlessEqual(sz[::-1], b"oof") + self.failUnlessEqual(sz[::3], b"f") + self.failUnlessEqual(sz[1:4:2], b"o") + self.failUnlessEqual(sz.value, b"foo") try: create_unicode_buffer diff --git a/Lib/ctypes/test/test_buffers.py b/Lib/ctypes/test/test_buffers.py index 5e7b23d31f..1f4091e68b 100644 --- a/Lib/ctypes/test/test_buffers.py +++ b/Lib/ctypes/test/test_buffers.py @@ -14,11 +14,11 @@ class StringBufferTestCase(unittest.TestCase): self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char)) self.failUnless(type(b[0]) is bytes) self.failUnlessEqual(b[0], b"a") - self.failUnlessEqual(b[:], "abc\0") - self.failUnlessEqual(b[::], "abc\0") - self.failUnlessEqual(b[::-1], "\0cba") - self.failUnlessEqual(b[::2], "ac") - self.failUnlessEqual(b[::5], "a") + self.failUnlessEqual(b[:], b"abc\0") + self.failUnlessEqual(b[::], b"abc\0") + self.failUnlessEqual(b[::-1], b"\0cba") + self.failUnlessEqual(b[::2], b"ac") + self.failUnlessEqual(b[::5], b"a") def test_string_conversion(self): b = create_string_buffer("abc") @@ -26,11 +26,11 @@ class StringBufferTestCase(unittest.TestCase): self.failUnlessEqual(sizeof(b), 4 * sizeof(c_char)) self.failUnless(type(b[0]) is bytes) self.failUnlessEqual(b[0], b"a") - self.failUnlessEqual(b[:], "abc\0") - self.failUnlessEqual(b[::], "abc\0") - self.failUnlessEqual(b[::-1], "\0cba") - self.failUnlessEqual(b[::2], "ac") - self.failUnlessEqual(b[::5], "a") + self.failUnlessEqual(b[:], b"abc\0") + self.failUnlessEqual(b[::], b"abc\0") + self.failUnlessEqual(b[::-1], b"\0cba") + self.failUnlessEqual(b[::2], b"ac") + self.failUnlessEqual(b[::5], b"a") try: c_wchar diff --git a/Lib/ctypes/test/test_libc.py b/Lib/ctypes/test/test_libc.py index c39f3507c0..0e14c2f6df 100644 --- a/Lib/ctypes/test/test_libc.py +++ b/Lib/ctypes/test/test_libc.py @@ -24,7 +24,7 @@ class LibTest(unittest.TestCase): chars = create_string_buffer("spam, spam, and spam") lib.my_qsort(chars, len(chars)-1, sizeof(c_char), comparefunc(sort)) - self.failUnlessEqual(chars.raw, " ,,aaaadmmmnpppsss\x00") + self.failUnlessEqual(chars.raw, b" ,,aaaadmmmnpppsss\x00") if __name__ == "__main__": unittest.main() diff --git a/Lib/ctypes/test/test_memfunctions.py b/Lib/ctypes/test/test_memfunctions.py index a5b3b157fc..6a6411e9eb 100644 --- a/Lib/ctypes/test/test_memfunctions.py +++ b/Lib/ctypes/test/test_memfunctions.py @@ -9,21 +9,21 @@ class MemFunctionsTest(unittest.TestCase): a = create_string_buffer(1000000) p = b"Hello, World" result = memmove(a, p, len(p)) - self.failUnlessEqual(a.value, "Hello, World") + self.failUnlessEqual(a.value, b"Hello, World") - self.failUnlessEqual(string_at(result), "Hello, World") - self.failUnlessEqual(string_at(result, 5), "Hello") - self.failUnlessEqual(string_at(result, 16), "Hello, World\0\0\0\0") - self.failUnlessEqual(string_at(result, 0), "") + self.failUnlessEqual(string_at(result), b"Hello, World") + self.failUnlessEqual(string_at(result, 5), b"Hello") + self.failUnlessEqual(string_at(result, 16), b"Hello, World\0\0\0\0") + self.failUnlessEqual(string_at(result, 0), b"") def test_memset(self): a = create_string_buffer(1000000) result = memset(a, ord('x'), 16) - self.failUnlessEqual(a.value, "xxxxxxxxxxxxxxxx") + self.failUnlessEqual(a.value, b"xxxxxxxxxxxxxxxx") - self.failUnlessEqual(string_at(result), "xxxxxxxxxxxxxxxx") - self.failUnlessEqual(string_at(a), "xxxxxxxxxxxxxxxx") - self.failUnlessEqual(string_at(a, 20), "xxxxxxxxxxxxxxxx\0\0\0\0") + self.failUnlessEqual(string_at(result), b"xxxxxxxxxxxxxxxx") + self.failUnlessEqual(string_at(a), b"xxxxxxxxxxxxxxxx") + self.failUnlessEqual(string_at(a, 20), b"xxxxxxxxxxxxxxxx\0\0\0\0") def test_cast(self): a = (c_ubyte * 32)(*map(ord, "abcdef")) @@ -46,8 +46,8 @@ class MemFunctionsTest(unittest.TestCase): self.failUnlessEqual(2, sys.getrefcount(s)) self.failUnless(s, "foo bar") - self.failUnlessEqual(string_at(b"foo bar", 7), "foo bar") - self.failUnlessEqual(string_at(b"foo bar", 3), "foo") + self.failUnlessEqual(string_at(b"foo bar", 7), b"foo bar") + self.failUnlessEqual(string_at(b"foo bar", 3), b"foo") try: create_unicode_buffer diff --git a/Lib/ctypes/test/test_python_api.py b/Lib/ctypes/test/test_python_api.py index f701d54f01..8e8b6f773f 100644 --- a/Lib/ctypes/test/test_python_api.py +++ b/Lib/ctypes/test/test_python_api.py @@ -23,13 +23,13 @@ class PythonAPITestCase(unittest.TestCase): PyString_FromStringAndSize.restype = py_object PyString_FromStringAndSize.argtypes = c_char_p, c_py_ssize_t - self.failUnlessEqual(PyString_FromStringAndSize("abcdefghi", 3), "abc") + self.failUnlessEqual(PyString_FromStringAndSize(b"abcdefghi", 3), b"abc") def test_PyString_FromString(self): pythonapi.PyString_FromString.restype = py_object pythonapi.PyString_FromString.argtypes = (c_char_p,) - s = "abc" + s = b"abc" refcnt = grc(s) pyob = pythonapi.PyString_FromString(s) self.failUnlessEqual(grc(s), refcnt) @@ -73,10 +73,10 @@ class PythonAPITestCase(unittest.TestCase): buf = c_buffer(256) PyOS_snprintf(buf, sizeof(buf), "Hello from %s", b"ctypes") - self.failUnlessEqual(buf.value, "Hello from ctypes") + self.failUnlessEqual(buf.value, b"Hello from ctypes") PyOS_snprintf(buf, sizeof(buf), "Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) - self.failUnlessEqual(buf.value, "Hello from ctypes (1, 2, 3)") + self.failUnlessEqual(buf.value, b"Hello from ctypes (1, 2, 3)") # not enough arguments self.failUnlessRaises(TypeError, PyOS_snprintf, buf) diff --git a/Lib/ctypes/test/test_returnfuncptrs.py b/Lib/ctypes/test/test_returnfuncptrs.py index 88dccf2ce1..cd451a29cc 100644 --- a/Lib/ctypes/test/test_returnfuncptrs.py +++ b/Lib/ctypes/test/test_returnfuncptrs.py @@ -14,7 +14,9 @@ class ReturnFuncPtrTestCase(unittest.TestCase): strchr = get_strchr() self.failUnlessEqual(strchr("abcdef", "b"), "bcdef") self.failUnlessEqual(strchr("abcdef", "x"), None) - self.assertRaises(ArgumentError, strchr, "abcdef", 3) + self.failUnlessEqual(strchr("abcdef", 98), "bcdef") + self.failUnlessEqual(strchr("abcdef", 107), None) + self.assertRaises(ArgumentError, strchr, "abcdef", 3.0) self.assertRaises(TypeError, strchr, "abcdef") def test_without_prototype(self): @@ -28,7 +30,7 @@ class ReturnFuncPtrTestCase(unittest.TestCase): strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(addr) self.failUnless(strchr("abcdef", "b"), "bcdef") self.failUnlessEqual(strchr("abcdef", "x"), None) - self.assertRaises(ArgumentError, strchr, "abcdef", 3) + self.assertRaises(ArgumentError, strchr, "abcdef", 3.0) self.assertRaises(TypeError, strchr, "abcdef") if __name__ == "__main__": diff --git a/Lib/ctypes/test/test_slicing.py b/Lib/ctypes/test/test_slicing.py index 5df00195e2..28e66da733 100644 --- a/Lib/ctypes/test/test_slicing.py +++ b/Lib/ctypes/test/test_slicing.py @@ -121,7 +121,7 @@ class SlicesTestCase(unittest.TestCase): def test_char_array(self): - s = "abcdefghijklmnopqrstuvwxyz\0" + s = b"abcdefghijklmnopqrstuvwxyz\0" p = (c_char * 27)(*s) self.failUnlessEqual(p[:], s) diff --git a/Lib/ctypes/test/test_strings.py b/Lib/ctypes/test/test_strings.py index 0a06332de3..ef1cfcf979 100644 --- a/Lib/ctypes/test/test_strings.py +++ b/Lib/ctypes/test/test_strings.py @@ -6,20 +6,20 @@ class StringArrayTestCase(unittest.TestCase): BUF = c_char * 4 buf = BUF("a", "b", "c") - self.failUnlessEqual(buf.value, "abc") - self.failUnlessEqual(buf.raw, "abc\000") + self.failUnlessEqual(buf.value, b"abc") + self.failUnlessEqual(buf.raw, b"abc\000") buf.value = "ABCD" - self.failUnlessEqual(buf.value, "ABCD") - self.failUnlessEqual(buf.raw, "ABCD") + self.failUnlessEqual(buf.value, b"ABCD") + self.failUnlessEqual(buf.raw, b"ABCD") buf.value = "x" - self.failUnlessEqual(buf.value, "x") - self.failUnlessEqual(buf.raw, "x\000CD") + self.failUnlessEqual(buf.value, b"x") + self.failUnlessEqual(buf.raw, b"x\000CD") buf[1] = "Z" - self.failUnlessEqual(buf.value, "xZCD") - self.failUnlessEqual(buf.raw, "xZCD") + self.failUnlessEqual(buf.value, b"xZCD") + self.failUnlessEqual(buf.raw, b"xZCD") self.assertRaises(ValueError, setattr, buf, "value", "aaaaaaaa") self.assertRaises(TypeError, setattr, buf, "value", 42) @@ -27,8 +27,8 @@ class StringArrayTestCase(unittest.TestCase): def test_c_buffer_value(self): buf = c_buffer(32) - buf.value = "Hello, World" - self.failUnlessEqual(buf.value, "Hello, World") + buf.value = b"Hello, World" + self.failUnlessEqual(buf.value, b"Hello, World") self.failUnlessRaises(TypeError, setattr, buf, "value", memoryview(b"Hello, World")) self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc")) @@ -38,7 +38,7 @@ class StringArrayTestCase(unittest.TestCase): buf = c_buffer(32) buf.raw = memoryview(b"Hello, World") - self.failUnlessEqual(buf.value, "Hello, World") + self.failUnlessEqual(buf.value, b"Hello, World") self.assertRaises(TypeError, setattr, buf, "value", memoryview(b"abc")) self.assertRaises(ValueError, setattr, buf, "raw", memoryview(b"x" * 100)) diff --git a/Lib/ctypes/test/test_unicode.py b/Lib/ctypes/test/test_unicode.py index 717805e263..0eae59b088 100644 --- a/Lib/ctypes/test/test_unicode.py +++ b/Lib/ctypes/test/test_unicode.py @@ -111,18 +111,18 @@ else: ctypes.set_conversion_mode("ascii", "replace") buf = ctypes.create_string_buffer("ab\xe4\xf6\xfc") - self.failUnlessEqual(buf[:], "ab???\0") - self.failUnlessEqual(buf[::], "ab???\0") - self.failUnlessEqual(buf[::-1], "\0???ba") - self.failUnlessEqual(buf[::2], "a??") - self.failUnlessEqual(buf[6:5:-1], "") + self.failUnlessEqual(buf[:], b"ab???\0") + self.failUnlessEqual(buf[::], b"ab???\0") + self.failUnlessEqual(buf[::-1], b"\0???ba") + self.failUnlessEqual(buf[::2], b"a??") + self.failUnlessEqual(buf[6:5:-1], b"") ctypes.set_conversion_mode("ascii", "ignore") buf = ctypes.create_string_buffer("ab\xe4\xf6\xfc") # is that correct? not sure. But with 'ignore', you get what you pay for.. - self.failUnlessEqual(buf[:], "ab\0\0\0\0") - self.failUnlessEqual(buf[::], "ab\0\0\0\0") - self.failUnlessEqual(buf[::-1], "\0\0\0\0ba") + self.failUnlessEqual(buf[:], b"ab\0\0\0\0") + self.failUnlessEqual(buf[::], b"ab\0\0\0\0") + self.failUnlessEqual(buf[::-1], b"\0\0\0\0ba") if __name__ == '__main__': unittest.main() diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index cca570ae2b..f92263030b 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -763,7 +763,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value) static PyObject * CharArray_get_raw(CDataObject *self) { - return PyString_FromStringAndSize(self->b_ptr, self->b_size); + return PyBytes_FromStringAndSize(self->b_ptr, self->b_size); } static PyObject * @@ -774,7 +774,7 @@ CharArray_get_value(CDataObject *self) for (i = 0; i < self->b_size; ++i) if (*ptr++ == '\0') break; - return PyString_FromStringAndSize(self->b_ptr, i); + return PyBytes_FromStringAndSize(self->b_ptr, i); } static int @@ -1251,7 +1251,7 @@ c_void_p_from_param(PyObject *type, PyObject *value) } /* XXX struni: remove later */ /* string */ - if (PyString_Check(value)) { + if (PyBytes_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = getentry("z"); @@ -1452,7 +1452,7 @@ SimpleType_paramfunc(CDataObject *self) dict = PyObject_stgdict((PyObject *)self); assert(dict); /* Cannot be NULL for CDataObject instances */ - fmt = PyString_AsString(dict->proto); + fmt = PyUnicode_AsString(dict->proto); assert(fmt); fd = getentry(fmt); @@ -1644,7 +1644,7 @@ SimpleType_from_param(PyObject *type, PyObject *value) assert(dict); /* I think we can rely on this being a one-character string */ - fmt = PyString_AsString(dict->proto); + fmt = PyUnicode_AsString(dict->proto); assert(fmt); fd = getentry(fmt); @@ -2667,7 +2667,7 @@ _validate_paramflags(PyTypeObject *type, PyObject *paramflags) char *name; PyObject *defval; PyObject *typ; - if (!PyArg_ParseTuple(item, "i|zO", &flag, &name, &defval)) { + if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) { PyErr_SetString(PyExc_TypeError, "paramflags must be a sequence of (int [,string [,value]]) tuples"); return 0; @@ -2705,8 +2705,12 @@ _get_name(PyObject *obj, char **pname) return 1; } #endif - if (PyString_Check(obj) || PyUnicode_Check(obj)) { - *pname = PyString_AsString(obj); + if (PyBytes_Check(obj)) { + *pname = PyBytes_AS_STRING(obj); + return *pname ? 1 : 0; + } + if (PyUnicode_Check(obj)) { + *pname = PyUnicode_AsString(obj); return *pname ? 1 : 0; } PyErr_SetString(PyExc_TypeError, @@ -2966,7 +2970,7 @@ _byref(PyObject *obj) } static PyObject * -_get_arg(int *pindex, char *name, PyObject *defval, PyObject *inargs, PyObject *kwds) +_get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds) { PyObject *v; @@ -2976,7 +2980,7 @@ _get_arg(int *pindex, char *name, PyObject *defval, PyObject *inargs, PyObject * Py_INCREF(v); return v; } - if (kwds && (v = PyDict_GetItemString(kwds, name))) { + if (kwds && (v = PyDict_GetItem(kwds, name))) { ++*pindex; Py_INCREF(v); return v; @@ -3057,15 +3061,15 @@ _build_callargs(CFuncPtrObject *self, PyObject *argtypes, PyObject *item = PyTuple_GET_ITEM(paramflags, i); PyObject *ob; int flag; - char *name = NULL; + PyObject *name = NULL; PyObject *defval = NULL; /* This way seems to be ~2 us faster than the PyArg_ParseTuple calls below. */ - /* We HAVE already checked that the tuple can be parsed with "i|zO", so... */ + /* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */ Py_ssize_t tsize = PyTuple_GET_SIZE(item); flag = PyInt_AS_LONG(PyTuple_GET_ITEM(item, 0)); - name = tsize > 1 ? PyString_AS_STRING(PyTuple_GET_ITEM(item, 1)) : NULL; + name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL; defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL; switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { @@ -3730,10 +3734,10 @@ Array_subscript(PyObject *_self, PyObject *item) char *dest; if (slicelen <= 0) - return PyString_FromString(""); + return PyBytes_FromStringAndSize("", 0); if (step == 1) { - return PyString_FromStringAndSize(ptr + start, - slicelen); + return PyBytes_FromStringAndSize(ptr + start, + slicelen); } dest = (char *)PyMem_Malloc(slicelen); @@ -3745,7 +3749,7 @@ Array_subscript(PyObject *_self, PyObject *item) dest[i] = ptr[cur]; } - np = PyString_FromStringAndSize(dest, slicelen); + np = PyBytes_FromStringAndSize(dest, slicelen); PyMem_Free(dest); return np; } @@ -4407,10 +4411,10 @@ Pointer_subscript(PyObject *_self, PyObject *item) char *dest; if (len <= 0) - return PyString_FromString(""); + return PyBytes_FromStringAndSize("", 0); if (step == 1) { - return PyString_FromStringAndSize(ptr + start, - len); + return PyBytes_FromStringAndSize(ptr + start, + len); } dest = (char *)PyMem_Malloc(len); if (dest == NULL) @@ -4418,7 +4422,7 @@ Pointer_subscript(PyObject *_self, PyObject *item) for (cur = start, i = 0; i < len; cur += step, i++) { dest[i] = ptr[cur]; } - np = PyString_FromStringAndSize(dest, len); + np = PyBytes_FromStringAndSize(dest, len); PyMem_Free(dest); return np; } @@ -4629,7 +4633,7 @@ create_comerror(void) ++methods; } - s = PyString_FromString(comerror_doc); + s = PyUnicode_FromString(comerror_doc); if (s == NULL) return -1; status = PyDict_SetItemString(dict, "__doc__", s); @@ -4654,8 +4658,8 @@ static PyObject * string_at(const char *ptr, int size) { if (size == -1) - return PyString_FromString(ptr); - return PyString_FromStringAndSize(ptr, size); + return PyBytes_FromStringAndSize(ptr, strlen(ptr)); + return PyBytes_FromStringAndSize(ptr, size); } static int diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index 385add4787..9f5e5d029a 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -365,7 +365,7 @@ long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) static PyObject *context; if (context == NULL) - context = PyString_FromString("_ctypes.DllGetClassObject"); + context = PyUnicode_FromString("_ctypes.DllGetClassObject"); mod = PyImport_ImportModule("ctypes"); if (!mod) { @@ -444,7 +444,7 @@ long Call_CanUnloadNow(void) static PyObject *context; if (context == NULL) - context = PyString_FromString("_ctypes.DllCanUnloadNow"); + context = PyUnicode_FromString("_ctypes.DllCanUnloadNow"); mod = PyImport_ImportModule("ctypes"); if (!mod) { diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c index 1c66c48be8..bc524f7a34 100644 --- a/Modules/_ctypes/callproc.c +++ b/Modules/_ctypes/callproc.c @@ -770,7 +770,7 @@ void Extend_Error_Info(PyObject *exc_class, char *fmt, ...) PyObject *tp, *v, *tb, *s, *cls_str, *msg_str; va_start(vargs, fmt); - s = PyString_FromFormatV(fmt, vargs); + s = PyUnicode_FromFormatV(fmt, vargs); va_end(vargs); if (!s) return; @@ -779,18 +779,18 @@ void Extend_Error_Info(PyObject *exc_class, char *fmt, ...) PyErr_NormalizeException(&tp, &v, &tb); cls_str = PyObject_Str(tp); if (cls_str) { - PyString_ConcatAndDel(&s, cls_str); - PyString_ConcatAndDel(&s, PyString_FromString(": ")); + PyUnicode_AppendAndDel(&s, cls_str); + PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": ")); if (s == NULL) goto error; } else PyErr_Clear(); - msg_str = PyObject_Str(v); + msg_str = PyObject_Unicode(v); if (msg_str) - PyString_ConcatAndDel(&s, msg_str); + PyUnicode_AppendAndDel(&s, msg_str); else { PyErr_Clear(); - PyString_ConcatAndDel(&s, PyString_FromString("???")); + PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???")); if (s == NULL) goto error; } @@ -1087,34 +1087,18 @@ The handle may be used to locate exported functions in this\n\ module.\n"; static PyObject *load_library(PyObject *self, PyObject *args) { - TCHAR *name; + WCHAR *name; PyObject *nameobj; PyObject *ignored; HMODULE hMod; if (!PyArg_ParseTuple(args, "O|O:LoadLibrary", &nameobj, &ignored)) return NULL; -#ifdef _UNICODE - name = alloca((PyString_Size(nameobj) + 1) * sizeof(WCHAR)); - if (!name) { - PyErr_NoMemory(); - return NULL; - } - { - int r; - char *aname = PyString_AsString(nameobj); - if(!aname) - return NULL; - r = MultiByteToWideChar(CP_ACP, 0, aname, -1, name, PyString_Size(nameobj) + 1); - name[r] = 0; - } -#else - name = PyString_AsString(nameobj); - if(!name) + name = PyUnicode_AsUnicode(nameobj); + if (!name) return NULL; -#endif - hMod = LoadLibrary(name); + hMod = LoadLibraryW(name); if (!hMod) return PyErr_SetFromWindowsErr(GetLastError()); #ifdef _WIN64 diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 534bb1e81d..3e9464eab9 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -1169,6 +1169,14 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size) *(char *)ptr = PyBytes_AsString(value)[0]; _RET(value); } + if (PyInt_Check(value)) + { + long longval = PyInt_AS_LONG(value); + if (longval < 0 || longval >= 256) + goto error; + *(char *)ptr = (char)longval; + _RET(value); + } error: PyErr_Format(PyExc_TypeError, "one character string expected"); |