From a8e4bde885d9f8620727b5dbc123aab00cb80ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EA=B4=91=ED=9A=A8?= <87641474+widehyo1@users.noreply.github.com> Date: Sat, 10 Oct 2026 00:49:44 +0900 Subject: [PATCH] Reject unsupported native keywords before argument binding (#8957) * Reject unsupported native keywords before argument binding Add explicit keyword dispatch metadata independently of inferred Rust calling flags. Reject nonempty keywords before binding and conversion, while preserving descriptor self checks and keyword-capable calls. Annotate 31 builtins and list.append/list.clear, derive C-API policies from ml_flags, and group dynamic registration metadata in MethodDefSpec. Add regression coverage and remove the id doctest expected-failure marker. Validation: workspace and C-API tests/clippy, VM without doc, keyword regressions, and test_call/test_extcall/test_builtin/test_list passed. The full snippet suite had 475 passes and 7 failures: six CPython-side expectation mismatches and one missing optional SQLite module. Assisted-by: Codex:GPT-6 * Infer native keyword dispatch from argument metadata Replace no_keywords annotations with registration-time inference from FromArgs::PARAMS, including nested parameter metadata. Forward arguments with keyword-capable parameters to the binder, preserving min/max body validation and descriptor self checks. Add regressions for raw arguments, keyword argument structs, and list.sort. Tested: C-API tests, workspace and C-API Clippy, VM without doc, both keyword regression snippets, and test_call/test_extcall/test_builtin/test_list. Workspace tests reported eight _opcode snapshot failures. The full snippet suite reported 477 passes and seven failures: six CPython-side failures and one RustPython failure due to the missing optional SQLite module. Assisted-by: Codex:GPT-6 * Simplify native function registration metadata Reuse SimpleItemMeta for pyfunction attributes and remove FunctionItemMeta. Remove MethodDefSpec and restore the original new_method_def arguments with an additional keyword_dispatch parameter, updating its callers. Consolidate keyword dispatch regressions in syntax_function_args.py, preserving their assertions and placing them before builtin shadowing. Validation: workspace and C-API tests/clippy, VM without doc, the combined snippet, and test_extcall/test_builtin/test_list/test_call passed. The full snippet suite reported 477 passes and seven existing failures: six CPython-side failures and one missing optional SQLite module. Assisted-by: Codex:GPT-6 * Reject native keywords by calling convention flags Remove `KeywordDispatch`, the `keyword_dispatch` field of `PyMethodDef`, `PyMethodDef::with_keyword_dispatch`, `function::keyword_dispatch` and the derive macros' `keyword_dispatch_tokens`. Native functions and method descriptors reject nonempty keywords when `flags` lacks `KEYWORDS`, and report the unqualified name when `flags` contains `VARARGS`. `Context::new_method_def` takes `(name, f, flags, doc)` again. Add a snippet for `int.__new__` called with a keyword argument. Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: Jeong YunWon --- Lib/test/test_extcall.py | 2 +- crates/capi/src/methodobject.rs | 22 +++++ crates/vm/src/builtins/builtin_func.rs | 33 ++++++++ crates/vm/src/builtins/descriptor.rs | 54 +++++++++++-- extra_tests/snippets/syntax_function_args.py | 84 ++++++++++++++++++++ 5 files changed, 189 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py index 483d5ad5f..c816d0135 100644 --- a/Lib/test/test_extcall.py +++ b/Lib/test/test_extcall.py @@ -442,7 +442,7 @@ TypeError if te dictionary is not empty ... False True - >>> id(1, **{'foo': 1}) # TODO: RUSTPYTHON # doctest:+EXPECTED_FAILURE + >>> id(1, **{'foo': 1}) Traceback (most recent call last): ... TypeError: id() takes no keyword arguments diff --git a/crates/capi/src/methodobject.rs b/crates/capi/src/methodobject.rs index 9868db36c..2e82c5e9d 100644 --- a/crates/capi/src/methodobject.rs +++ b/crates/capi/src/methodobject.rs @@ -385,4 +385,26 @@ mod tests { func.call0().unwrap(); }); } + + #[test] + fn no_keywords_before_arity() { + Python::attach(|py| { + unsafe extern "C" fn c_fn(_self: *mut PyObject, _args: *mut PyObject) -> *mut PyObject { + unsafe { PyLong_FromLong(42) } + } + let f = PyCFunction::new(py, c_fn, c"no_keywords", c"", None).unwrap(); + let kwargs = pyo3::types::PyDict::new(py); + kwargs.set_item("unexpected", 1).unwrap(); + let err = f.call((1,), Some(&kwargs)).unwrap_err(); + assert_eq!( + err.value(py).str().unwrap().to_str().unwrap(), + "no_keywords() takes no keyword arguments" + ); + kwargs.clear(); + assert_eq!( + f.call((), Some(&kwargs)).unwrap().extract::().unwrap(), + 42 + ); + }); + } } diff --git a/crates/vm/src/builtins/builtin_func.rs b/crates/vm/src/builtins/builtin_func.rs index 17e3e3424..e03999870 100644 --- a/crates/vm/src/builtins/builtin_func.rs +++ b/crates/vm/src/builtins/builtin_func.rs @@ -87,6 +87,9 @@ impl Callable for PyNativeFunction { type Args = FuncArgs; #[inline] fn call(zelf: &Py, mut args: FuncArgs, vm: &VirtualMachine) -> PyResult { + if !args.kwargs.is_empty() && !zelf.value.flags.contains(PyMethodFlags::KEYWORDS) { + return Err(native_no_keywords_error(zelf, vm)?); + } let mut callee = Callee::named(zelf.value.name); if let Some(z) = &zelf.zelf { // STATIC methods store the class in zelf for qualname/repr purposes, @@ -272,6 +275,12 @@ fn vectorcall_native_function( ) -> PyResult { let zelf: &Py = zelf_obj.downcast_ref().unwrap(); + if kwnames.is_some_and(|names| !names.is_empty()) + && !zelf.value.flags.contains(PyMethodFlags::KEYWORDS) + { + return Err(native_no_keywords_error(zelf, vm)?); + } + // Build FuncArgs with self already at position 0 (no insert(0) needed) let needs_self = zelf .zelf @@ -292,6 +301,30 @@ fn vectorcall_native_function( (zelf.value.func)(vm, func_args, callee) } +#[cold] +fn native_no_keywords_error( + zelf: &Py, + vm: &VirtualMachine, +) -> PyResult { + if zelf.value.flags.contains(PyMethodFlags::VARARGS) { + return Ok(vm.new_type_error(format!("{}() takes no keyword arguments", zelf.value.name))); + } + let qualname = PyNativeFunction::__qualname__(NativeFunctionOrMethod(zelf.to_owned()), vm)?; + let module = zelf + .module + .load_owned() + .filter(|module| !vm.is_none(module)); + let module = module.map(|module| module.str(vm)).transpose()?; + let name = if let Some(module) = module + && module.to_string() != "builtins" + { + format!("{module}.{qualname}") + } else { + qualname.to_string() + }; + Ok(vm.new_type_error(format!("{name}() takes no keyword arguments"))) +} + pub(crate) fn init(context: &'static Context) { PyNativeFunction::extend_class(context, context.types.builtin_function_or_method_type); context diff --git a/crates/vm/src/builtins/descriptor.rs b/crates/vm/src/builtins/descriptor.rs index 301448654..55c2275f5 100644 --- a/crates/vm/src/builtins/descriptor.rs +++ b/crates/vm/src/builtins/descriptor.rs @@ -112,8 +112,9 @@ impl Callable for PyMethodDescriptor { type Args = FuncArgs; #[inline] fn call(zelf: &Py, args: FuncArgs, vm: &VirtualMachine) -> PyResult { - if let Some(obj) = args.args.first() { - method_descr_typecheck(zelf, obj, vm)?; + method_descr_check_self(zelf, args.args.first().map(|obj| &**obj), vm)?; + if !args.kwargs.is_empty() && !zelf.method.flags.contains(PyMethodFlags::KEYWORDS) { + return Err(method_descr_no_keywords_error(zelf, vm)); } (zelf.method.func)( vm, @@ -1556,6 +1557,40 @@ fn method_descr_typecheck( ))) } +#[inline] +fn method_descr_check_self( + descr: &PyMethodDescriptor, + obj: Option<&PyObject>, + vm: &VirtualMachine, +) -> PyResult<()> { + let obj = obj.ok_or_else(|| method_descr_missing_self_error(descr, vm))?; + method_descr_typecheck(descr, obj, vm) +} + +#[cold] +fn method_descr_missing_self_error( + descr: &PyMethodDescriptor, + vm: &VirtualMachine, +) -> crate::exceptions::types::PyBaseExceptionRef { + vm.new_type_error(format!( + "unbound method {}.{}() needs an argument", + descr.common.typ.name(), + descr.common.name + )) +} + +#[cold] +fn method_descr_no_keywords_error( + descr: &PyMethodDescriptor, + vm: &VirtualMachine, +) -> crate::exceptions::types::PyBaseExceptionRef { + vm.new_type_error(format!( + "{}.{}() takes no keyword arguments", + descr.common.typ.name(), + descr.common.name + )) +} + /// Vectorcall for method_descriptor: calls native method directly fn vectorcall_method_descriptor( zelf_obj: &PyObject, @@ -1565,10 +1600,19 @@ fn vectorcall_method_descriptor( vm: &VirtualMachine, ) -> PyResult { let zelf: &Py = zelf_obj.downcast_ref().unwrap(); - if nargs > 0 - && let Some(obj) = args.first() + method_descr_check_self( + zelf, + if nargs > 0 { + args.first().map(|obj| &**obj) + } else { + None + }, + vm, + )?; + if kwnames.is_some_and(|names| !names.is_empty()) + && !zelf.method.flags.contains(PyMethodFlags::KEYWORDS) { - method_descr_typecheck(zelf, obj, vm)?; + return Err(method_descr_no_keywords_error(zelf, vm)); } let func_args = FuncArgs::from_vectorcall_owned(args, nargs, kwnames); (zelf.method.func)( diff --git a/extra_tests/snippets/syntax_function_args.py b/extra_tests/snippets/syntax_function_args.py index eb4f94b8a..96cbaf372 100644 --- a/extra_tests/snippets/syntax_function_args.py +++ b/extra_tests/snippets/syntax_function_args.py @@ -1,6 +1,90 @@ from testutils import assert_raises +def error(call, expected): + try: + call() + except TypeError as exc: + assert str(exc) == expected, (str(exc), expected) + else: + raise AssertionError("expected TypeError") + + +error(lambda: id(obj=1), "id() takes no keyword arguments") +error(lambda: id(**{"obj": 1}), "id() takes no keyword arguments") +error(lambda: id(1, 2, obj=1), "id() takes no keyword arguments") +error(lambda: list.append(x=1), "unbound method list.append() needs an argument") +error(lambda: list.append(**{"x": 1}), "unbound method list.append() needs an argument") +error( + lambda: list.append(1, x=1), + "descriptor 'append' for 'list' objects doesn't apply to a 'int' object", +) +error(lambda: list.append([], x=1), "list.append() takes no keyword arguments") +error(lambda: list.append([], **{"x": 1}), "list.append() takes no keyword arguments") +error(lambda: [].append(x=1), "list.append() takes no keyword arguments") +error(lambda: [].clear(1, x=1), "list.clear() takes no keyword arguments") + + +class Index: + def __index__(self): + raise AssertionError("conversion ran before keyword check") + + +error(lambda: bin(Index(), x=1), "bin() takes no keyword arguments") +assert id(1, **{}) == id(1) +assert round(1.25, ndigits=1) == 1.2 +assert pow(2, exp=3) == 8 +assert sorted([2, 1], reverse=True) == [2, 1] +assert sum([1, 2], start=4) == 7 +values = [] +list.append(values, 1) +values.append(2) +assert values == [1, 2] +list.clear(values) +assert values == [] + +# Keyword metadata must forward raw arguments without changing body validation. +for choose in (min, max): + assert choose([], default=42) == 42 + assert choose([1, 2], key=lambda value: -value) == (2 if choose is min else 1) + try: + choose(bogus=1) + except TypeError as exc: + assert str(exc) == f"{choose.__name__} expected at least 1 argument, got 0" + else: + raise AssertionError("missing positional argument was accepted") + try: + choose(1, 2, default=0) + except TypeError as exc: + assert str(exc) == ( + f"Cannot specify a default for {choose.__name__}() " + "with multiple positional arguments" + ) + else: + raise AssertionError("default with multiple arguments was accepted") + +# Named fields in argument structs must override positional-only arguments. +assert eval("value", globals={"value": 42}) == 42 +namespace = {} +exec("value = 42", globals=namespace) +assert namespace["value"] == 42 +assert compile(source="42", filename="", mode="eval") +assert __import__(name="sys").__name__ == "sys" + +from io import StringIO + +output = StringIO() +print(1, 2, sep=":", end="!", file=output) +assert output.getvalue() == "1:2!" + +# Methods using a keyword-only argument struct must keep accepting keywords. +values = [1, 2] +values.sort(reverse=True) +assert values == [2, 1] + +# Definitions built without the macros infer their calling convention too. +assert int.__new__(int, "11", base=2) == 3 + def sum(x, y): return x+y