[Python-checkins] r81911 - in python/branches/py3k: Lib/ctypes/test/test_bytes.py Lib/ctypes/test/test_structures.py Misc/NEWS Modules/_ctypes/cfield.c

victor.stinner python-checkins at python.org
Fri Jun 11 23:50:30 CEST 2010


Author: victor.stinner
Date: Fri Jun 11 23:50:30 2010
New Revision: 81911

Log:
Issue #8966: If a ctypes structure field is an array of c_char, convert its
value to bytes instead of str (as done for c_char and c_char_p).


Modified:
   python/branches/py3k/Lib/ctypes/test/test_bytes.py
   python/branches/py3k/Lib/ctypes/test/test_structures.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/_ctypes/cfield.c

Modified: python/branches/py3k/Lib/ctypes/test/test_bytes.py
==============================================================================
--- python/branches/py3k/Lib/ctypes/test/test_bytes.py	(original)
+++ python/branches/py3k/Lib/ctypes/test/test_bytes.py	Fri Jun 11 23:50:30 2010
@@ -30,8 +30,8 @@
 
         X("abc")
         x = X(b"abc")
-        self.assertEqual(x.a, "abc")
-        self.assertEqual(type(x.a), str)
+        self.assertEqual(x.a, b"abc")
+        self.assertEqual(type(x.a), bytes)
 
     def test_struct_W(self):
         class X(Structure):

Modified: python/branches/py3k/Lib/ctypes/test/test_structures.py
==============================================================================
--- python/branches/py3k/Lib/ctypes/test/test_structures.py	(original)
+++ python/branches/py3k/Lib/ctypes/test/test_structures.py	Fri Jun 11 23:50:30 2010
@@ -209,9 +209,9 @@
         self.assertRaises(TypeError, Person, "Name", "HI")
 
         # short enough
-        self.assertEqual(Person("12345", 5).name, "12345")
+        self.assertEqual(Person("12345", 5).name, b"12345")
         # exact fit
-        self.assertEqual(Person("123456", 5).name, "123456")
+        self.assertEqual(Person("123456", 5).name, b"123456")
         # too long
         self.assertRaises(ValueError, Person, "1234567", 5)
 
@@ -269,9 +269,9 @@
 
         p = Person("Someone", ("1234", "5678"), 5)
 
-        self.assertEqual(p.name, "Someone")
-        self.assertEqual(p.phone.areacode, "1234")
-        self.assertEqual(p.phone.number, "5678")
+        self.assertEqual(p.name, b"Someone")
+        self.assertEqual(p.phone.areacode, b"1234")
+        self.assertEqual(p.phone.number, b"5678")
         self.assertEqual(p.age, 5)
 
     def test_structures_with_wchar(self):

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Jun 11 23:50:30 2010
@@ -421,6 +421,9 @@
 Library
 -------
 
+- Issue #8966: If a ctypes structure field is an array of c_char, convert its
+  value to bytes instead of str (as done for c_char and c_char_p).
+
 - Issue #8188: Comparisons between Decimal and Fraction objects are
   now permitted, returning a result based on the exact numerical
   values of the operands.  This builds on issue #2531, which allowed
@@ -1288,7 +1291,7 @@
 -----------------
 
 - Issue #3129: Trailing digits in format string are no longer ignored.
-  For example, "1" or "ilib123" are now invalid formats and cause 
+  For example, "1" or "ilib123" are now invalid formats and cause
   ``struct.error`` to be raised.
 
 - Issue #7384: If the system readline library is linked against ncurses,

Modified: python/branches/py3k/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/py3k/Modules/_ctypes/cfield.c	(original)
+++ python/branches/py3k/Modules/_ctypes/cfield.c	Fri Jun 11 23:50:30 2010
@@ -1333,7 +1333,7 @@
             break;
     }
 
-    return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
+    return PyBytes_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
 }
 
 static PyObject *


More information about the Python-checkins mailing list