Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
6155211
Move paramfunc into a new struct, PyStgInfo
encukou Feb 16, 2024
0171c30
Move the StgInfo out of StgDictObject and into the classes themselves
encukou Feb 16, 2024
acd91e9
Move StgInfo init/retrieval right after the corresponding StgDict one
encukou Feb 19, 2024
55af504
Move `size` from StgDict to StgInfo
encukou Feb 19, 2024
27e5c50
Move `align` from StgDict to StgInfo
encukou Feb 19, 2024
01bc637
Move `length` from StgDict to StgInfo, except for sizeof calculation
encukou Feb 19, 2024
c8017e9
Move part of the sizeof calculation
encukou Feb 19, 2024
40b012f
Move `ffi_type_pointer` from StgDict to StgInfo
encukou Feb 19, 2024
537defb
Move `proto` from StgDict to StgInfo. Doesn't work just yet.
encukou Feb 20, 2024
ace6623
Hack: Reserve space for StgInfo in static type objects
encukou Feb 20, 2024
6f775b9
If StgInfo is not initialized, act as if it's not there
encukou Feb 20, 2024
4aaa06a
Move `getfunc` & `setfunc`
encukou Feb 20, 2024
6808524
Move `argtypes`
encukou Feb 20, 2024
f031c00
Move `converters`
encukou Feb 20, 2024
535176c
Move `restype`
encukou Feb 20, 2024
5260003
Move `checker`
encukou Feb 20, 2024
0d3017e
Move `flags`
encukou Feb 20, 2024
269b825
Move `format`
encukou Feb 20, 2024
946f474
Move `ndim`
encukou Feb 20, 2024
607ed23
Move `shape`
encukou Feb 21, 2024
cb126b7
Replace more uses of StgDict
encukou Feb 21, 2024
fd04c91
Don't pass dicts to PyCStgDict_clone
encukou Feb 21, 2024
8dce549
Don't set StgDict as type dict; use name `attrdict` for the type __di…
encukou Feb 21, 2024
6705e0e
Remove more uses of StgDict
encukou Feb 21, 2024
8f99377
Remove StgDict
encukou Feb 21, 2024
daea3e5
Remove StgDict from comments, names, and platform-specific code
encukou Feb 21, 2024
f67211f
Switch PyCSimpleType_new to _init
encukou Feb 23, 2024
02aa9e1
Switch _SimpleCData to a heap type
encukou Feb 23, 2024
13feac7
Convert Struct and Union type _new to _init
encukou Feb 23, 2024
a68417b
Switch PyCPointerType_new to _init
encukou Feb 23, 2024
ad5d7b4
Switch PyCFuncPtrType_new to _init
encukou Feb 23, 2024
8d5e230
Switch CFuncPtr to heap type
encukou Feb 23, 2024
cdfae13
Switch Array to heap type
encukou Feb 23, 2024
2d80960
Switch _Pointer to a heap type
encukou Feb 23, 2024
3337ba4
Switch _ctypes.Union to heap type
encukou Feb 23, 2024
596cb89
Switch Structure to heap type
encukou Feb 23, 2024
91feb64
Make the type-adding macros more regular
encukou Feb 23, 2024
77c64fe
Switch _CData to a heap type
encukou Feb 23, 2024
0be46a1
Visit/decref type in PyCData visit/dealloc
encukou Feb 23, 2024
9be3520
Remove _HackyHeapType
encukou Feb 23, 2024
e523c2f
Decref attrdicts
encukou Mar 1, 2024
fb70e43
Delegate CType_Type deallocation to PyType_Type.tp_dealloc
encukou Mar 5, 2024
cc59dea
Fix Windows-specific crashes
neonene Mar 5, 2024
9bd9eb1
Update comments
encukou Mar 6, 2024
946a0b1
Add tests for metaclass details
encukou Mar 6, 2024
970b41d
Fix getting the module state
encukou Mar 7, 2024
2a450ad
Add a blurb, even though this *should* not affect usage
encukou Mar 7, 2024
9878809
Apply suggestions from code review
encukou Mar 11, 2024
b2ba89a
Inline PyStgInfo_From* & PyStgInfo_Init for performance
encukou Mar 11, 2024
605c30c
Make CType_Type's clear & dealloc safer
encukou Mar 11, 2024
41dea1b
Remove self decrefs in error cases in init functions
encukou Mar 11, 2024
be0888f
Add `static` to PyType_Spec
encukou Mar 11, 2024
ac6eb38
Remove redundant comment
encukou Mar 11, 2024
33be37f
Apply suggestions from code review
encukou Mar 11, 2024
0b9d0a7
Fix comment
encukou Mar 12, 2024
f02bad6
PyErr_WriteUnraisable clears the exception
encukou Mar 12, 2024
16faac7
Call tp_init for argument validation
encukou Mar 20, 2024
2461e50
Add test for swapped type creation
neonene Mar 20, 2024
ab90768
Merge branch 'main' into ctypes-no-stgdict
encukou Mar 20, 2024
de054af
Fix test_swapped_type_creation
encukou Mar 20, 2024
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
Switch PyCPointerType_new to _init
  • Loading branch information
encukou committed Mar 7, 2024
commit a68417b53c451754f2c82f49c97ede5ef13b5c66
66 changes: 22 additions & 44 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1150,35 +1150,25 @@ PyCPointerType_paramfunc(CDataObject *self)
return parg;
}

static PyObject *
PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
PyCPointerType_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyTypeObject *result;
PyObject *proto;
PyObject *typedict;


typedict = PyTuple_GetItem(args, 2);
if (!typedict) {
return NULL;
}

/* create the new instance (which is a class,
since we are a metatype!) */
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
if (result == NULL) {
return NULL;
return -1;
}

/*
stginfo items size, align, length contain info about pointers itself,
stginfo->proto has info about the pointed to type!
*/
ctypes_state *st = GLOBAL_STATE();
StgInfo *stginfo = PyStgInfo_Init(st, result);
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject *)self);
if (!stginfo) {
Py_DECREF((PyObject *)result);
return NULL;
return -1;
}
stginfo->size = sizeof(void *);
stginfo->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
Expand All @@ -1188,21 +1178,18 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
stginfo->flags |= TYPEFLAG_ISPOINTER;

if (PyDict_GetItemRef(typedict, &_Py_ID(_type_), &proto) < 0) {
Py_DECREF((PyObject *)result);
return NULL;
return -1;
}
if (proto) {
const char *current_format;
if (-1 == PyCPointerType_SetProto(stginfo, proto)) {
Py_DECREF(proto);
Py_DECREF((PyObject *)result);
return NULL;
return -1;
}
StgInfo *iteminfo;
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) {
Py_DECREF(proto);
Py_DECREF((PyObject *)result);
return NULL;
return -1;
}
/* PyCPointerType_SetProto has verified proto has a stginfo. */
assert(iteminfo);
Expand All @@ -1221,12 +1208,11 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
Py_DECREF(proto);
if (stginfo->format == NULL) {
Py_DECREF((PyObject *)result);
return NULL;
return -1;
}
}

return (PyObject *)result;
return 0;
}


Expand Down Expand Up @@ -1327,7 +1313,7 @@ static PyType_Slot pycpointer_type_slots[] = {
{Py_tp_traverse, CDataType_traverse},
{Py_tp_clear, CDataType_clear},
{Py_tp_methods, PyCPointerType_methods},
{Py_tp_new, PyCPointerType_new},
{Py_tp_init, PyCPointerType_init},

// Sequence protocol.
{Py_sq_repeat, CDataType_repeat},
Expand All @@ -1347,7 +1333,7 @@ static PyType_Spec pycpointer_type_spec = {
PyCArrayType_Type
*/
/*
PyCArrayType_new ensures that the new Array subclass created has a _length_
PyCArrayType_init ensures that the new Array subclass created has a _length_
attribute, and a _type_ attribute.
*/

Expand Down Expand Up @@ -1531,25 +1517,18 @@ PyCArrayType_paramfunc(CDataObject *self)
return p;
}

static PyObject *
PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static int
PyCArrayType_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyTypeObject *result;
PyObject *length_attr, *type_attr;
Py_ssize_t length;
Py_ssize_t itemsize, itemalign;

/* create the new instance (which is a class,
since we are a metatype!) */
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
if (result == NULL)
return NULL;

/* Initialize these variables to NULL so that we can simplify error
handling by using Py_XDECREF. */
type_attr = NULL;

if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_length_), &length_attr) < 0) {
if (PyObject_GetOptionalAttr(self, &_Py_ID(_length_), &length_attr) < 0) {
goto error;
}
if (!length_attr) {
Expand Down Expand Up @@ -1582,7 +1561,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
goto error;
}

if (PyObject_GetOptionalAttr((PyObject *)result, &_Py_ID(_type_), &type_attr) < 0) {
if (PyObject_GetOptionalAttr(self, &_Py_ID(_type_), &type_attr) < 0) {
goto error;
}
if (!type_attr) {
Expand All @@ -1592,7 +1571,7 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}

ctypes_state *st = GLOBAL_STATE();
StgInfo *stginfo = PyStgInfo_Init(st, result);
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject*)self);
if (!stginfo) {
goto error;
}
Expand Down Expand Up @@ -1650,26 +1629,25 @@ PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
A permanent annoyance: char arrays are also strings!
*/
if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
if (-1 == add_getset(result, CharArray_getsets))
if (-1 == add_getset((PyTypeObject*)self, CharArray_getsets))
goto error;
}
else if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
if (-1 == add_getset(result, WCharArray_getsets))
if (-1 == add_getset((PyTypeObject*)self, WCharArray_getsets))
goto error;
}

return (PyObject *)result;
return 0;
error:
Py_XDECREF(type_attr);
Py_DECREF(result);
return NULL;
return -1;
}

static PyType_Slot pycarray_type_slots[] = {
{Py_tp_doc, PyDoc_STR("metatype for the Array Objects")},
{Py_tp_traverse, CDataType_traverse},
{Py_tp_methods, CDataType_methods},
{Py_tp_new, PyCArrayType_new},
{Py_tp_init, PyCArrayType_init},
{Py_tp_clear, CDataType_clear},

// Sequence protocol.
Expand Down