Files
RustPython/crates/vm/src/function/mod.rs
T
Jeong, YunWon 7cd4a54e1c Compose native text signatures at compile time from FromArgs (#8807)
* Compose native text signatures at compile time from FromArgs

FromArgs gains a PARAMS const describing its Python parameters.
The derive builds the whole internal doc, `name(sig)\n--\n\ndoc`,
in a const context from those params, and `__text_signature__` /
`__doc__` parse it from the doc. The macro-time string path and the
runtime sig_parts composition are removed.

Signatures follow clinic conventions: `$module` for module functions,
Rust literal defaults rendered as Python reprs, other defaults as
`<unrepresentable>`, and a new `#[pyarg(py_default = "...")]` for the
few fields whose Rust default differs from the Python one. The
`#[pyarg]` keys are documented on the FromArgs derive.

Module functions keep their module object and return it from
`__self__`. PyNativeFunction now traverses its references.

os.stat, os.mkdir, os.listdir, os.open, dict.fromkeys, round, sorted,
int.to_bytes and bytes.hex take arguments matching their signatures.

Assisted-by: Grok:grok-4.7
Assisted-by: Claude:claude-opus-5-5

* 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

* Remove expectedFailure from two signature tests

test_cmp_to_signature and test_autospec_on_bound_builtin_function
now pass. The TestCmpToKeyC override existed only for the marker.

Assisted-by: Claude:claude-opus-5-5
2026-09-24 20:53:28 +09:00

61 lines
1.9 KiB
Rust

mod argument;
mod arithmetic;
mod buffer;
mod builtin;
mod either;
mod fspath;
mod getargs;
mod getset;
pub(crate) mod method;
mod number;
mod protocol;
mod signature;
mod time;
pub use argument::{
ArgumentError, Callee, FromArgOptional, FromArgs, FuncArgs, IntoFuncArgs, KwArgs, KwArgsMap,
OptionalArg, OptionalOption, PosArgs,
};
pub(crate) use argument::{arity_message, unexpected_keyword_message};
pub use arithmetic::{PyArithmeticValue, PyComparisonValue};
pub use buffer::{
ArgAsciiBuffer, ArgBytesLike, ArgContiguousBytesLike, ArgMemoryBuffer, ArgStrOrBytesLike,
};
pub use builtin::{IntoPyNativeFn, PyNativeFn, static_func, static_raw_func};
pub use either::Either;
pub use fspath::FsPath;
pub(crate) use getargs::ArgSpec;
pub use getset::PySetterValue;
pub(super) use getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetterFunc, PySetterFunc};
pub use method::{HeapMethodDef, PyMethodDef, PyMethodFlags};
pub use number::{ArgIndex, ArgIntoBool, ArgIntoComplex, ArgIntoFloat, ArgPrimitiveIndex, ArgSize};
pub use protocol::{ArgCallable, ArgIterable, ArgMapping, ArgSequence};
pub use signature::{
Param, ParamKind, SigArg, has_signature, internal_doc_bytes, internal_doc_len,
};
pub use time::TimeoutSeconds;
use crate::{
Py, PyObject, PyResult, VirtualMachine, builtins::PyStr, convert::TryFromBorrowedObject,
};
use builtin::{BorrowedParam, OwnedParam, RefParam};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ArgByteOrder {
Big,
Little,
}
impl<'a> TryFromBorrowedObject<'a> for ArgByteOrder {
fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
obj.try_value_with(
|s: &Py<PyStr>| match s.as_bytes() {
b"big" => Ok(Self::Big),
b"little" => Ok(Self::Little),
_ => Err(vm.new_value_error("byteorder must be either 'little' or 'big'")),
},
vm,
)
}
}