Skip to content

Commit 269d548

Browse files
committed
Update builtin_signature snippet and fix mkdir on Windows
round and sum now report their FromArgs parameters, so the snippet asserts their signatures instead of their absence. sum's start is a PyObjectRef defaulting to int 0 with py_default "0". mkdir reads dir_fd only on platforms that use it. Assisted-by: Claude:claude-opus-5-5
1 parent 8ea9a44 commit 269d548

3 files changed

Lines changed: 15 additions & 30 deletions

File tree

‎crates/vm/src/stdlib/builtins.rs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,9 @@ mod builtins {
12011201
pub(super) struct SumArgs {
12021202
#[pyarg(positional)]
12031203
iterable: ArgIterable,
1204-
#[pyarg(any, optional)]
1205-
start: OptionalArg<PyObjectRef>,
1204+
// The int object needs the VM, so the default is not a literal.
1205+
#[pyarg(any, default = vm.ctx.new_int(0).into(), py_default = "0")]
1206+
start: PyObjectRef,
12061207
}
12071208

12081209
#[expect(
@@ -1211,10 +1212,7 @@ mod builtins {
12111212
)]
12121213
#[pyfunction]
12131214
fn sum(SumArgs { iterable, start }: SumArgs, vm: &VirtualMachine) -> PyResult {
1214-
// Start with zero and add at will:
1215-
let mut sum = start
1216-
.into_option()
1217-
.unwrap_or_else(|| vm.ctx.new_int(0).into());
1215+
let mut sum = start;
12181216

12191217
match_class!(match sum {
12201218
PyStr =>

‎crates/vm/src/stdlib/os.rs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,14 +414,13 @@ pub(super) mod _os {
414414

415415
#[pyfunction]
416416
fn mkdir(args: MkdirArgs<'_>, vm: &VirtualMachine) -> PyResult<()> {
417-
let MkdirArgs { path, mode, dir_fd } = args;
418417
#[cfg(any(unix, target_os = "wasi"))]
419-
let dir_fd = dir_fd.get_opt();
418+
let dir_fd = args.dir_fd.get_opt();
420419
#[cfg(not(any(unix, target_os = "wasi")))]
421420
let dir_fd = None;
422421

423-
crate::host_env::posix::make_dir(dir_fd, &path.path, mode)
424-
.map_err(|err| OSErrorBuilder::with_filename(&err, path, vm))
422+
crate::host_env::posix::make_dir(dir_fd, &args.path.path, args.mode)
423+
.map_err(|err| OSErrorBuilder::with_filename(&err, args.path, vm))
425424
}
426425

427426
#[pyfunction]

‎extra_tests/snippets/builtin_signature.py‎

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import inspect
22
import os
3-
import sys
43

54
# __text_signature__ is generated from the Rust parameter list, so it must not
65
# describe parameters the function does not actually take, and must mark the
76
# ones it does take as positional-only.
87

9-
# No phantom `module` parameter. RustPython's #[pyfunction]s take no module
10-
# argument, so `__self__` is None and inspect has nothing to strip.
8+
# No phantom `module` parameter. The signature marks it as `$module` and
9+
# `__self__` is the module, so inspect strips it.
1110
for f in (len, abs, hash, id, repr, bin, ord, divmod, hex, oct, chr, callable):
1211
assert "module" not in inspect.signature(f).parameters, f.__name__
1312

@@ -59,20 +58,9 @@
5958
assert str(inspect.signature([].__dir__)) == "()"
6059
assert str(inspect.signature(float.fromhex)) == "(string, /)"
6160

62-
if sys.implementation.name == "rustpython":
63-
# Functions whose Rust arguments are destructuring patterns rather than
64-
# plain names get no signature at all, instead of emitting text that is not
65-
# valid Python and makes inspect.signature() raise "invalid signature".
66-
#
67-
# CPython does have signatures for these, hand-written via Argument Clinic.
68-
# We cannot derive them until FromArgs reports the parameters of its own
69-
# structs, so until then we report no signature, which is at least how
70-
# CPython behaves for the builtins it has no signature for.
71-
for f in (round, sum):
72-
assert f.__text_signature__ is None, f.__name__
73-
try:
74-
inspect.signature(f)
75-
except ValueError as e:
76-
assert "no signature found" in str(e), str(e)
77-
else:
78-
raise AssertionError(f"{f.__name__} should have no signature")
61+
# Functions whose arguments come from a FromArgs struct report the struct's
62+
# parameters.
63+
assert round.__text_signature__ == "($module, /, number, ndigits=None)"
64+
assert sum.__text_signature__ == "($module, iterable, /, start=0)"
65+
assert str(inspect.signature(round)) == "(number, ndigits=None)"
66+
assert str(inspect.signature(sum)) == "(iterable, /, start=0)"

0 commit comments

Comments
 (0)