Skip to content
Open
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
12 changes: 12 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4295,6 +4295,18 @@ def test_kwds_with_pos_only(self):
self.assertEqual(ac_tester.kwds_with_pos_only(1, 2, y='y', z='z'), (1, 2, kwds))
self.assertEqual(ac_tester.kwds_with_pos_only(1, 2, **kwds), (1, 2, kwds))

def test_kwds_with_optional_pos_only(self):
with self.assertRaises(TypeError):
ac_tester.kwds_with_optional_pos_only()
with self.assertRaises(TypeError):
ac_tester.kwds_with_optional_pos_only(y='y')
self.assertEqual(ac_tester.kwds_with_optional_pos_only(1), (1, None, {}))
self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, 2), (1, 2, {}))
self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, y='y'),
(1, None, {'y': 'y'}))
self.assertEqual(ac_tester.kwds_with_optional_pos_only(1, 2, y='y'),
(1, 2, {'y': 'y'}))

def test_kwds_with_stararg(self):
self.assertEqual(ac_tester.kwds_with_stararg(), ((), {}))
self.assertEqual(ac_tester.kwds_with_stararg(1, 2), ((1, 2), {}))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix Argument Clinic generating code which reads an optional positional
argument which was not passed, if the function has a ``**kwds`` parameter.
18 changes: 18 additions & 0 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,23 @@ kwds_with_pos_only_impl(PyObject *module, PyObject *a, PyObject *b,
}


/*[clinic input]
kwds_with_optional_pos_only
a: object
b: object = None
/
**kwds: dict
[clinic start generated code]*/

static PyObject *
kwds_with_optional_pos_only_impl(PyObject *module, PyObject *a, PyObject *b,
PyObject *kwds)
/*[clinic end generated code: output=25a8458f5acc1a07 input=0b18b9e1670904ec]*/
{
return pack_arguments_newref(3, a, b, kwds);
}


/*[clinic input]
kwds_with_stararg
*args: tuple
Expand Down Expand Up @@ -2490,6 +2507,7 @@ static PyMethodDef tester_methods[] = {

LONE_KWDS_METHODDEF
KWDS_WITH_POS_ONLY_METHODDEF
KWDS_WITH_OPTIONAL_POS_ONLY_METHODDEF
KWDS_WITH_STARARG_METHODDEF
KWDS_WITH_POS_ONLY_AND_STARARG_METHODDEF

Expand Down
49 changes: 48 additions & 1 deletion Modules/clinic/_testclinic_kwds.c.h

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

10 changes: 10 additions & 0 deletions Tools/clinic/libclinic/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,24 @@ def parse_var_keyword(self) -> None:
}}}}
""", indent=4))

has_optional = False
for i, p in enumerate(self.parameters):
parse_arg = p.converter.parse_arg(
f'PyTuple_GET_ITEM(args, {i})',
p.get_displayname(i+1),
limited_capi=self.limited_capi,
)
assert parse_arg is not None
if has_optional or p.is_optional():
has_optional = True
parser_code.append(libclinic.normalize_snippet("""
if (%s < %d) {{
goto skip_optional;
}}
""", indent=4) % (nargs, i + 1))
parser_code.append(libclinic.normalize_snippet(parse_arg, indent=4))
if has_optional:
parser_code.append("skip_optional:")

if self.varpos:
parser_code.append(libclinic.normalize_snippet(self._parse_vararg(), indent=4))
Expand Down
Loading