[Python-3000-checkins] r56325 - in python/branches/py3k-struni: Lib/ctypes/__init__.py Lib/ctypes/test/test_unicode.py Modules/_ctypes/_ctypes.c

thomas.heller python-3000-checkins at python.org
Thu Jul 12 21:06:25 CEST 2007


Author: thomas.heller
Date: Thu Jul 12 21:06:25 2007
New Revision: 56325

Modified:
   python/branches/py3k-struni/Lib/ctypes/__init__.py
   python/branches/py3k-struni/Lib/ctypes/test/test_unicode.py
   python/branches/py3k-struni/Modules/_ctypes/_ctypes.c
Log:
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.


Modified: python/branches/py3k-struni/Lib/ctypes/__init__.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/__init__.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/__init__.py	Thu Jul 12 21:06:25 2007
@@ -281,7 +281,7 @@
         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

Modified: python/branches/py3k-struni/Lib/ctypes/test/test_unicode.py
==============================================================================
--- python/branches/py3k-struni/Lib/ctypes/test/test_unicode.py	(original)
+++ python/branches/py3k-struni/Lib/ctypes/test/test_unicode.py	Thu Jul 12 21:06:25 2007
@@ -26,7 +26,7 @@
             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 @@
             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 @@
             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")
 

Modified: python/branches/py3k-struni/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/py3k-struni/Modules/_ctypes/_ctypes.c	(original)
+++ python/branches/py3k-struni/Modules/_ctypes/_ctypes.c	Thu Jul 12 21:06:25 2007
@@ -847,7 +847,7 @@
 {
 	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 @@
 		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");
 


More information about the Python-3000-checkins mailing list