Commit 30b61b51 authored by Oren Milman's avatar Oren Milman Committed by Serhiy Storchaka

bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is...

bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is defined only outside _fields_. (#3615)
parent a6bb313c
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)]
......
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.
......@@ -302,7 +302,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. */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment