Skip to content

Commit 3c29fb2

Browse files
[3.13] gh-132753: Argument Clinic: Fix support of c_default for the bool converter (GH-132754) (GH-132766)
(cherry picked from commit 78cfee6)
1 parent c709d3b commit 3c29fb2

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

Lib/test/test_clinic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,6 +3023,11 @@ def test_bool_converter(self):
30233023
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True))
30243024
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False))
30253025

3026+
def test_bool_converter_c_default(self):
3027+
self.assertEqual(ac_tester.bool_converter_c_default(), (1, 0, -2, -3))
3028+
self.assertEqual(ac_tester.bool_converter_c_default(False, True, False, True),
3029+
(0, 1, 0, 1))
3030+
30263031
def test_char_converter(self):
30273032
with self.assertRaises(TypeError):
30283033
ac_tester.char_converter(1)

Modules/_testclinic.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ bool_converter_impl(PyObject *module, int a, int b, int c)
209209
}
210210

211211

212+
/*[clinic input]
213+
bool_converter_c_default
214+
215+
a: bool = True
216+
b: bool = False
217+
c: bool(c_default="-2") = True
218+
d: bool(c_default="-3") = x
219+
/
220+
221+
[clinic start generated code]*/
222+
223+
static PyObject *
224+
bool_converter_c_default_impl(PyObject *module, int a, int b, int c, int d)
225+
/*[clinic end generated code: output=cf204382e1e4c30c input=185786302ab84081]*/
226+
{
227+
return Py_BuildValue("iiii", a, b, c, d);
228+
}
229+
230+
212231
/*[clinic input]
213232
char_converter
214233
@@ -1888,6 +1907,7 @@ static PyMethodDef tester_methods[] = {
18881907
BYTE_ARRAY_OBJECT_CONVERTER_METHODDEF
18891908
UNICODE_CONVERTER_METHODDEF
18901909
BOOL_CONVERTER_METHODDEF
1910+
BOOL_CONVERTER_C_DEFAULT_METHODDEF
18911911
CHAR_CONVERTER_METHODDEF
18921912
UNSIGNED_CHAR_CONVERTER_METHODDEF
18931913
SHORT_CONVERTER_METHODDEF

Modules/clinic/_testclinic.c.h

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

Tools/clinic/libclinic/converters.py

Lines changed: 2 additions & 1 deletion
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)