[Python-checkins] gh-99275: Fix `SystemError` in `ctypes` during `__initsubclass__` (GH-99283)
miss-islington
webhook-mailer at python.org
Sun Nov 13 14:40:24 EST 2022
https://github.com/python/cpython/commit/14c13955c59c15bd58520241ce64e27c68028d64
commit: 14c13955c59c15bd58520241ce64e27c68028d64
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-11-13T11:40:18-08:00
summary:
gh-99275: Fix `SystemError` in `ctypes` during `__initsubclass__` (GH-99283)
(cherry picked from commit 343eb0f94b26f2a4c1c15505d417e8157ec19660)
Co-authored-by: Nikita Sobolev <mail at sobolevn.me>
files:
A Misc/NEWS.d/next/Library/2022-11-09-12-16-35.gh-issue-99275.klOqoL.rst
M Lib/ctypes/test/test_struct_fields.py
M Modules/_ctypes/stgdict.c
diff --git a/Lib/ctypes/test/test_struct_fields.py b/Lib/ctypes/test/test_struct_fields.py
index ee8415f3e630..fefeaea1496a 100644
--- a/Lib/ctypes/test/test_struct_fields.py
+++ b/Lib/ctypes/test/test_struct_fields.py
@@ -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):
diff --git a/Misc/NEWS.d/next/Library/2022-11-09-12-16-35.gh-issue-99275.klOqoL.rst b/Misc/NEWS.d/next/Library/2022-11-09-12-16-35.gh-issue-99275.klOqoL.rst
new file mode 100644
index 000000000000..2bf05a3bdfbd
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-09-12-16-35.gh-issue-99275.klOqoL.rst
@@ -0,0 +1,2 @@
+Fix ``SystemError`` in :mod:`ctypes` when exception was not set during
+``__initsubclass__``.
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index 747339dee352..f9811aced7c8 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -420,8 +420,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. */
More information about the Python-checkins
mailing list