[Python-checkins] r64968 - in python/trunk: Lib/ctypes/test/test_pep3118.py Modules/_ctypes/_ctypes.c

thomas.heller python-checkins at python.org
Tue Jul 15 19:03:08 CEST 2008


Author: thomas.heller
Date: Tue Jul 15 19:03:08 2008
New Revision: 64968

Log:
Issue #3258: Fix an assertion error (in debug build) and a crash (in
release build) when the format string of a pointer to an incomplete
structure is created.


Modified:
   python/trunk/Lib/ctypes/test/test_pep3118.py
   python/trunk/Modules/_ctypes/_ctypes.c

Modified: python/trunk/Lib/ctypes/test/test_pep3118.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_pep3118.py	(original)
+++ python/trunk/Lib/ctypes/test/test_pep3118.py	Tue Jul 15 19:03:08 2008
@@ -33,6 +33,8 @@
 def normalize(format):
     # Remove current endian specifier and white space from a format
     # string
+    if format is None:
+        return ""
     format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
     return re.sub(r"\s", "", format)
 
@@ -105,6 +107,14 @@
 class aUnion(Union):
     _fields_ = [("a", c_int)]
 
+class Incomplete(Structure):
+    pass
+
+class Complete(Structure):
+    pass
+PComplete = POINTER(Complete)
+Complete._fields_ = [("a", c_int)]
+
 ################################################################
 #
 # This table contains format strings as they look on little endian
@@ -162,6 +172,16 @@
     # the pep does't support unions
     (aUnion,                    "B",                    None,           aUnion),
 
+    ## pointer to incomplete structure
+    (Incomplete,                "B",                    None,           Incomplete),
+    (POINTER(Incomplete),       "&B",                   None,           POINTER(Incomplete)),
+
+    # 'Complete' is a structure that starts incomplete, but is completed after the
+    # pointer type to it has been created.
+    (Complete,                  "T{<l:a:}",             None,           Complete),
+    # Unfortunately the pointer format string is not fixed...
+    (POINTER(Complete),         "&B",                   None,           POINTER(Complete)),
+
     ## other
 
     # function signatures are not implemented

Modified: python/trunk/Modules/_ctypes/_ctypes.c
==============================================================================
--- python/trunk/Modules/_ctypes/_ctypes.c	(original)
+++ python/trunk/Modules/_ctypes/_ctypes.c	Tue Jul 15 19:03:08 2008
@@ -386,6 +386,11 @@
 	}
 	Py_DECREF(result->tp_dict);
 	result->tp_dict = (PyObject *)dict;
+	dict->format = alloc_format_string(NULL, "B");
+	if (dict->format == NULL) {
+		Py_DECREF(result);
+		return NULL;
+	}
 
 	dict->paramfunc = StructUnionType_paramfunc;
 
@@ -907,7 +912,13 @@
 	if (proto) {
 		StgDictObject *itemdict = PyType_stgdict(proto);
 		assert(itemdict);
-		stgdict->format = alloc_format_string("&", itemdict->format);
+		/* If itemdict->format is NULL, then this is a pointer to an
+		   incomplete type.  We create a generic format string
+		   'pointer to bytes' in this case.  XXX Better would be to
+		   fix the format string later...
+		*/
+		stgdict->format = alloc_format_string("&",
+			      itemdict->format ? itemdict->format : "B");
 		if (stgdict->format == NULL) {
 			Py_DECREF((PyObject *)stgdict);
 			return NULL;


More information about the Python-checkins mailing list