Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Add a lock for generic helper functions.
  • Loading branch information
ZeroIntensity committed Jan 4, 2025
commit 8bb980f519854417ebb28c1550dd8c57e0b0f4a3
32 changes: 25 additions & 7 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3216,7 +3216,6 @@ PyCData_AtAddress(ctypes_state *st, PyObject *type, void *buf)
return NULL;
}
assert(CDataObject_Check(st, pd));
// XXX Use an atomic load if the size is 1, 2, 4, or 8 bytes?
pd->b_ptr = (char *)buf;
pd->b_length = info->length;
pd->b_size = info->size;
Expand All @@ -3242,15 +3241,23 @@ PyObject *
PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src,
Py_ssize_t index, Py_ssize_t size, char *adr)
{
if (getfunc)
return getfunc(adr, size);
CDataObject *cdata = (CDataObject *)src;
if (getfunc) {
LOCK_PTR(cdata);
PyObject *res = getfunc(adr, size);
UNLOCK_PTR(cdata);
return res;
}
assert(type);
StgInfo *info;
if (PyStgInfo_FromType(st, type, &info) < 0) {
return NULL;
}
if (info && info->getfunc && !_ctypes_simple_instance(st, type)) {
return info->getfunc(adr, size);
LOCK_PTR(cdata);
PyObject *res = info->getfunc(adr, size);
UNLOCK_PTR(cdata);
return res;
}
return PyCData_FromBaseObj(st, type, src, index, adr);
}
Expand All @@ -3267,15 +3274,22 @@ _PyCData_set(ctypes_state *st,
int err;

if (setfunc) {
return setfunc(ptr, value, size);
LOCK_PTR(dst);
PyObject *res = setfunc(ptr, value, size);
UNLOCK_PTR(dst);
return res;
}
if (!CDataObject_Check(st, value)) {
StgInfo *info;
if (PyStgInfo_FromType(st, type, &info) < 0) {
return NULL;
}
if (info && info->setfunc)
return info->setfunc(ptr, value, size);
if (info && info->setfunc) {
LOCK_PTR(dst);
PyObject *res = info->setfunc(ptr, value, size);
UNLOCK_PTR(dst);
return res;
}
/*
If value is a tuple, we try to call the type with the tuple
and use the result!
Expand All @@ -3295,7 +3309,9 @@ _PyCData_set(ctypes_state *st,
Py_DECREF(ob);
return result;
} else if (value == Py_None && PyCPointerTypeObject_Check(st, type)) {
LOCK_PTR(dst);
*(void **)ptr = NULL;
UNLOCK_PTR(dst);
Py_RETURN_NONE;
} else {
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -3345,7 +3361,9 @@ _PyCData_set(ctypes_state *st,
((PyTypeObject *)type)->tp_name);
return NULL;
}
LOCK_PTR(dst);
Comment thread
ZeroIntensity marked this conversation as resolved.
Outdated
*(void **)ptr = src->b_ptr;
UNLOCK_PTR(dst);

keep = GetKeepedObjects(src);
if (keep == NULL)
Expand Down