import unittest
+import test.support
from ctypes import *
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)]
--- /dev/null
+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.
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. */