[Python-checkins] r52842 - in python/branches/release25-maint: Lib/ctypes/test/test_structures.py Misc/NEWS Modules/_ctypes/stgdict.c

thomas.heller python-checkins at python.org
Fri Nov 24 20:00:41 CET 2006


Author: thomas.heller
Date: Fri Nov 24 20:00:39 2006
New Revision: 52842

Modified:
   python/branches/release25-maint/Lib/ctypes/test/test_structures.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Modules/_ctypes/stgdict.c
Log:
Fix bug #1598620: A ctypes structure cannot contain itself.

Backport from trunk.

Modified: python/branches/release25-maint/Lib/ctypes/test/test_structures.py
==============================================================================
--- python/branches/release25-maint/Lib/ctypes/test/test_structures.py	(original)
+++ python/branches/release25-maint/Lib/ctypes/test/test_structures.py	Fri Nov 24 20:00:39 2006
@@ -381,5 +381,35 @@
         s.p = None
         self.failUnlessEqual(s.x, 12345678)
 
+class TestRecursiveStructure(unittest.TestCase):
+    def test_contains_itself(self):
+        class Recursive(Structure):
+            pass
+
+        try:
+            Recursive._fields_ = [("next", Recursive)]
+        except AttributeError, details:
+            self.failUnless("Structure or union cannot contain itself" in
+                            str(details))
+        else:
+            self.fail("Structure or union cannot contain itself")
+
+
+    def test_vice_versa(self):
+        class First(Structure):
+            pass
+        class Second(Structure):
+            pass
+
+        First._fields_ = [("second", Second)]
+
+        try:
+            Second._fields_ = [("first", First)]
+        except AttributeError, details:
+            self.failUnless("_fields_ is final" in
+                            str(details))
+        else:
+            self.fail("AttributeError not raised")
+
 if __name__ == '__main__':
     unittest.main()

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Fri Nov 24 20:00:39 2006
@@ -70,6 +70,8 @@
 Extension Modules
 -----------------
 
+- Bug #1598620: A ctypes Structure cannot contain itself.
+ 
 - Bug #1588217: don't parse "= " as a soft line break in binascii's
   a2b_qp() function, instead leave it in the string as quopri.decode()
   does.

Modified: python/branches/release25-maint/Modules/_ctypes/stgdict.c
==============================================================================
--- python/branches/release25-maint/Modules/_ctypes/stgdict.c	(original)
+++ python/branches/release25-maint/Modules/_ctypes/stgdict.c	Fri Nov 24 20:00:39 2006
@@ -339,14 +339,14 @@
 	stgdict = PyType_stgdict(type);
 	if (!stgdict)
 		return -1;
+	/* If this structure/union is already marked final we cannot assign
+	   _fields_ anymore. */
+
 	if (stgdict->flags & DICTFLAG_FINAL) {/* is final ? */
 		PyErr_SetString(PyExc_AttributeError,
 				"_fields_ is final");
 		return -1;
 	}
-	/* XXX This should probably be moved to a point when all this
-	   stuff is sucessfully finished. */
-	stgdict->flags |= DICTFLAG_FINAL;	/* set final */
 
 	if (stgdict->ffi_type_pointer.elements)
 		PyMem_Free(stgdict->ffi_type_pointer.elements);
@@ -480,5 +480,15 @@
 	stgdict->size = size;
 	stgdict->align = total_align;
 	stgdict->length = len;	/* ADD ffi_ofs? */
+
+	/* We did check that this flag was NOT set above, it must not
+	   have been set until now. */
+	if (stgdict->flags & DICTFLAG_FINAL) {
+		PyErr_SetString(PyExc_AttributeError,
+				"Structure or union cannot contain itself");
+		return -1;
+	}
+	stgdict->flags |= DICTFLAG_FINAL;
+
 	return MakeAnonFields(type);
 }


More information about the Python-checkins mailing list