Visitar URL original
gh-78878: Fix crash when creating an instance of `_ctypes.CField` by shihai1991 · Pull Request #14837 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge main
  • Loading branch information
shihai1991 committed Aug 4, 2022
commit 76eeee2811ed48deba02aa5d83c029c164772d59
8 changes: 8 additions & 0 deletions Lib/test/test_ctypes/test_struct_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ class Y(X):

def test_5(self):
class X(Structure):
<<<<<<< HEAD:Lib/ctypes/test/test_struct_fields.py
_fields_ = [("x", c_int)]
CField = type(X.x)
self.assertRaises(TypeError, CField)
=======
_fields_ = (("char", c_char * 5),)

x = X(b'#' * 5)
x.char = b'a\0b\0'
self.assertEqual(bytes(x), b'a\x00###')
>>>>>>> origin/main:Lib/test/test_ctypes/test_struct_fields.py

# __set__ and __get__ should raise a TypeError in case their self
# argument is not a ctype instance.
Expand Down
4 changes: 4 additions & 0 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
#define CONT_BITFIELD 2
#define EXPAND_BITFIELD 3

<<<<<<< HEAD
self = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
=======
self = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
>>>>>>> origin/main
if (self == NULL)
return NULL;
dict = PyType_stgdict(desc);
Expand Down
4 changes: 4 additions & 0 deletions Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ MakeFields(PyObject *type, CFieldObject *descr,
}
continue;
}
<<<<<<< HEAD
new_descr = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
=======
new_descr = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
>>>>>>> origin/main
if (new_descr == NULL) {
Py_DECREF(fdescr);
Py_DECREF(fieldlist);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.