This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: assertion failure in ctypes in case an _anonymous_ attr appears outside _fields_
Type: crash Stage: resolved
Components: ctypes Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-09-16 09:57 by Oren Milman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3615 merged Oren Milman, 2017-09-16 11:33
PR 3774 merged python-dev, 2017-09-26 23:24
PR 3780 merged python-dev, 2017-09-27 05:51
PR 3952 merged Oren Milman, 2017-10-11 13:24
Messages (6)
msg302331 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-09-16 09:57
The following code causes an assertion failure:
import ctypes
class BadStruct(ctypes.Structure):
    _fields_ = []
    _anonymous_ = ['foo']
    foo = None


this is because MakeAnonFields() (in Modules/_ctypes/stgdict.c) goes over the
names specified in _anonymous_, and looks each name up in the class by calling
PyObject_GetAttr().
in case an attribute of such a name is found (i.e. PyObject_GetAttr() succeeded),
MakeAnonFields() assumes that the attribute was created by MakeFields(), so it
asserts the type of the attribute is PyCField_Type.

however, PyObject_GetAttr() would succeed also in case it is a normal attribute
specified by the user, but isn't specified in _fields_, as in the code above.
in such a case, the type of the attribute is not PyCField_Type, and so the
assertion fails.
msg302363 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-17 10:45
New changeset 30b61b51e05d2d43e8e2e783b0a9df738535423b by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is defined only outside _fields_. (#3615)
https://github.com/python/cpython/commit/30b61b51e05d2d43e8e2e783b0a9df738535423b
msg303091 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-27 04:37
New changeset bdb215b18a42360b6a9c82876fa71f19ca1a416d by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is defined only outside _fields_. (GH-3615) (#3774)
https://github.com/python/cpython/commit/bdb215b18a42360b6a9c82876fa71f19ca1a416d
msg303108 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-27 06:52
New changeset 9bfa55bfea60553831e99dbbca6a7c7d1065fd4b by Serhiy Storchaka (Miss Islington (bot)) in branch '2.7':
[2.7] bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is defined only outside _fields_. (GH-3615) (#3780)
https://github.com/python/cpython/commit/9bfa55bfea60553831e99dbbca6a7c7d1065fd4b
msg303908 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-08 12:02
In 2.7 "%U" is not recognized in the format string.

>>> class Name(Structure):
...     _fields_ = []
...     _anonymous_ = ["x"]
...     x = 42
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '%U' is specified in _anonymous_ but not in _fields_
msg304243 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-12 14:39
New changeset fb3bb8d5d5d70acaaa0fdec15c137544fdd4463f by Serhiy Storchaka (Oren Milman) in branch '2.7':
[2.7] bpo-31490: Fix an assertion failure in ctypes in case an _anonymous_ attr is defined only outside _fields_. (GH-3615) (#3952)
https://github.com/python/cpython/commit/fb3bb8d5d5d70acaaa0fdec15c137544fdd4463f
History
Date User Action Args
2022-04-11 14:58:52adminsetgithub: 75671
2017-10-12 15:02:42serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
2017-10-12 14:39:03serhiy.storchakasetmessages: + msg304243
2017-10-11 13:24:32Oren Milmansetstage: resolved -> patch review
pull_requests: + pull_request3927
2017-10-08 12:02:58serhiy.storchakasetstatus: closed -> open

messages: + msg303908
2017-09-27 06:53:22serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-09-27 06:52:46serhiy.storchakasetmessages: + msg303108
2017-09-27 05:51:08python-devsetpull_requests: + pull_request3767
2017-09-27 04:37:39serhiy.storchakasetmessages: + msg303091
2017-09-26 23:24:06python-devsetpull_requests: + pull_request3761
2017-09-17 10:46:13serhiy.storchakasetversions: + Python 2.7, Python 3.6
2017-09-17 10:45:40serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg302363
2017-09-16 11:33:24Oren Milmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request3606
2017-09-16 09:57:38Oren Milmancreate