FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-99275: Fix `SystemError` in `ctypes` during `__initsubclass__` by sobolevn · Pull Request #99283 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions Lib/test/test_ctypes/test_struct_fields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``SystemError`` in :mod:`ctypes` when exception was not set during
``__initsubclass__``.
5 changes: 4 additions & 1 deletion Modules/_ctypes/stgdict.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,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. */

Expand Down

Back | FazBrowse Home | New Git URL