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

thomas.heller python-3000-checkins at python.org
Sat Jan 19 22:56:12 CET 2008


Author: thomas.heller
Date: Sat Jan 19 22:56:12 2008
New Revision: 60108

Added:
   python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py   (contents, props changed)
Modified:
   python/branches/py3k-ctypes-pep3118/Modules/_ctypes/_ctypes.c
Log:
Always use explicit endian specifiers for simple types, and a bug fix
too.  Add unittest.


Added: python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py
==============================================================================
--- (empty file)
+++ python/branches/py3k-ctypes-pep3118/Lib/ctypes/test/test_pep3118.py	Sat Jan 19 22:56:12 2008
@@ -0,0 +1,51 @@
+import unittest
+from ctypes import *
+import struct, sys
+
+if sys.byteorder == "little":
+    ENDIAN = "<"
+else:
+    ENDIAN = ">"
+
+simple_types = [
+    ("b", c_byte),
+    ("B", c_ubyte),
+    ("h", c_short),
+    ("H", c_ushort),
+# c_int and c_uint may be aliases to c_long
+##    ("i", c_int),
+##    ("I", c_uint),
+    ("l", c_long),
+    ("L", c_ulong),
+    ("q", c_longlong),
+    ("Q", c_ulonglong),
+    ("f", c_float),
+    ("d", c_double),
+# c_longdouble may be an alias to c_double
+##    ("g", c_longdouble),
+    ("t", c_bool),
+# struct doesn't support this (yet)
+##    ("O", py_object),
+]
+
+class Test(unittest.TestCase):
+
+    def test_simpletypes(self):
+        # simple types in native byte order
+        for fmt, typ in simple_types:
+            v = memoryview(typ())
+
+            # check the PEP3118 format string
+            self.failUnlessEqual(v.format, ENDIAN + fmt)
+
+            # shape and strides are None for integral types
+            self.failUnlessEqual((v.shape, v.strides),
+                                 (None, None))
+
+            # size and itemsize must be what struct.calcsize reports
+            struct_size = struct.calcsize(fmt)
+            self.failUnlessEqual((v.size, v.itemsize),
+                                 (struct_size, struct_size))
+
+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	Sat Jan 19 22:56:12 2008
@@ -1596,7 +1596,11 @@
 	stgdict->size = fmt->pffi_type->size;
 	stgdict->setfunc = fmt->setfunc;
 	stgdict->getfunc = fmt->getfunc;
-	stgdict->format = alloc_format_string(NULL, proto_str);
+#ifdef WORDS_BIGENDIAN
+	stgdict->format = alloc_format_string(">", proto_str);
+#else
+	stgdict->format = alloc_format_string("<", proto_str);
+#endif
 	if (stgdict->format == NULL) {
 		Py_DECREF(result);
 		Py_DECREF((PyObject *)stgdict);
@@ -1669,18 +1673,20 @@
 		}
 		sw_dict = PyType_stgdict(swapped);
 #ifdef WORDS_BIGENDIAN
-		sw_dict->format = alloc_format_string("<", stgdict->format);
 		PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
 		PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
 		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);
 #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);
 #endif
-		sw_dict->format = alloc_format_string("<", stgdict->format);
 		Py_DECREF(swapped);
 		if (PyErr_Occurred()) {
 			Py_DECREF(result);


More information about the Python-3000-checkins mailing list