Skip to content

Commit c58b713

Browse files
authored
gh-114258: Use Argument Clinic for __bool__ (GH-160057)
1 parent 3a3d39c commit c58b713

6 files changed

Lines changed: 53 additions & 33 deletions

File tree

‎Lib/test/test_clinic.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import re
1616
import sys
1717
import unittest
18-
from unittest import mock
1918
import warnings
2019

2120
test_tools.skip_if_missing('clinic')
@@ -35,7 +34,7 @@
3534
from libclinic.block_parser import Block, BlockParser
3635
from libclinic.codegen import BlockPrinter, Destination
3736
from libclinic.dsl_parser import DSLParser
38-
from libclinic.slots import SlotSpec, SLOTS
37+
from libclinic.slots import SLOTS
3938
from libclinic.cli import parse_file, Clinic
4039

4140

@@ -856,9 +855,8 @@ class Foo "FooObject *" "&Foo_Type"
856855
self.assertNotIn(text, generated)
857856

858857
def test_slot_with_parameters_and_return_type(self):
859-
# Slots which take arguments or return something else than an
860-
# object are not supported yet; check the machinery with them.
861-
slots = {'__bool__': SlotSpec(return_converter='int')}
858+
# Check the machinery with slots which take arguments or return
859+
# something else than an object.
862860
block = """
863861
/*[clinic input]
864862
output everything block
@@ -874,8 +872,7 @@ class Foo "FooObject *" "&Foo_Type"
874872
Foo.__bool__
875873
[clinic start generated code]*/
876874
"""
877-
with mock.patch.dict(SLOTS, slots):
878-
generated = self.clinic.parse(dedent(block))
875+
generated = self.clinic.parse(dedent(block))
879876
self.assertIn(dedent("""
880877
static int
881878
Foo_contains(PyObject *self, PyObject *value)

‎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
/*[clinic input]
191187
@critical_section
192188
_dbm.dbm.__getitem__ as dbm_subscript

‎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
@@ -39,6 +39,7 @@ def make_return_converter(self) -> CReturnConverter:
3939
# not be in dsl_parser.unsupported_special_methods.
4040
SLOTS: Final[dict[str, SlotSpec]] = {
4141
'__add__': SlotSpec(return_converter='object'),
42+
'__bool__': SlotSpec(return_converter='int'),
4243
'__contains__': SlotSpec(return_converter='int'),
4344
'__getitem__': SlotSpec(return_converter='object'),
4445
'__iadd__': SlotSpec(return_converter='object'),

0 commit comments

Comments
 (0)