Skip to content

gh-132753: Argument Clinic: Fix support of c_default for the bool converter #132754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,11 @@ def test_bool_converter(self):
self.assertEqual(ac_tester.bool_converter('', [], 5), (False, False, True))
self.assertEqual(ac_tester.bool_converter(('not empty',), {1: 2}, 0), (True, True, False))

def test_bool_converter_c_default(self):
self.assertEqual(ac_tester.bool_converter_c_default(), (1, 0, -2, -3))
self.assertEqual(ac_tester.bool_converter_c_default(False, True, False, True),
(0, 1, 0, 1))

def test_char_converter(self):
with self.assertRaises(TypeError):
ac_tester.char_converter(1)
Expand Down
20 changes: 20 additions & 0 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ bool_converter_impl(PyObject *module, int a, int b, int c)
}


/*[clinic input]
bool_converter_c_default

a: bool = True
b: bool = False
c: bool(c_default="-2") = True
d: bool(c_default="-3") = x
/

[clinic start generated code]*/

static PyObject *
bool_converter_c_default_impl(PyObject *module, int a, int b, int c, int d)
/*[clinic end generated code: output=cf204382e1e4c30c input=185786302ab84081]*/
{
return Py_BuildValue("iiii", a, b, c, d);
}


/*[clinic input]
char_converter

Expand Down Expand Up @@ -2296,6 +2315,7 @@ static PyMethodDef tester_methods[] = {
BYTE_ARRAY_OBJECT_CONVERTER_METHODDEF
UNICODE_CONVERTER_METHODDEF
BOOL_CONVERTER_METHODDEF
BOOL_CONVERTER_C_DEFAULT_METHODDEF
CHAR_CONVERTER_METHODDEF
UNSIGNED_CHAR_CONVERTER_METHODDEF
SHORT_CONVERTER_METHODDEF
Expand Down
60 changes: 59 additions & 1 deletion Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Tools/clinic/libclinic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def converter_init(self, *, accept: TypeSet = {object}) -> None:
fail(f"bool_converter: illegal 'accept' argument {accept!r}")
if self.default is not unspecified and self.default is not unknown:
self.default = bool(self.default)
self.c_default = str(int(self.default))
if self.c_default in {'Py_True', 'Py_False'}:
self.c_default = str(int(self.default))

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