From 4efcf147dfc8fe3a58f403d280cdc72adcbd5f01 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 4 Apr 2025 19:00:43 +0530 Subject: [PATCH 1/2] add critical section to _ctypes.Simple --- Modules/_ctypes/_ctypes.c | 47 ++++++++++++++++----------- Modules/_ctypes/clinic/_ctypes.c.h | 52 +++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index efefc0157237bd..592d8f82220375 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5174,15 +5174,21 @@ PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length) */ /*[clinic input] -class _ctypes.Simple "PyObject *" "clinic_state()->Simple_Type" +class _ctypes.Simple "CDataObject *" "clinic_state()->Simple_Type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0493451fecf8cd4]*/ + +/*[clinic input] +@critical_section +@setter +_ctypes.Simple.value [clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=016c476c7aa8b8a8]*/ static int -Simple_set_value(PyObject *op, PyObject *value, void *Py_UNUSED(ignored)) +_ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value) +/*[clinic end generated code: output=f267186118939863 input=977af9dc9e71e857]*/ { PyObject *result; - CDataObject *self = _CDataObject_CAST(op); if (value == NULL) { PyErr_SetString(PyExc_TypeError, @@ -5192,15 +5198,13 @@ Simple_set_value(PyObject *op, PyObject *value, void *Py_UNUSED(ignored)) ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); StgInfo *info; - if (PyStgInfo_FromObject(st, op, &info) < 0) { + if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { return -1; } assert(info); /* Cannot be NULL for CDataObject instances */ assert(info->setfunc); - LOCK_PTR(self); result = info->setfunc(self->b_ptr, value, info->size); - UNLOCK_PTR(self); if (!result) return -1; @@ -5208,6 +5212,7 @@ Simple_set_value(PyObject *op, PyObject *value, void *Py_UNUSED(ignored)) return KeepRef(self, 0, result); } + static int Simple_init(PyObject *self, PyObject *args, PyObject *kw) { @@ -5215,31 +5220,35 @@ Simple_init(PyObject *self, PyObject *args, PyObject *kw) if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value)) return -1; if (value) - return Simple_set_value(self, value, NULL); + return _ctypes_Simple_value_set(self, value, NULL); return 0; } + +/*[clinic input] +@critical_section +@getter +_ctypes.Simple.value +[clinic start generated code]*/ + static PyObject * -Simple_get_value(PyObject *op, void *Py_UNUSED(ignored)) +_ctypes_Simple_value_get_impl(CDataObject *self) +/*[clinic end generated code: output=ce5a26570830a243 input=3ed3f735cec89282]*/ { - CDataObject *self = _CDataObject_CAST(op); ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); StgInfo *info; - if (PyStgInfo_FromObject(st, op, &info) < 0) { + if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { return NULL; } assert(info); /* Cannot be NULL for CDataObject instances */ assert(info->getfunc); PyObject *res; - LOCK_PTR(self); res = info->getfunc(self->b_ptr, self->b_size); - UNLOCK_PTR(self); return res; } static PyGetSetDef Simple_getsets[] = { - { "value", Simple_get_value, Simple_set_value, - "current value", NULL }, + _CTYPES_SIMPLE_VALUE_GETSETDEF { NULL, NULL } }; @@ -5260,7 +5269,7 @@ Simple_from_outparm_impl(PyObject *self, PyTypeObject *cls) return Py_NewRef(self); } /* call stginfo->getfunc */ - return Simple_get_value(self, NULL); + return _ctypes_Simple_value_get(self, NULL); } static PyMethodDef Simple_methods[] = { @@ -5273,9 +5282,9 @@ Simple_bool(PyObject *op) { int cmp; CDataObject *self = _CDataObject_CAST(op); - LOCK_PTR(self); + Py_BEGIN_CRITICAL_SECTION(op); cmp = memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size); - UNLOCK_PTR(self); + Py_END_CRITICAL_SECTION(); return cmp; } @@ -5291,7 +5300,7 @@ Simple_repr(PyObject *self) Py_TYPE(self)->tp_name, self); } - val = Simple_get_value(self, NULL); + val = _ctypes_Simple_value_get(self, NULL); if (val == NULL) return NULL; diff --git a/Modules/_ctypes/clinic/_ctypes.c.h b/Modules/_ctypes/clinic/_ctypes.c.h index 1f2e871137ed79..bccdbe2ebb0bc0 100644 --- a/Modules/_ctypes/clinic/_ctypes.c.h +++ b/Modules/_ctypes/clinic/_ctypes.c.h @@ -773,6 +773,56 @@ _ctypes_CFuncPtr_argtypes_get(PyObject *self, void *Py_UNUSED(context)) return return_value; } +#if !defined(_ctypes_Simple_value_DOCSTR) +# define _ctypes_Simple_value_DOCSTR NULL +#endif +#if defined(_CTYPES_SIMPLE_VALUE_GETSETDEF) +# undef _CTYPES_SIMPLE_VALUE_GETSETDEF +# define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", (getter)_ctypes_Simple_value_get, (setter)_ctypes_Simple_value_set, _ctypes_Simple_value_DOCSTR}, +#else +# define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", NULL, (setter)_ctypes_Simple_value_set, NULL}, +#endif + +static int +_ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value); + +static int +_ctypes_Simple_value_set(PyObject *self, PyObject *value, void *Py_UNUSED(context)) +{ + int return_value; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = _ctypes_Simple_value_set_impl((CDataObject *)self, value); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + +#if !defined(_ctypes_Simple_value_DOCSTR) +# define _ctypes_Simple_value_DOCSTR NULL +#endif +#if defined(_CTYPES_SIMPLE_VALUE_GETSETDEF) +# undef _CTYPES_SIMPLE_VALUE_GETSETDEF +# define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", (getter)_ctypes_Simple_value_get, (setter)_ctypes_Simple_value_set, _ctypes_Simple_value_DOCSTR}, +#else +# define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", (getter)_ctypes_Simple_value_get, NULL, _ctypes_Simple_value_DOCSTR}, +#endif + +static PyObject * +_ctypes_Simple_value_get_impl(CDataObject *self); + +static PyObject * +_ctypes_Simple_value_get(PyObject *self, void *Py_UNUSED(context)) +{ + PyObject *return_value = NULL; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = _ctypes_Simple_value_get_impl((CDataObject *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(Simple_from_outparm__doc__, "__ctypes_from_outparam__($self, /)\n" "--\n" @@ -793,4 +843,4 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py } return Simple_from_outparm_impl(self, cls); } -/*[clinic end generated code: output=a18d87239b6fb8ca input=a9049054013a1b77]*/ +/*[clinic end generated code: output=11c369afc64d1e66 input=a9049054013a1b77]*/ From 275e809e7b228bdfa6d3b798acc892035fb6b1e7 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 5 Apr 2025 15:05:43 +0530 Subject: [PATCH 2/2] remove from bool --- Modules/_ctypes/_ctypes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 592d8f82220375..772c36739847b3 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -5282,9 +5282,9 @@ Simple_bool(PyObject *op) { int cmp; CDataObject *self = _CDataObject_CAST(op); - Py_BEGIN_CRITICAL_SECTION(op); + LOCK_PTR(self); cmp = memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size); - Py_END_CRITICAL_SECTION(); + UNLOCK_PTR(self); return cmp; }