diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 8c9fe1a6496..86678ea766c 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -281,7 +281,7 @@ else: create_unicode_buffer(anInteger) -> character array create_unicode_buffer(aString, anInteger) -> character array """ - if isinstance(init, str): + if isinstance(init, (str, bytes)): if size is None: size = len(init)+1 buftype = c_wchar * size diff --git a/Lib/ctypes/test/test_unicode.py b/Lib/ctypes/test/test_unicode.py index 3a89d8d156f..bf5486c777c 100644 --- a/Lib/ctypes/test/test_unicode.py +++ b/Lib/ctypes/test/test_unicode.py @@ -26,7 +26,7 @@ else: self.failUnlessEqual(wcslen("ab\u2070"), 3) # string args are converted self.failUnlessEqual(wcslen("abc"), 3) - self.failUnlessRaises(ctypes.ArgumentError, wcslen, "ab\xe4") + self.failUnlessRaises(ctypes.ArgumentError, wcslen, b"ab\xe4") def test_ascii_replace(self): ctypes.set_conversion_mode("ascii", "replace") @@ -41,7 +41,7 @@ else: self.failUnlessEqual(wcslen("ab\u2070"), 3) # ignore error mode skips non-ascii characters self.failUnlessEqual(wcslen("abc"), 3) - self.failUnlessEqual(wcslen("\xe4\xf6\xfc\xdf"), 0) + self.failUnlessEqual(wcslen(b"\xe4\xf6\xfc\xdf"), 0) def test_latin1_strict(self): ctypes.set_conversion_mode("latin-1", "strict") @@ -56,11 +56,11 @@ else: self.failUnlessEqual(len(buf), 3+1) ctypes.set_conversion_mode("ascii", "replace") - buf = ctypes.create_unicode_buffer("ab\xe4\xf6\xfc") + buf = ctypes.create_unicode_buffer(b"ab\xe4\xf6\xfc") self.failUnlessEqual(buf[:], "ab\uFFFD\uFFFD\uFFFD\0") ctypes.set_conversion_mode("ascii", "ignore") - buf = ctypes.create_unicode_buffer("ab\xe4\xf6\xfc") + buf = ctypes.create_unicode_buffer(b"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") diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 5d4e7c5f0fe..bab1e351346 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -847,7 +847,7 @@ WCharArray_set_value(CDataObject *self, PyObject *value) { Py_ssize_t result = 0; - if (PyString_Check(value)) { + if (PyBytes_Check(value)) { value = PyUnicode_FromEncodedObject(value, conversion_mode_encoding, conversion_mode_errors); @@ -1119,7 +1119,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value) Py_INCREF(Py_None); return Py_None; } - if (PyUnicode_Check(value) || PyString_Check(value)) { + if (PyUnicode_Check(value) || PyBytes_Check(value)) { PyCArgObject *parg; struct fielddesc *fd = getentry("Z");