[Python-3000-checkins] r60385 - in python/branches/py3k-ctypes-pep3118: Lib/ctypes/test/test_pep3118.py Modules/_ctypes/_ctypes.c

thomas.heller python-3000-checkins at python.org
Mon Jan 28 08:58:47 CET 2008


Author: thomas.heller
Date: Mon Jan 28 08:58:46 2008
New Revision: 60385

Modified:
   python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
Log:
Bugfix and test for explicit big and little endian types.

Modified: python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py	(original)
+++ python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py	Mon Jan 28 08:58:46 2008
@@ -22,7 +22,31 @@
             ob = tp()
             v = memoryview(ob)
             try:
-                self.failUnlessEqual(normalize(v.format), fmt)
+                self.failUnlessEqual(normalize(v.format), normalize(fmt))
+                self.failUnlessEqual(v.size, sizeof(ob))
+                self.failUnlessEqual(v.itemsize, sizeof(itemtp))
+                self.failUnlessEqual(v.shape, shape)
+                # ctypes object always have a non-strided memory block
+                self.failUnlessEqual(v.strides, None)
+                # they are always read/write
+                self.failIf(v.readonly)
+
+                if v.shape:
+                    n = 1
+                    for dim in v.shape:
+                        n = n * dim
+                    self.failUnlessEqual(v.itemsize * n, v.size)
+            except:
+                # so that we can see the failing type
+                print(tp)
+                raise
+
+    def test_endian_types(self):
+        for tp, fmt, shape, itemtp in endian_types:
+            ob = tp()
+            v = memoryview(ob)
+            try:
+                self.failUnlessEqual(v.format, fmt)
                 self.failUnlessEqual(v.size, sizeof(ob))
                 self.failUnlessEqual(v.itemsize, sizeof(itemtp))
                 self.failUnlessEqual(v.shape, shape)
@@ -119,5 +143,18 @@
 
     ]
 
+class BEPoint(BigEndianStructure):
+    _fields_ = [("x", c_long), ("y", c_long)]
+
+class LEPoint(LittleEndianStructure):
+    _fields_ = [("x", c_long), ("y", c_long)]
+
+endian_types = [
+    (BEPoint,                   "T{>l:x:>l:y:}",        None,           BEPoint),
+    (LEPoint,                   "T{<l:x:<l:y:}",        None,           LEPoint),
+    (POINTER(BEPoint),          "&T{>l:x:>l:y:}",       None,           POINTER(BEPoint)),
+    (POINTER(LEPoint),          "&T{<l:x:<l:y:}",       None,           POINTER(LEPoint)),
+    ]
+
 if __name__ == "__main__":
     unittest.main()

Modified: python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c	(original)
+++ python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c	Mon Jan 28 08:58:46 2008
@@ -1823,14 +1823,14 @@
 		PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
 		PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
 		/* We are creating the type for the OTHER endian */
-		sw_dict->format = alloc_format_string("<", stgdict->format);
+		sw_dict->format = alloc_format_string("<", stgdict->format+1);
 #else
 		PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
 		PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
 		PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
 		PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
 		/* We are creating the type for the OTHER endian */
-		sw_dict->format = alloc_format_string(">", stgdict->format);
+		sw_dict->format = alloc_format_string(">", stgdict->format+1);
 #endif
 		Py_DECREF(swapped);
 		if (PyErr_Occurred()) {


More information about the Python-3000-checkins mailing list