Skip to content

Commit 78cfee6

Browse files
gh-132753: Argument Clinic: Fix support of c_default for the bool converter (GH-132754)
1 parent dc4a707 commit 78cfee6

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

Lib/test/test_clinic.py

+5
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,11 @@ def test_bool_converter(self):
30323032
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True))
30333033
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False))
30343034

3035+
def test_bool_converter_c_default(self):
3036+
self.assertEqual(ac_tester.bool_converter_c_default(), (1, 0, -2, -3))
3037+
self.assertEqual(ac_tester.bool_converter_c_default(False, True, False, True),
3038+
(0, 1, 0, 1))
3039+
30353040
def test_char_converter(self):
30363041
with self.assertRaises(TypeError):
30373042
ac_tester.char_converter(1)

Modules/_testclinic.c

+20
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,25 @@ bool_converter_impl(PyObject *module, int a, int b, int c)
224224
}
225225

226226

227+
/*[clinic input]
228+
bool_converter_c_default
229+
230+
a: bool = True
231+
b: bool = False
232+
c: bool(c_default="-2") = True
233+
d: bool(c_default="-3") = x
234+
/
235+
236+
[clinic start generated code]*/
237+
238+
static PyObject *
239+
bool_converter_c_default_impl(PyObject *module, int a, int b, int c, int d)
240+
/*[clinic end generated code: output=cf204382e1e4c30c input=185786302ab84081]*/
241+
{
242+
return Py_BuildValue("iiii", a, b, c, d);
243+
}
244+
245+
227246
/*[clinic input]
228247
char_converter
229248
@@ -2296,6 +2315,7 @@ static PyMethodDef tester_methods[] = {
22962315
BYTE_ARRAY_OBJECT_CONVERTER_METHODDEF
22972316
UNICODE_CONVERTER_METHODDEF
22982317
BOOL_CONVERTER_METHODDEF
2318+
BOOL_CONVERTER_C_DEFAULT_METHODDEF
22992319
CHAR_CONVERTER_METHODDEF
23002320
UNSIGNED_CHAR_CONVERTER_METHODDEF
23012321
SHORT_CONVERTER_METHODDEF

Modules/clinic/_testclinic.c.h

+59-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/libclinic/converters.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def converter_init(self, *, accept: TypeSet = {object}) -> None:
3030
fail(f"bool_converter: illegal 'accept' argument {accept!r}")
3131
if self.default is not unspecified and self.default is not unknown:
3232
self.default = bool(self.default)
33-
self.c_default = str(int(self.default))
33+
if self.c_default in {'Py_True', 'Py_False'}:
34+
self.c_default = str(int(self.default))
3435

3536
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
3637
if self.format_unit == 'i':

0 commit comments

Comments
 (0)