Skip to content

Commit

Permalink
[2.7] bpo-31490: Fix an assertion failure in ctypes in case an _anony…
Browse files Browse the repository at this point in the history
…mous_ attr is defined only outside _fields_. (GH-3615) (#3780)

(cherry picked from commit 30b61b5)
  • Loading branch information
miss-islington authored and serhiy-storchaka committed Sep 27, 2017
1 parent 81691b0 commit 9bfa55b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Lib/ctypes/test/test_anon.py
@@ -1,4 +1,5 @@
import unittest
from test.support import cpython_only
from ctypes import *

class AnonTest(unittest.TestCase):
Expand Down Expand Up @@ -35,6 +36,18 @@ def test_anon_nonmember(self):
{"_fields_": [],
"_anonymous_": ["x"]}))

@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)]
Expand Down
@@ -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.
10 changes: 9 additions & 1 deletion Modules/_ctypes/stgdict.c
Expand Up @@ -286,7 +286,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. */
Expand Down

0 comments on commit 9bfa55b

Please sign in to comment.