8000 gh-128182: add critical section to `_ctypes.Simple` getters and sette… · python/cpython@b9d8d99 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9d8d99

Browse files
gh-128182: add critical section to _ctypes.Simple getters and setters (#132081)
1 parent f7a8bc5 commit b9d8d99

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5179,15 +5179,21 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
51795179
*/
51805180

51815181
/*[clinic input]
5182-
class _ctypes.Simple "PyObject *" "clinic_state()->Simple_Type"
5182+
class _ctypes.Simple "CDataObject *" "clinic_state()->Simple_Type"
5183+
[clinic start generated code]*/
5184+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0493451fecf8cd4]*/
5185+
5186+
/*[clinic input]
5187+
@critical_section
5188+
@setter
5189+
_ctypes.Simple.value
51835190
[clinic start generated code]*/
5184-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=016c476c7aa8b8a8]*/
51855191

51865192
static int
5187-
Simple_set_value(PyObject *op, PyObject *value, void *Py 8000 _UNUSED(ignored))
5193+
_ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value)
5194+
/*[clinic end generated code: output=f267186118939863 input=977af9dc9e71e857]*/
51885195
{
51895196
PyObject *result;
5190-
CDataObject *self = _CDataObject_CAST(op);
51915197

51925198
if (value == NULL) {
51935199
PyErr_SetString(PyExc_TypeError,
@@ -5197,54 +5203,57 @@ Simple_set_value(PyObject *op, PyObject *value, void *Py_UNUSED(ignored))
51975203

51985204
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self)));
51995205
StgInfo *info;
5200-
if (PyStgInfo_FromObject(st, op, &info) < 0) {
5206+
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) {
52015207
return -1;
52025208
}
52035209
assert(info); /* Cannot be NULL for CDataObject instances */
52045210
assert(info->setfunc);
52055211

5206-
LOCK_PTR(self);
52075212
result = info->setfunc(self->b_ptr, value, info->size);
5208-
UNLOCK_PTR(self);
52095213
if (!result)
52105214
return -1;
52115215

52125216
/* consumes the refcount the setfunc returns */
52135217
return KeepRef(self, 0, result);
52145218
}
52155219

5220+
52165221
static int
52175222
Simple_init(PyObject *self, PyObject *args, PyObject *kw)
52185223
{
52195224
PyObject *value = NULL;
52205225
if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value))
52215226
return -1;
52225227
if (value)
5223-
return Simple_set_value(self, value, NULL);
5228+
return _ctypes_Simple_value_set(self, value, NULL);
52245229
return 0;
52255230
}
52265231

5232+
5233+
/*[clinic input]
5234+
@critical_section
5235+
@getter
5236+
_ctypes.Simple.value
5237+
[clinic start generated code]*/
5238+
52275239
static PyObject *
5228-
Simple_get_value(PyObject *op, void *Py_UNUSED(ignored))
5240+
_ctypes_Simple_value_get_impl(CDataObject *self)
5241+
/*[clinic end generated code: output=ce5a26570830a243 input=3ed3f735cec89282]*/
52295242
{
5230-
CDataObject *self = _CDataObject_CAST(op);
52315243
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self)));
52325244
StgInfo *info;
5233-
if (PyStgInfo_FromObject(st, op, &info) < 0) {
5245+
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) {
52345246
return NULL;
52355247
}
52365248
assert(info); /* Cannot be NULL for CDataObject instances */
52375249
assert(info->getfunc);
52385250
PyObject *res;
5239-
LOCK_PTR(self);
52405251
res = info->getfunc(self->b_ptr, self->b_size);
5241-
UNLOCK_PTR(self);
52425252
return res;
52435253
}
52445254

52455255
static PyGetSetDef Simple_getsets[] = {
5246-
{ "value", Simple_get_value, Simple_set_value,
5247-
"current value", NULL },
5256+
_CTYPES_SIMPLE_VALUE_GETSETDEF
52485257
{ NULL, NULL }
52495258
};
52505259

@@ -5265,7 +5274,7 @@ Simple_from_outparm_impl(PyObject *self, PyTypeObject *cls)
52655274
return Py_NewRef(self);
52665275
}
52675276
/* call stginfo->getfunc */
5268-
return Simple_get_value(self, NULL);
5277+
return _ctypes_Simple_value_get(self, NULL);
52695278
}
52705279

52715280
static PyMethodDef Simple_methods[] = {
@@ -5296,7 +5305,7 @@ Simple_repr(PyObject *self)
52965305
Py_TYPE(self)->tp_name, self);
52975306
}
52985307

5299-
val = Simple_get_value(self, NULL);
5308+
val = _ctypes_Simple_value_get(self, NULL);
53005309
if (val == NULL)
53015310
return NULL;
53025311

Modules/_ctypes/clinic/_ctypes.c.h

Lines changed: 51 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0