Skip to content

Commit eae2dfb

Browse files
committed
gh-114258: Use Argument Clinic for __bool__
1 parent 638d638 commit eae2dfb

6 files changed

Lines changed: 50 additions & 30 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,10 +858,7 @@ class Foo "FooObject *" "&Foo_Type"
858858
def test_slot_with_parameters_and_return_type(self):
859859
# Slots which take arguments or return something else than an
860860
# object are not supported yet; check the machinery with them.
861-
slots = {
862-
'__contains__': SlotSpec(return_converter='int'),
863-
'__bool__': SlotSpec(return_converter='int'),
864-
}
861+
slots = {'__contains__': SlotSpec(return_converter='int')}
865862
block = """
866863
/*[clinic input]
867864
output everything block

‎Modules/_dbmmodule.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,16 @@ dbm_length_impl(dbmobject *self)
145145
return dp->di_size;
146146
}
147147

148+
/*[clinic input]
149+
@critical_section
150+
_dbm.dbm.__bool__ as dbm_bool
151+
[clinic start generated code]*/
152+
148153
static int
149-
dbm_bool_lock_held(PyObject *self)
154+
dbm_bool_impl(dbmobject *self)
155+
/*[clinic end generated code: output=b550ce1ae52d6ad1 input=212af765b6e60719]*/
150156
{
151-
dbmobject *dp = dbmobject_CAST(self);
157+
dbmobject *dp = self;
152158
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
153159
assert(state != NULL);
154160

@@ -177,16 +183,6 @@ dbm_bool_lock_held(PyObject *self)
177183
return 1;
178184
}
179185

180-
static int
181-
dbm_bool(PyObject *self)
182-
{
183-
int result;
184-
Py_BEGIN_CRITICAL_SECTION(self);
185-
result = dbm_bool_lock_held(self);
186-
Py_END_CRITICAL_SECTION();
187-
return result;
188-
}
189-
190186
static PyObject *
191187
dbm_subscript_lock_held(PyObject *self, PyObject *key)
192188
{

‎Modules/_gdbmmodule.c‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,16 @@ gdbm_length_impl(gdbmobject *self)
189189
return dp->di_size;
190190
}
191191

192+
/*[clinic input]
193+
@critical_section
194+
_gdbm.gdbm.__bool__ as gdbm_bool
195+
[clinic start generated code]*/
196+
192197
static int
193-
gdbm_bool_lock_held(PyObject *op)
198+
gdbm_bool_impl(gdbmobject *self)
199+
/*[clinic end generated code: output=91316ca18acb945c input=bf2878ff5e311848]*/
194200
{
195-
gdbmobject *dp = _gdbmobject_CAST(op);
201+
gdbmobject *dp = self;
196202
_gdbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
197203
if (dp->di_dbm == NULL) {
198204
PyErr_SetString(state->gdbm_error, "GDBM object has already been closed");
@@ -219,16 +225,6 @@ gdbm_bool_lock_held(PyObject *op)
219225
return 1;
220226
}
221227

222-
static int
223-
gdbm_bool(PyObject *op)
224-
{
225-
int result;
226-
Py_BEGIN_CRITICAL_SECTION(op);
227-
result = gdbm_bool_lock_held(op);
228-
Py_END_CRITICAL_SECTION();
229-
return result;
230-
}
231-
232228
// Wrapper function for PyArg_Parse(o, "s#", &d.dptr, &d.size).
233229
// This function is needed to support PY_SSIZE_T_CLEAN.
234230
// Return 1 on success, same to PyArg_Parse().

‎Modules/clinic/_dbmmodule.c.h‎

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

‎Modules/clinic/_gdbmmodule.c.h‎

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

‎Tools/clinic/libclinic/slots.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ def make_return_converter(self) -> CReturnConverter:
4040
SLOTS: Final[dict[str, SlotSpec]] = {
4141
'__next__': SlotSpec(return_converter='object'),
4242
'__len__': SlotSpec(return_converter='Py_ssize_t'),
43+
'__bool__': SlotSpec(return_converter='int'),
4344
}

0 commit comments

Comments
 (0)