https://github.com/python/cpython/commit/1a80f6d454f7c3ad493a4dc86bd2bbb17c44bc8a
commit: 1a80f6d454f7c3ad493a4dc86bd2bbb17c44bc8a
branch: main
author: Donghee Na <[email protected]>
committer: corona10 <[email protected]>
date: 2026-10-23T02:42:16Z
summary:

gh-114258: Update _ctypes and tokenize to use AC where it can be. (gh-160404)

files:
M Modules/_ctypes/_ctypes.c
M Modules/_ctypes/clinic/_ctypes.c.h
M Python/Python-tokenize.c
M Python/clinic/Python-tokenize.c.h

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index bbd744cdba8b38..a2e5cbab10944b 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -5513,15 +5513,16 @@ static PyMethodDef Simple_methods[] = {
     { NULL, NULL },
 };
 
+/*[clinic input]
+@critical_section
+_ctypes.Simple.__bool__ as Simple_bool
+[clinic start generated code]*/
+
 static int
-Simple_bool(PyObject *op)
+Simple_bool_impl(CDataObject *self)
+/*[clinic end generated code: output=c4466ece6a99fe8b input=326a1c8b24d8918e]*/
 {
-    int cmp;
-    CDataObject *self = _CDataObject_CAST(op);
-    Py_BEGIN_CRITICAL_SECTION(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();
-    return cmp;
+    return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 
self->b_size);
 }
 
 /* "%s(%s)" % (self.__class__.__name__, self.value) */
@@ -5988,14 +5989,16 @@ Pointer_subscript(PyObject *myself, PyObject *item)
     }
 }
 
+/*[clinic input]
+@critical_section
+_ctypes._Pointer.__bool__ as Pointer_bool
+[clinic start generated code]*/
+
 static int
-Pointer_bool(PyObject *self)
+Pointer_bool_impl(PyObject *self)
+/*[clinic end generated code: output=0139acaf4d395a69 input=672b8d35e7e0752d]*/
 {
-    int res;
-    Py_BEGIN_CRITICAL_SECTION(self);
-    res = *(void **)_CDataObject_CAST(self)->b_ptr != NULL;
-    Py_END_CRITICAL_SECTION();
-    return res;
+    return *(void **)_CDataObject_CAST(self)->b_ptr != NULL;
 }
 
 static PyType_Slot pycpointer_slots[] = {
diff --git a/Modules/_ctypes/clinic/_ctypes.c.h 
b/Modules/_ctypes/clinic/_ctypes.c.h
index adc1a6011b230b..7a3c4bba9fc209 100644
--- a/Modules/_ctypes/clinic/_ctypes.c.h
+++ b/Modules/_ctypes/clinic/_ctypes.c.h
@@ -865,6 +865,21 @@ Simple_from_outparm(PyObject *self, PyTypeObject *cls, 
PyObject *const *args, Py
     return Simple_from_outparm_impl(self, cls);
 }
 
+static int
+Simple_bool_impl(CDataObject *self);
+
+static int
+Simple_bool(PyObject *self)
+{
+    int return_value = -1;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = Simple_bool_impl((CDataObject *)self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
+
 static PyObject *
 Pointer_item_impl(PyObject *myself, Py_ssize_t index);
 
@@ -894,6 +909,21 @@ Pointer_ass_item(PyObject *myself, Py_ssize_t index, 
PyObject *value)
 
     return return_value;
 }
+
+static int
+Pointer_bool_impl(PyObject *self);
+
+static int
+Pointer_bool(PyObject *self)
+{
+    int return_value = -1;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = Pointer_bool_impl(self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
 #define _CTYPES_CTYPE_TYPE___POINTER_TYPE___GETSETDEF {"__pointer_type__", 
(getter)_ctypes_CType_Type___pointer_type___get, 
(setter)_ctypes_CType_Type___pointer_type___set, NULL},
 
 #define _CTYPES_PYCARRAYTYPE_TYPE_RAW_GETSETDEF {"raw", 
(getter)_ctypes_PyCArrayType_Type_raw_get, 
(setter)_ctypes_PyCArrayType_Type_raw_set, NULL},
@@ -908,4 +938,4 @@ Pointer_ass_item(PyObject *myself, Py_ssize_t index, 
PyObject *value)
 
 #define _CTYPES_SIMPLE_VALUE_GETSETDEF {"value", 
(getter)_ctypes_Simple_value_get, (setter)_ctypes_Simple_value_set, NULL},
 
-/*[clinic end generated code: output=8fc24edc31ab60b5 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=5d9dd4bbae13cfca input=a9049054013a1b77]*/
diff --git a/Python/Python-tokenize.c b/Python/Python-tokenize.c
index 731444e13a0275..951d4976c147c0 100644
--- a/Python/Python-tokenize.c
+++ b/Python/Python-tokenize.c
@@ -19,7 +19,6 @@ get_tokenize_state(PyObject *module) {
     get_tokenize_state(PyType_GetModuleByDef(type, &_tokenizemodule))
 
 #include "pycore_runtime.h"
-#include "clinic/Python-tokenize.c.h"
 
 /*[clinic input]
 module _tokenizer
@@ -39,6 +38,8 @@ typedef struct
     Py_ssize_t byte_col_offset_diff;
 } tokenizeriterobject;
 
+#include "clinic/Python-tokenize.c.h"
+
 /*[clinic input]
 @classmethod
 _tokenizer.tokenizeriter.__new__ as tokenizeriter_new
@@ -228,17 +229,21 @@ _get_col_offsets(tokenizeriterobject *it, const struct 
token *token,
     return 0;
 }
 
+/*[clinic input]
+@critical_section
+_tokenizer.tokenizeriter.__next__ as tokenizeriter_next
+[clinic start generated code]*/
+
 static PyObject *
-tokenizeriter_next(PyObject *op)
+tokenizeriter_next_impl(tokenizeriterobject *self)
+/*[clinic end generated code: output=745c9c646781ff45 input=66f70c965ba8ccfe]*/
 {
-    tokenizeriterobject *it = (tokenizeriterobject*)op;
+    tokenizeriterobject *it = self;
     PyObject* result = NULL;
 
-    Py_BEGIN_CRITICAL_SECTION(it);
-
     if (it->done) {
         PyErr_SetString(PyExc_StopIteration, "EOF");
-        goto unlock;
+        return NULL;
     }
 
     struct token token;
@@ -324,9 +329,6 @@ tokenizeriter_next(PyObject *op)
     if (type == ENDMARKER) {
         it->done = 1;
     }
-
-unlock:;
-    Py_END_CRITICAL_SECTION();
     return result;
 }
 
diff --git a/Python/clinic/Python-tokenize.c.h 
b/Python/clinic/Python-tokenize.c.h
index ebdb6dbc73a980..d0311a9374d1a5 100644
--- a/Python/clinic/Python-tokenize.c.h
+++ b/Python/clinic/Python-tokenize.c.h
@@ -6,6 +6,7 @@ preserve
 #  include "pycore_gc.h"          // PyGC_Head
 #  include "pycore_runtime.h"     // _Py_ID()
 #endif
+#include "pycore_critical_section.h"// Py_BEGIN_CRITICAL_SECTION()
 #include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
 #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL()
 
@@ -79,4 +80,19 @@ tokenizeriter_new(PyTypeObject *type, PyObject *args, 
PyObject *kwargs)
 exit:
     return return_value;
 }
-/*[clinic end generated code: output=f9091945b96e09b7 input=a9049054013a1b77]*/
+
+static PyObject *
+tokenizeriter_next_impl(tokenizeriterobject *self);
+
+static PyObject *
+tokenizeriter_next(PyObject *self)
+{
+    PyObject *return_value = NULL;
+
+    Py_BEGIN_CRITICAL_SECTION(self);
+    return_value = tokenizeriter_next_impl((tokenizeriterobject *)self);
+    Py_END_CRITICAL_SECTION();
+
+    return return_value;
+}
+/*[clinic end generated code: output=67cc510ef85272ba input=a9049054013a1b77]*/

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to