From eae2dfb5789655c8b4f020a280e6dc64fa7cb836 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 21 Oct 2026 10:00:52 -0700 Subject: [PATCH] gh-114258: Use Argument Clinic for ``__bool__`` --- Lib/test/test_clinic.py | 5 +---- Modules/_dbmmodule.c | 20 ++++++++------------ Modules/_gdbmmodule.c | 20 ++++++++------------ Modules/clinic/_dbmmodule.c.h | 17 ++++++++++++++++- Modules/clinic/_gdbmmodule.c.h | 17 ++++++++++++++++- Tools/clinic/libclinic/slots.py | 1 + 6 files changed, 50 insertions(+), 30 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e3dd4a2be9e410..da6982662b16f9 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -858,10 +858,7 @@ class Foo "FooObject *" "&Foo_Type" def test_slot_with_parameters_and_return_type(self): # Slots which take arguments or return something else than an # object are not supported yet; check the machinery with them. - slots = { - '__contains__': SlotSpec(return_converter='int'), - '__bool__': SlotSpec(return_converter='int'), - } + slots = {'__contains__': SlotSpec(return_converter='int')} block = """ /*[clinic input] output everything block diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 3186ce92749b1b..b4d7e75888c632 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -145,10 +145,16 @@ dbm_length_impl(dbmobject *self) return dp->di_size; } +/*[clinic input] +@critical_section +_dbm.dbm.__bool__ as dbm_bool +[clinic start generated code]*/ + static int -dbm_bool_lock_held(PyObject *self) +dbm_bool_impl(dbmobject *self) +/*[clinic end generated code: output=b550ce1ae52d6ad1 input=212af765b6e60719]*/ { - dbmobject *dp = dbmobject_CAST(self); + dbmobject *dp = self; _dbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); assert(state != NULL); @@ -177,16 +183,6 @@ dbm_bool_lock_held(PyObject *self) return 1; } -static int -dbm_bool(PyObject *self) -{ - int result; - Py_BEGIN_CRITICAL_SECTION(self); - result = dbm_bool_lock_held(self); - Py_END_CRITICAL_SECTION(); - return result; -} - static PyObject * dbm_subscript_lock_held(PyObject *self, PyObject *key) { diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 0a15012e6eb01a..fafb16b4cc1b46 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -189,10 +189,16 @@ gdbm_length_impl(gdbmobject *self) return dp->di_size; } +/*[clinic input] +@critical_section +_gdbm.gdbm.__bool__ as gdbm_bool +[clinic start generated code]*/ + static int -gdbm_bool_lock_held(PyObject *op) +gdbm_bool_impl(gdbmobject *self) +/*[clinic end generated code: output=91316ca18acb945c input=bf2878ff5e311848]*/ { - gdbmobject *dp = _gdbmobject_CAST(op); + gdbmobject *dp = self; _gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp)); if (dp->di_dbm == NULL) { PyErr_SetString(state->gdbm_error, "GDBM object has already been closed"); @@ -219,16 +225,6 @@ gdbm_bool_lock_held(PyObject *op) return 1; } -static int -gdbm_bool(PyObject *op) -{ - int result; - Py_BEGIN_CRITICAL_SECTION(op); - result = gdbm_bool_lock_held(op); - Py_END_CRITICAL_SECTION(); - return result; -} - // Wrapper function for PyArg_Parse(o, "s#", &d.dptr, &d.size). // This function is needed to support PY_SSIZE_T_CLEAN. // Return 1 on success, same to PyArg_Parse(). diff --git a/Modules/clinic/_dbmmodule.c.h b/Modules/clinic/_dbmmodule.c.h index 4e1a9eaa741e4e..9271097d994bdd 100644 --- a/Modules/clinic/_dbmmodule.c.h +++ b/Modules/clinic/_dbmmodule.c.h @@ -21,6 +21,21 @@ dbm_length(PyObject *self) return return_value; } +static int +dbm_bool_impl(dbmobject *self); + +static int +dbm_bool(PyObject *self) +{ + int return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = dbm_bool_impl((dbmobject *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_dbm_dbm_close__doc__, "close($self, /)\n" "--\n" @@ -264,4 +279,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=7ae22ba4a7aeffa2 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=44c45991f38d3d96 input=a9049054013a1b77]*/ diff --git a/Modules/clinic/_gdbmmodule.c.h b/Modules/clinic/_gdbmmodule.c.h index 55e2100751f090..f212f0bd3f04e3 100644 --- a/Modules/clinic/_gdbmmodule.c.h +++ b/Modules/clinic/_gdbmmodule.c.h @@ -21,6 +21,21 @@ gdbm_length(PyObject *self) return return_value; } +static int +gdbm_bool_impl(gdbmobject *self); + +static int +gdbm_bool(PyObject *self) +{ + int return_value = -1; + + Py_BEGIN_CRITICAL_SECTION(self); + return_value = gdbm_bool_impl((gdbmobject *)self); + Py_END_CRITICAL_SECTION(); + + return return_value; +} + PyDoc_STRVAR(_gdbm_gdbm_get__doc__, "get($self, key, default=None, /)\n" "--\n" @@ -416,4 +431,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) exit: return return_value; } -/*[clinic end generated code: output=36e1ed1cc3523aeb input=a9049054013a1b77]*/ +/*[clinic end generated code: output=554c7c952a77058f input=a9049054013a1b77]*/ diff --git a/Tools/clinic/libclinic/slots.py b/Tools/clinic/libclinic/slots.py index 95137800fd868c..8d90b39d1c4fb7 100644 --- a/Tools/clinic/libclinic/slots.py +++ b/Tools/clinic/libclinic/slots.py @@ -40,4 +40,5 @@ def make_return_converter(self) -> CReturnConverter: SLOTS: Final[dict[str, SlotSpec]] = { '__next__': SlotSpec(return_converter='object'), '__len__': SlotSpec(return_converter='Py_ssize_t'), + '__bool__': SlotSpec(return_converter='int'), }