Skip to content
Snippets Groups Projects
Unverified Commit 343eb0f9 authored by Nikita Sobolev's avatar Nikita Sobolev Committed by GitHub
Browse files

gh-99275: Fix `SystemError` in `ctypes` during `__initsubclass__` (#99283)

parent d329f859
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,15 @@ class X(Structure): ...@@ -54,6 +54,15 @@ class X(Structure):
x.char = b'a\0b\0' x.char = b'a\0b\0'
self.assertEqual(bytes(x), b'a\x00###') self.assertEqual(bytes(x), b'a\x00###')
def test_gh99275(self):
class BrokenStructure(Structure):
def __init_subclass__(cls, **kwargs):
cls._fields_ = [] # This line will fail, `stgdict` is not ready
with self.assertRaisesRegex(TypeError,
'ctypes state is not initialized'):
class Subclass(BrokenStructure): ...
# __set__ and __get__ should raise a TypeError in case their self # __set__ and __get__ should raise a TypeError in case their self
# argument is not a ctype instance. # argument is not a ctype instance.
def test___set__(self): def test___set__(self):
......
Fix ``SystemError`` in :mod:`ctypes` when exception was not set during
``__initsubclass__``.
...@@ -424,8 +424,11 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct ...@@ -424,8 +424,11 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
} }
stgdict = PyType_stgdict(type); stgdict = PyType_stgdict(type);
if (!stgdict) if (!stgdict) {
PyErr_SetString(PyExc_TypeError,
"ctypes state is not initialized");
return -1; return -1;
}
/* If this structure/union is already marked final we cannot assign /* If this structure/union is already marked final we cannot assign
_fields_ anymore. */ _fields_ anymore. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment