diff options
author | Thomas Heller <theller@ctypes.org> | 2007-07-12 19:06:25 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2007-07-12 19:06:25 (GMT) |
commit | 60831316df8893e973f92592b7c4c95ad489ff15 (patch) | |
tree | 6e788f97e7c2874b12540a1cb30c946896efb926 | |
parent | d0762282022ebb9809e488325cf86bb71e1371c0 (diff) | |
download | cpython-60831316df8893e973f92592b7c4c95ad489ff15.zip cpython-60831316df8893e973f92592b7c4c95ad489ff15.tar.gz cpython-60831316df8893e973f92592b7c4c95ad489ff15.tar.bz2 |
Accept bytes for c_wchar_p instances and c_wchar array instances.
ctypes.create_unicode_buffer also accepts bytes now.
Revert some tests in test_unicode: Since string literals are unicode
now, conversion takes place when byte literals are passed as unicode
parameters.
-rw-r--r-- | Lib/ctypes/__init__.py | 2 | ||||
-rw-r--r-- | Lib/ctypes/test/test_unicode.py | 8 | ||||
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 4 |
3 files changed, 7 insertions, 7 deletions
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 8c9fe1a..86678ea 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 3a89d8d..bf5486c 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 5d4e7c5..bab1e35 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"); |