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
8 changes: 3 additions & 5 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5175,6 +5175,7 @@ Test_an_metho_arg_named_arg_impl(TestObj *self, int arg)

/*[clinic input]
Test.__init__
self: self(type="TestObj *", always_reject_kwargs=True)
*args: tuple

Varargs init method.
Expand All @@ -5197,12 +5198,9 @@ static int
Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
{
int return_value = -1;
PyTypeObject *base_tp = TestType;
PyObject *__clinic_args = NULL;

if ((Py_IS_TYPE(self, base_tp) ||
Py_TYPE(self)->tp_new == base_tp->tp_new) &&
!_PyArg_NoKeywords("Test", kwargs)) {
if (!_PyArg_NoKeywords("Test", kwargs)) {
goto exit;
}
__clinic_args = Py_NewRef(args);
Expand All @@ -5217,7 +5215,7 @@ Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)

static int
Test___init___impl(TestObj *self, PyObject *args)
/*[clinic end generated code: output=0e5836c40dbc2397 input=a615a4485c0fc3e2]*/
/*[clinic end generated code: output=d912a2c5da6467d9 input=8e0d779137ce4f66]*/

/*[clinic input]
@vectorcall
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ def test_mangled_marker_line(self):
)
self.expect_failure(raw, err, filename="test.c", lineno=3)

def test_always_reject_kwargs_with_keyword_parameter(self):
raw = """
/*[clinic input]
class Foo "FooObject *" "Foo_Type"
Foo.__init__
self: self(always_reject_kwargs=True)
a: object
[clinic start generated code]*/
"""
err = "always_reject_kwargs is incompatible with keyword parameters"
self.expect_failure(raw, err)

def test_checksum_mismatch(self):
raw = """
/*[clinic input]
Expand Down
4 changes: 3 additions & 1 deletion Tools/clinic/libclinic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,10 @@ class self_converter(CConverter):
format_unit = ''
specified_type: str | None = None

def converter_init(self, *, type: str | None = None) -> None:
def converter_init(self, *, type: str | None = None,
always_reject_kwargs: bool = False) -> None:
self.specified_type = type
self.always_reject_kwargs = always_reject_kwargs

def pre_render(self) -> None:
f = self.function
Expand Down
14 changes: 11 additions & 3 deletions Tools/clinic/libclinic/parse_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -1484,14 +1484,18 @@ def _new_or_init_parser_body(self) -> None:
raise ValueError("Slot methods cannot access their defining class.")

if not parses_keywords:
self.declarations = '{base_type_ptr}'
always_reject = self.self_parameter_converter.always_reject_kwargs
# The no-positional check below still exempts subclasses.
if not (always_reject and parses_positional):
self.declarations = '{base_type_ptr}'
self.codegen.add_include('pycore_modsupport.h',
'_PyArg_NoKeywords()')
type_check = '' if always_reject else '{self_type_check}'
fields.insert(0, libclinic.normalize_snippet("""
if ({self_type_check}!_PyArg_NoKeywords("{name}", kwargs)) {{
if (%s!_PyArg_NoKeywords("{name}", kwargs)) {{
goto exit;
}}
""", indent=4))
""" % type_check, indent=4))
if not parses_positional:
self.codegen.add_include('pycore_modsupport.h',
'_PyArg_NoPositional()')
Expand Down Expand Up @@ -1812,6 +1816,10 @@ def parse_args(self, clang: CLanguage) -> dict[str, str]:
else:
self.parse_general(clang)

if (self.self_parameter_converter.always_reject_kwargs
and 'METH_KEYWORDS' in self.flags):
fail("always_reject_kwargs is incompatible with keyword parameters")

self.copy_includes()
if self.is_new_or_init():
self.handle_new_or_init()
Expand Down
Loading