-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
ctypes Stucture._fields_ cannot be set when Structure has been subclassed #100980
Copy link
Copy link
Closed
Labels
Description
Bug report
Currently the method for defining Structure fields that cannot be defined within the class, is to define the class with pass and then set _fields_ outside. But ctypes.Structure incorrectly raises AttributeError, even when no class has ever set a _fields_.
from ctypes import *
class PyObject(Structure):
pass
class PyVarObject(PyObject):
pass
class PyTypeObject(PyVarObject):
pass
PyObject._fields_ = [
("ob_refcnt", c_long),
("ob_type", POINTER(PyTypeObject)),
] PyObject._fields_ = [
^^^^^^^^^^^^^^^^^
AttributeError: _fields_ is finalStrangely, suppressing this Attribute error reveals the _fields_ attribute does actually get set?
with suppress(AttributeError):
PyObject._fields_ = [
("ob_refcnt", c_long),
("ob_type", POINTER(PyTypeObject)),
]
print(PyObject._fields_)
>> [('ob_refcnt', <class 'ctypes.c_long'>), ('ob_type', <class '__main__.LP_PyTypeObject'>)]Your environment
- CPython versions tested on: 3.11.1, main
- Operating system and architecture: macOS ARM, ubuntu x64
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Projects
Status
Done