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

(cherry picked from commit 343eb0f94b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2022-11-13 11:51:26 -08:00 committed by GitHub
parent 72d356e358
commit bf76d9bd4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View File

@ -54,6 +54,15 @@ class X(Structure):
x.char = b'a\0b\0'
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
# argument is not a ctype instance.
def test___set__(self):

View File

@ -0,0 +1,2 @@
Fix ``SystemError`` in :mod:`ctypes` when exception was not set during
``__initsubclass__``.

View File

@ -430,8 +430,11 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
}
stgdict = PyType_stgdict(type);
if (!stgdict)
if (!stgdict) {
PyErr_SetString(PyExc_TypeError,
"ctypes state is not initialized");
return -1;
}
/* If this structure/union is already marked final we cannot assign
_fields_ anymore. */