]> granicus.if.org Git - python/commitdiff
Issue #8966: If a ctypes structure field is an array of c_char, convert its
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 11 Jun 2010 21:50:30 +0000 (21:50 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 11 Jun 2010 21:50:30 +0000 (21:50 +0000)
value to bytes instead of str (as done for c_char and c_char_p).

Lib/ctypes/test/test_bytes.py
Lib/ctypes/test/test_structures.py
Misc/NEWS
Modules/_ctypes/cfield.c

index 374b2d77519c7ea9af76b21c3e700e07255a8f1a..4a1d0fd71ae5598a29107cb4428336351223b60b 100644 (file)
@@ -30,8 +30,8 @@ class BytesTest(unittest.TestCase):
 
         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):
index c58d94989c6ae9832852b79b5987f9b90f03da29..fb048aa1e13d256d8691182b222d9b8b1600f056 100644 (file)
@@ -209,9 +209,9 @@ class StructureTestCase(unittest.TestCase):
         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 @@ class StructureTestCase(unittest.TestCase):
 
         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):
index b7b4f6512380e210e9992558e970fdba933203d6..1335dbc0d1ba7da2bb281ddc2c768a008b522516 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -421,6 +421,9 @@ C-API
 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 @@ Extension Modules
 -----------------
 
 - 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,
index 441ed9e447135a33fbeb729792421f4eb5d3c53e..876e2c0c75f5374c04260f7082ff3fdb5bd8d733 100644 (file)
@@ -1333,7 +1333,7 @@ s_get(void *ptr, Py_ssize_t size)
             break;
     }
 
-    return PyUnicode_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
+    return PyBytes_FromStringAndSize((char *)ptr, (Py_ssize_t)i);
 }
 
 static PyObject *