← 返回首页
[3.11] gh-78878: Fix crash when creating an instance of `_ctypes.CField` (GH-14837) by miss-islington · Pull Request #100413 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (1) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
6 changes: 6 additions & 0 deletions Lib/ctypes/test/test_struct_fields.py
Show comments View file Edit file Delete file Open in desktop
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,12 @@ class X(Structure):
x.char = b'a\0b\0'
self.assertEqual(bytes(x), b'a\x00###')

def test_6(self):
class X(Structure):
_fields_ = [("x", c_int)]
CField = type(X.x)
self.assertRaises(TypeError, CField)

def test_gh99275(self):
class BrokenStructure(Structure):
def __init_subclass__(cls, **kwargs):
Expand Down
Show comments View file Edit file Delete file Open in desktop
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 @@
Fix crash when creating an instance of :class:`!_ctypes.CField`.
11 changes: 2 additions & 9 deletions Modules/_ctypes/cfield.c
Show comments View file Edit file Delete file Open in desktop
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 @@ -30,13 +30,6 @@ static void pymem_destructor(PyObject *ptr)
/*
PyCField_Type
*/
static PyObject *
PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
CFieldObject *obj;
obj = (CFieldObject *)type->tp_alloc(type, 0);
return (PyObject *)obj;
}

/*
* Expects the size, index and offset for the current field in *psize and
Expand Down Expand Up @@ -68,7 +61,7 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
#define CONT_BITFIELD 2
#define EXPAND_BITFIELD 3

self = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
self = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
if (self == NULL)
return NULL;
dict = PyType_stgdict(desc);
Expand Down Expand Up @@ -343,7 +336,7 @@ PyTypeObject PyCField_Type = {
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
PyCField_new, /* tp_new */
0, /* tp_new */
0, /* tp_free */
};

Expand Down
2 changes: 1 addition & 1 deletion Modules/_ctypes/stgdict.c
Show comments View file Edit file Delete file Open in desktop
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 @@ -258,7 +258,7 @@ MakeFields(PyObject *type, CFieldObject *descr,
}
continue;
}
new_descr = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
new_descr = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
if (new_descr == NULL) {
Py_DECREF(fdescr);
Py_DECREF(fieldlist);
Expand Down
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.