Vectorcall implementation for PyFunction (PEP 590). Takes owned args to avoid cloning when filling fastlocals.
(
zelf_obj: &PyObject,
mut args: Vec<PyObjectRef>,
nargs: usize,
kwnames: Option<&[PyObjectRef]>,
vm: &VirtualMachine,
)
| 1384 | /// Vectorcall implementation for PyFunction (PEP 590). |
| 1385 | /// Takes owned args to avoid cloning when filling fastlocals. |
| 1386 | pub(crate) fn vectorcall_function( |
| 1387 | zelf_obj: &PyObject, |
| 1388 | mut args: Vec<PyObjectRef>, |
| 1389 | nargs: usize, |
| 1390 | kwnames: Option<&[PyObjectRef]>, |
| 1391 | vm: &VirtualMachine, |
| 1392 | ) -> PyResult { |
| 1393 | let zelf: &Py<PyFunction> = zelf_obj.downcast_ref().unwrap(); |
| 1394 | let code: &Py<PyCode> = &zelf.code; |
| 1395 | |
| 1396 | let has_kwargs = kwnames.is_some_and(|kw| !kw.is_empty()); |
| 1397 | let is_simple = !has_kwargs |
| 1398 | && code.flags.contains(bytecode::CodeFlags::OPTIMIZED) |
| 1399 | && !code.flags.contains(bytecode::CodeFlags::VARARGS) |
| 1400 | && !code.flags.contains(bytecode::CodeFlags::VARKEYWORDS) |
| 1401 | && code.kwonlyarg_count == 0 |
| 1402 | && !code |
| 1403 | .flags |
| 1404 | .intersects(bytecode::CodeFlags::GENERATOR | bytecode::CodeFlags::COROUTINE); |
| 1405 | |
| 1406 | if is_simple && nargs == code.arg_count as usize { |
| 1407 | // FAST PATH: simple positional-only call, exact arg count. |
| 1408 | // Move owned args directly into fastlocals — no clone needed. |
| 1409 | args.truncate(nargs); |
| 1410 | let frame = zelf.prepare_exact_args_frame(args, vm); |
| 1411 | |
| 1412 | let result = vm.run_frame(frame.clone()); |
| 1413 | unsafe { |
| 1414 | if let Some(base) = frame.materialize_localsplus() { |
| 1415 | vm.datastack_pop(base); |
| 1416 | } |
| 1417 | } |
| 1418 | return result; |
| 1419 | } |
| 1420 | |
| 1421 | // SLOW PATH: construct FuncArgs from owned Vec and delegate to invoke() |
| 1422 | let func_args = if has_kwargs { |
| 1423 | FuncArgs::from_vectorcall(&args, nargs, kwnames) |
| 1424 | } else { |
| 1425 | args.truncate(nargs); |
| 1426 | FuncArgs::from(args) |
| 1427 | }; |
| 1428 | zelf.invoke(func_args, vm) |
| 1429 | } |
| 1430 | |
| 1431 | /// Vectorcall implementation for PyBoundMethod (PEP 590). |
| 1432 | fn vectorcall_bound_method( |
no test coverage detected