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 StructParam type
  • Loading branch information
erlend-aasland committed Apr 26, 2023
commit a0d3a681bf5bfce96ca8f2586f2a62deb23ceee7
44 changes: 34 additions & 10 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,23 +413,45 @@ typedef struct {
PyObject *keep; // If set, a reference to the original CDataObject.
} StructParamObject;

static int
StructParam_traverse(StructParamObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
}

static int
StructParam_clear(StructParamObject *self)
{
Py_CLEAR(self->keep);
return 0;
}

static void
StructParam_dealloc(PyObject *myself)
{
StructParamObject *self = (StructParamObject *)myself;
Py_XDECREF(self->keep);
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(myself);
(void)StructParam_clear(self);
PyMem_Free(self->ptr);
Py_TYPE(self)->tp_free(myself);
tp->tp_free(myself);
Py_DECREF(tp);
}

static PyType_Slot structparam_slots[] = {
{Py_tp_traverse, StructParam_traverse},
{Py_tp_clear, StructParam_clear},
{Py_tp_dealloc, StructParam_dealloc},
{0, NULL},
};

static PyTypeObject StructParam_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_ctypes.StructParam_Type",
.tp_basicsize = sizeof(StructParamObject),
.tp_dealloc = StructParam_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
static PyType_Spec structparam_spec = {
.name = "_ctypes.StructParam_Type",
.basicsize = sizeof(StructParamObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_DISALLOW_INSTANTIATION),
.slots = structparam_slots,
};


Expand Down Expand Up @@ -458,7 +480,9 @@ StructUnionType_paramfunc(CDataObject *self)
/* Create a Python object which calls PyMem_Free(ptr) in
its deallocator. The object will be destroyed
at _ctypes_callproc() cleanup. */
obj = (&StructParam_Type)->tp_alloc(&StructParam_Type, 0);
ctypes_state *st = GLOBAL_STATE();
PyTypeObject *tp = st->StructParam_Type;
obj = tp->tp_alloc(tp, 0);
if (obj == NULL) {
PyMem_Free(ptr);
return NULL;
Expand Down Expand Up @@ -5694,7 +5718,7 @@ _ctypes_add_types(PyObject *mod)
*/

CREATE_TYPE(mod, st->DictRemover_Type, &dictremover_spec);
TYPE_READY(&StructParam_Type);
CREATE_TYPE(mod, st->StructParam_Type, &structparam_spec);

#ifdef MS_WIN32
TYPE_READY_BASE(&PyComError_Type, (PyTypeObject*)PyExc_Exception);
Expand Down
1 change: 1 addition & 0 deletions Modules/_ctypes/ctypes.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct {
PyTypeObject *PyCArg_Type;
PyTypeObject *PyCField_Type;
PyTypeObject *PyCThunk_Type;
PyTypeObject *StructParam_Type;
} ctypes_state;

extern ctypes_state global_state;
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ Modules/_ctypes/_ctypes.c - PyCPointer_Type -
Modules/_ctypes/_ctypes.c - PyCSimpleType_Type -
Modules/_ctypes/_ctypes.c - PyCStructType_Type -
Modules/_ctypes/_ctypes.c - Simple_Type -
Modules/_ctypes/_ctypes.c - StructParam_Type -
Modules/_ctypes/_ctypes.c - Struct_Type -
Modules/_ctypes/_ctypes.c - UnionType_Type -
Modules/_ctypes/_ctypes.c - Union_Type -
Expand Down
0