https://github.com/python/cpython/commit/9b9cdb644056572aeab531662427230bccfd77f8 commit: 9b9cdb644056572aeab531662427230bccfd77f8 branch: main author: Kumar Aditya <kumaradi...@python.org> committer: kumaraditya303 <kumaradi...@python.org> date: 2025-05-11T08:24:20Z summary:
gh-100926: use explicit stginfo lock for pointer cache (#133867) files: M Modules/_ctypes/_ctypes.c M Modules/_ctypes/clinic/_ctypes.c.h diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index a6606381e492e2..7de6bb396b084e 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -578,14 +578,13 @@ _ctypes_CType_Type___sizeof___impl(PyObject *self, PyTypeObject *cls) /*[clinic input] @getter -@critical_section _ctypes.CType_Type.__pointer_type__ [clinic start generated code]*/ static PyObject * _ctypes_CType_Type___pointer_type___get_impl(PyObject *self) -/*[clinic end generated code: output=718c9ff10b2b0012 input=ff7498aa6edf487c]*/ +/*[clinic end generated code: output=718c9ff10b2b0012 input=ad12dc835943ceb8]*/ { ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); StgInfo *info; @@ -596,9 +595,12 @@ _ctypes_CType_Type___pointer_type___get_impl(PyObject *self) PyErr_Format(PyExc_TypeError, "%R must have storage info", self); return NULL; } - - if (info->pointer_type) { - return Py_NewRef(info->pointer_type); + PyObject *pointer_type; + STGINFO_LOCK(info); + pointer_type = Py_XNewRef(info->pointer_type); + STGINFO_UNLOCK(); + if (pointer_type) { + return pointer_type; } PyErr_Format(PyExc_AttributeError, @@ -609,14 +611,13 @@ _ctypes_CType_Type___pointer_type___get_impl(PyObject *self) /*[clinic input] @setter -@critical_section _ctypes.CType_Type.__pointer_type__ [clinic start generated code]*/ static int _ctypes_CType_Type___pointer_type___set_impl(PyObject *self, PyObject *value) -/*[clinic end generated code: output=6259be8ea21693fa input=9b2dc2400c388982]*/ +/*[clinic end generated code: output=6259be8ea21693fa input=a05055fc7f4714b6]*/ { ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); StgInfo *info; @@ -627,8 +628,9 @@ _ctypes_CType_Type___pointer_type___set_impl(PyObject *self, PyObject *value) PyErr_Format(PyExc_TypeError, "%R must have storage info", self); return -1; } - + STGINFO_LOCK(info); Py_XSETREF(info->pointer_type, Py_XNewRef(value)); + STGINFO_UNLOCK(); return 0; } diff --git a/Modules/_ctypes/clinic/_ctypes.c.h b/Modules/_ctypes/clinic/_ctypes.c.h index d9a2ab19661c3b..cf2e3fa21072ae 100644 --- a/Modules/_ctypes/clinic/_ctypes.c.h +++ b/Modules/_ctypes/clinic/_ctypes.c.h @@ -47,13 +47,7 @@ _ctypes_CType_Type___pointer_type___get_impl(PyObject *self); static PyObject * _ctypes_CType_Type___pointer_type___get(PyObject *self, void *Py_UNUSED(context)) { - PyObject *return_value = NULL; - - Py_BEGIN_CRITICAL_SECTION(self); - return_value = _ctypes_CType_Type___pointer_type___get_impl(self); - Py_END_CRITICAL_SECTION(); - - return return_value; + return _ctypes_CType_Type___pointer_type___get_impl(self); } #if !defined(_ctypes_CType_Type___pointer_type___DOCSTR) @@ -74,9 +68,7 @@ _ctypes_CType_Type___pointer_type___set(PyObject *self, PyObject *value, void *P { int return_value; - Py_BEGIN_CRITICAL_SECTION(self); return_value = _ctypes_CType_Type___pointer_type___set_impl(self, value); - Py_END_CRITICAL_SECTION(); return return_value; } @@ -1050,4 +1042,4 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, PyObject *const *args, Py } return Simple_from_outparm_impl(self, cls); } -/*[clinic end generated code: output=f4bc2a77ec073b8a input=a9049054013a1b77]*/ +/*[clinic end generated code: output=536c9bcf4e05913e input=a9049054013a1b77]*/ _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com