Skip to content

gh-117142: ctypes: Migrate global vars to module state #117189

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 12 commits into from
Mar 29, 2024
24 changes: 14 additions & 10 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ PyCPointerType_init(PyObject *self, PyObject *args, PyObject *kwds)
}
if (proto) {
const char *current_format;
if (-1 == PyCPointerType_SetProto(st, stginfo, proto)) {
if (PyCPointerType_SetProto(st, stginfo, proto) < 0) {
Py_DECREF(proto);
return -1;
}
Expand Down Expand Up @@ -1224,7 +1224,7 @@ PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
return NULL;
}

if (-1 == PyCPointerType_SetProto(st, info, type)) {
if (PyCPointerType_SetProto(st, info, type) < 0) {
Py_DECREF(attrdict);
return NULL;
}
Expand Down Expand Up @@ -1978,12 +1978,13 @@ static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type,
if (!swapped_args)
return NULL;

if (st->swapped_suffix == NULL)
if (st->swapped_suffix == NULL) {
#ifdef WORDS_BIGENDIAN
st->swapped_suffix = PyUnicode_InternFromString("_le");
#else
st->swapped_suffix = PyUnicode_InternFromString("_be");
#endif
}
if (st->swapped_suffix == NULL) {
Py_DECREF(swapped_args);
return NULL;
Expand Down Expand Up @@ -2558,7 +2559,7 @@ PyCFuncPtrType_init(PyObject *self, PyObject *args, PyObject *kwds)
}
stginfo->flags |= TYPEFLAG_ISPOINTER;

if (-1 == make_funcptrtype_dict(st, attrdict, stginfo)) {
if (make_funcptrtype_dict(st, attrdict, stginfo) < 0) {
Py_DECREF(attrdict);
return -1;
}
Expand Down Expand Up @@ -3042,8 +3043,9 @@ PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src,
if (PyStgInfo_FromType(st, type, &info) < 0) {
return NULL;
}
if (info && info->getfunc && !_ctypes_simple_instance(st, type))
if (info && info->getfunc && !_ctypes_simple_instance(st, type)) {
return info->getfunc(adr, size);
}
return PyCData_FromBaseObj(st, type, src, index, adr);
}

Expand Down Expand Up @@ -3676,9 +3678,9 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
paramflags = NULL;

ctypes_state *st = GLOBAL_STATE();
if (!_validate_paramflags(st, type, paramflags))
if (!_validate_paramflags(st, type, paramflags)) {
return NULL;

}
self = (PyCFuncPtrObject *)_GenericPyCData_new(st, type, args, kwds);
self->index = index + 0x1000;
self->paramflags = Py_XNewRef(paramflags);
Expand Down Expand Up @@ -4790,8 +4792,9 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)

if (st->array_cache == NULL) {
st->array_cache = PyDict_New();
if (st->array_cache == NULL)
if (st->array_cache == NULL) {
return NULL;
}
}
len = PyLong_FromSsize_t(length);
if (len == NULL)
Expand Down Expand Up @@ -4834,7 +4837,7 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
Py_DECREF(key);
return NULL;
}
if (-1 == PyDict_SetItemProxy(st, st->array_cache, key, result)) {
if (PyDict_SetItemProxy(st, st->array_cache, key, result) < 0) {
Py_DECREF(key);
Py_DECREF(result);
return NULL;
Expand Down Expand Up @@ -5461,8 +5464,9 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
ctypes_state *st = GLOBAL_STATE();

CDataObject *result;
if (0 == cast_check_pointertype(st, ctype))
if (cast_check_pointertype(st, ctype) == 0) {
return NULL;
}
result = (CDataObject *)_PyObject_CallNoArgs(ctype);
if (result == NULL)
return NULL;
Expand Down
16 changes: 9 additions & 7 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ _ctypes_get_errobj(ctypes_state *st, int **pspace)
}
if (st->error_object_name == NULL) {
st->error_object_name = PyUnicode_InternFromString("ctypes.error_object");
if (st->error_object_name == NULL)
if (st->error_object_name == NULL) {
return NULL;
}
}
if (PyDict_GetItemRef(dict, st->error_object_name, &errobj) < 0) {
return NULL;
Expand All @@ -187,8 +188,7 @@ _ctypes_get_errobj(ctypes_state *st, int **pspace)
PyMem_Free(space);
return NULL;
}
if (-1 == PyDict_SetItem(dict, st->error_object_name,
errobj)) {
if (PyDict_SetItem(dict, st->error_object_name, errobj) < 0) {
Py_DECREF(errobj);
return NULL;
}
Expand Down Expand Up @@ -1022,9 +1022,10 @@ static PyObject *GetResult(ctypes_state *st,
if (info->getfunc == _ctypes_get_fielddesc("O")->getfunc) {
Py_DECREF(retval);
}
} else
}
else {
retval = PyCData_FromBaseObj(st, restype, NULL, 0, result);

}
if (!checker || !retval)
return retval;

Expand Down Expand Up @@ -1464,8 +1465,9 @@ copy_com_pointer(PyObject *self, PyObject *args)
a.keep = b.keep = NULL;

ctypes_state *st = GLOBAL_STATE();
if (-1 == ConvParam(st, p1, 0, &a) || -1 == ConvParam(st, p2, 1, &b))
if (ConvParam(st, p1, 0, &a) < 0 || ConvParam(st, p2, 1, &b) < 0) {
goto done;
}
src = (IUnknown *)a.value.p;
pdst = (IUnknown **)b.value.p;

Expand Down Expand Up @@ -1988,7 +1990,7 @@ create_pointer_type(PyObject *module, PyObject *cls)
PyErr_SetString(PyExc_TypeError, "must be a ctypes type");
return NULL;
}
if (-1 == PyDict_SetItem(st->_ctypes_ptrtype_cache, key, result)) {
if (PyDict_SetItem(st->_ctypes_ptrtype_cache, key, result) < 0) {
Py_DECREF(result);
Py_DECREF(key);
return NULL;
Expand Down