]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 27 Sep 2017 04:37:37 +0000 (21:37 -0700)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 27 Sep 2017 04:37:37 +0000 (07:37 +0300)
(cherry picked from commit 30b61b51e05d2d43e8e2e783b0a9df738535423b)

Lib/ctypes/test/test_anon.py
Misc/NEWS.d/next/Core and Builtins/2017-09-16-13-32-35.bpo-31490.r7m2sj.rst [new file with mode: 0644]
Modules/_ctypes/stgdict.c

index d892b59898589fc8981c8cba7fc34e31683770ca..d378392ebe28443d36fe6eae9aafad7927b8cc46 100644 (file)
@@ -1,4 +1,5 @@
 import unittest
+import test.support
 from ctypes import *
 
 class AnonTest(unittest.TestCase):
@@ -35,6 +36,18 @@ class AnonTest(unittest.TestCase):
                                                       {"_fields_": [],
                                                        "_anonymous_": ["x"]}))
 
+    @test.support.cpython_only
+    def test_issue31490(self):
+        # There shouldn't be an assertion failure in case the class has an
+        # attribute whose name is specified in _anonymous_ but not in _fields_.
+
+        # AttributeError: 'x' is specified in _anonymous_ but not in _fields_
+        with self.assertRaises(AttributeError):
+            class Name(Structure):
+                _fields_ = []
+                _anonymous_ = ["x"]
+                x = 42
+
     def test_nested(self):
         class ANON_S(Structure):
             _fields_ = [("a", c_int)]
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-16-13-32-35.bpo-31490.r7m2sj.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-16-13-32-35.bpo-31490.r7m2sj.rst
new file mode 100644 (file)
index 0000000..d95e825
--- /dev/null
@@ -0,0 +1,3 @@
+Fix an assertion failure in `ctypes` class definition, in case the class has
+an attribute whose name is specified in ``_anonymous_`` but not in
+``_fields_``. Patch by Oren Milman.
index 716d1e9e8eb5df76d10078afff02072e9ecd71db..8278b2fbefbfc0110ef3d679c65421dbb24739e6 100644 (file)
@@ -282,7 +282,15 @@ MakeAnonFields(PyObject *type)
             Py_DECREF(anon_names);
             return -1;
         }
-        assert(Py_TYPE(descr) == &PyCField_Type);
+        if (Py_TYPE(descr) != &PyCField_Type) {
+            PyErr_Format(PyExc_AttributeError,
+                         "'%U' is specified in _anonymous_ but not in "
+                         "_fields_",
+                         fname);
+            Py_DECREF(anon_names);
+            Py_DECREF(descr);
+            return -1;
+        }
         descr->anonymous = 1;
 
         /* descr is in the field descriptor. */