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.
This commit is contained in:
Thomas Heller 2007-07-12 19:06:25 +00:00
parent d076228202
commit 60831316df
3 changed files with 7 additions and 7 deletions

View File

@ -281,7 +281,7 @@ else:
create_unicode_buffer(anInteger) -> character array create_unicode_buffer(anInteger) -> character array
create_unicode_buffer(aString, anInteger) -> character array create_unicode_buffer(aString, anInteger) -> character array
""" """
if isinstance(init, str): if isinstance(init, (str, bytes)):
if size is None: if size is None:
size = len(init)+1 size = len(init)+1
buftype = c_wchar * size buftype = c_wchar * size

View File

@ -26,7 +26,7 @@ else:
self.failUnlessEqual(wcslen("ab\u2070"), 3) self.failUnlessEqual(wcslen("ab\u2070"), 3)
# string args are converted # string args are converted
self.failUnlessEqual(wcslen("abc"), 3) 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): def test_ascii_replace(self):
ctypes.set_conversion_mode("ascii", "replace") ctypes.set_conversion_mode("ascii", "replace")
@ -41,7 +41,7 @@ else:
self.failUnlessEqual(wcslen("ab\u2070"), 3) self.failUnlessEqual(wcslen("ab\u2070"), 3)
# ignore error mode skips non-ascii characters # ignore error mode skips non-ascii characters
self.failUnlessEqual(wcslen("abc"), 3) 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): def test_latin1_strict(self):
ctypes.set_conversion_mode("latin-1", "strict") ctypes.set_conversion_mode("latin-1", "strict")
@ -56,11 +56,11 @@ else:
self.failUnlessEqual(len(buf), 3+1) self.failUnlessEqual(len(buf), 3+1)
ctypes.set_conversion_mode("ascii", "replace") 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") self.failUnlessEqual(buf[:], "ab\uFFFD\uFFFD\uFFFD\0")
ctypes.set_conversion_mode("ascii", "ignore") 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.. # 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")

View File

@ -847,7 +847,7 @@ WCharArray_set_value(CDataObject *self, PyObject *value)
{ {
Py_ssize_t result = 0; Py_ssize_t result = 0;
if (PyString_Check(value)) { if (PyBytes_Check(value)) {
value = PyUnicode_FromEncodedObject(value, value = PyUnicode_FromEncodedObject(value,
conversion_mode_encoding, conversion_mode_encoding,
conversion_mode_errors); conversion_mode_errors);
@ -1119,7 +1119,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }
if (PyUnicode_Check(value) || PyString_Check(value)) { if (PyUnicode_Check(value) || PyBytes_Check(value)) {
PyCArgObject *parg; PyCArgObject *parg;
struct fielddesc *fd = getentry("Z"); struct fielddesc *fd = getentry("Z");