8000 gh-103092: Isolate _ctypes, part 1 by erlend-aasland · Pull Request #103893 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103092: Isolate _ctypes, part 1 #103893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 27, 2023
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
Adapt PyCField type
  • Loading branch information
erlend-aasland committed Apr 26, 2023
commit bcd391fc950bbbae96bed8808f74802a62f3a1c4
3 changes: 1 addition & 2 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5686,8 +5686,7 @@ _ctypes_add_types(PyObject *mod)
* Simple classes
*/

/* PyCField_Type is derived from PyBaseObject_Type */
TYPE_READY(&PyCField_Type);
CREATE_TYPE(mod, st->PyCField_Type, &cfield_spec);

/*************************************************
*
Expand Down
67 changes: 25 additions & 42 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
#define CONT_BITFIELD 2
#define EXPAND_BITFIELD 3

self = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
ctypes_state *st = GLOBAL_STATE();
PyTypeObject *tp = st->PyCField_Type;
self = (CFieldObject *)tp->tp_alloc(tp, 0);
if (self == NULL)
return NULL;
dict = PyType_stgdict(desc);
Expand Down 8000 Expand Up @@ -256,6 +258,7 @@ static PyGetSetDef PyCField_getset[] = {
static int
PyCField_traverse(CFieldObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->proto);
return 0;
}
Expand All @@ -270,9 +273,11 @@ PyCField_clear(CFieldObject *self)
static void
PyCField_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
PyCField_clear((CFieldObject *)self);
(void)PyCField_clear((CFieldObject *)self);
Py_TYPE(self)->tp_free((PyObject *)self);
Py_DECREF(tp);
}

static PyObject *
Expand All @@ -296,46 +301,24 @@ PyCField_repr(CFieldObject *self)
return result;
}

PyTypeObject PyCField_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_ctypes.CField", /* tp_name */
sizeof(CFieldObject), /* tp_basicsize */
0, /* tp_itemsize */
PyCField_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
(reprfunc)PyCField_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
PyDoc_STR("Structure/Union member"), /* tp_doc */
(traverseproc)PyCField_traverse, /* tp_traverse */
(inquiry)PyCField_clear, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
0, /* tp_members */
PyCField_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
(descrgetfunc)PyCField_get, /* tp_descr_get */
(descrsetfunc)PyCField_set, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0, /* tp_free */
static PyType_Slot cfield_slots[] = {
{Py_tp_dealloc, PyCField_dealloc},
{Py_tp_repr, PyCField_repr},
{Py_tp_doc, (void *)PyDoc_STR("Structure/Union member")},
{Py_tp_traverse, PyCField_traverse},
{Py_tp_clear, PyCField_clear},
{Py_tp_getset, PyCField_getset},
{Py_tp_descr_get, PyCField_get},
{Py_tp_descr_set, PyCField_set},
{0, NULL},
};

PyType_Spec cfield_spec = {
.name = "_ctypes.CField",
.basicsize = sizeof(CFieldObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = cfield_slots,
};


Expand Down
3 changes: 2 additions & 1 deletion Modules/_ctypes/ctypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
typedef struct {
PyTypeObject *DictRemover_Type;
PyTypeObject *PyCArg_Type;
PyTypeObject *PyCField_Type;
PyTypeObject *PyCThunk_Type;
} ctypes_state;

Expand All @@ -43,6 +44,7 @@ extern ctypes_state global_state;
#define GLOBAL_STATE() (&global_state)

extern PyType_Spec carg_spec;
extern PyType_Spec cfield_spec;
extern PyType_Spec cthunk_spec;

typedef struct tagPyCArgObject PyCArgObject;
Expand Down Expand Up @@ -153,7 +155,6 @@ extern PyTypeObject PyCSimpleType_Type;
#define PyCSimpleTypeObject_CheckExact(v) Py_IS_TYPE(v, &PyCSimpleType_Type)
#define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type)

extern PyTypeObject PyCField_Type;
extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);


Expand Down
12 changes: 8 additions & 4 deletions Modules/_ctypes/stgdict.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ MakeFields(PyObject *type, CFieldObject *descr,
if (fieldlist == NULL)
return -1;

ctypes_state *st = GLOBAL_STATE();
PyTypeObject *cfield_tp = st->PyCField_Type;
for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) {
PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */
PyObject *fname, *ftype, *bits;
Expand All @@ -240,7 +242,7 @@ MakeFields(PyObject *type, CFieldObject *descr,
Py_DECREF(fieldlist);
return -1;
}
if (!Py_IS_TYPE(fdescr, &PyCField_Type)) {
if (!Py_IS_TYPE(fdescr, cfield_tp)) {
PyErr_SetString(PyExc_TypeError, "unexpected type");
Py_DECREF(fdescr);
Py_DECREF(fieldlist);
Expand All @@ -257,13 +259,13 @@ MakeFields(PyObject *type, CFieldObject *descr,
}
continue;
}
new_descr = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
new_descr = (CFieldObject *)cfield_tp->tp_alloc(cfield_tp, 0);
if (new_descr == NULL) {
Py_DECREF(fdescr);
Py_DECREF(fieldlist);
return -1;
}
assert(Py_IS_TYPE(new_descr, &PyCField_Type));
assert(Py_IS_TYPE(new_descr, cfield_tp));
new_descr->size = fdescr->size;
new_descr->offset = fdescr->offset + offset;
new_descr->index = fdescr->index + index;
Expand Down Expand Up @@ -304,14 +306,16 @@ MakeAnonFields(PyObject *type)
if (anon_names == NULL)
return -1;

ctypes_state *st = GLOBAL_STATE();
PyTypeObject *cfield_tp = st->PyCField_Type;
for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) {
PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */
CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname);
if (descr == NULL) {
Py_DECREF(anon_names);
return -1;
}
if (!Py_IS_TYPE(descr, &PyCField_Type)) {
if (!Py_IS_TYPE(descr, cfield_tp)) {
PyErr_Format(PyExc_AttributeError,
"'%U' is specified in _anonymous_ but not in "
"_fields_",
Expand Down
2 changes: 0 additions & 2 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,10 @@ Modules/_ctypes/_ctypes.c - Struct_Type -
Modules/_ctypes/_ctypes.c - UnionType_Type -
Modules/_ctypes/_ctypes.c - Union_Type -
Modules/_ctypes/callproc.c - PyCArg_Type -
Modules/_ctypes/cfield.c - PyCField_Type -
Modules/_ctypes/ctypes.h - PyCArg_Type -
Modules/_ctypes/ctypes.h - PyCArrayType_Type -
Modules/_ctypes/ctypes.h - PyCArray_Type -
Modules/_ctypes/ctypes.h - PyCData_Type -
Modules/_ctypes/ctypes.h - PyCField_Type -
Modules/_ctypes/ctypes.h - PyCFuncPtrType_Type -
Modules/_ctypes/ctypes.h - PyCFuncPtr_Type -
Modules/_ctypes/ctypes.h - PyCPointerType_Type -
Expand Down
0