Skip to content

Commit a6cfb62

Browse files
committed
address review: negative defaults, to_bytes signed, get_self, fstat
- python_default_repr renders negated int/float literals and looks through parentheses and groups. - int.to_bytes signed is ArgIntoBool again, so any object is accepted by truthiness; its signature default stays False via py_default. - PyNativeFunction::get_self falls back to the module object. - os.fstat has its own FromArgs with a single fd parameter instead of reusing stat's arguments. Assisted-by: Claude:claude-opus-5-5
1 parent 155cea2 commit a6cfb62

4 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎crates/derive-impl/src/from_args.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,23 @@ fn python_default_repr(default: Option<&DefaultValue>, py_default: Option<&str>)
266266
Expr::Path(path) if path.qself.is_none() && path.path.is_ident("None") => {
267267
Some("None".to_owned())
268268
}
269+
Expr::Unary(syn::ExprUnary {
270+
op: syn::UnOp::Neg(_),
271+
expr: inner,
272+
..
273+
}) => match inner.as_ref() {
274+
Expr::Lit(syn::ExprLit {
275+
lit: Lit::Int(i), ..
276+
}) => Some(format!("-{}", i.base10_digits())),
277+
Expr::Lit(syn::ExprLit {
278+
lit: Lit::Float(f), ..
279+
}) => Some(format!("-{}", f.base10_digits())),
280+
_ => Some("<unrepresentable>".to_owned()),
281+
},
282+
Expr::Paren(syn::ExprParen { expr: inner, .. })
283+
| Expr::Group(syn::ExprGroup { expr: inner, .. }) => {
284+
python_default_repr(Some(&Some(inner.as_ref().clone())), None)
285+
}
269286
_ => Some("<unrepresentable>".to_owned()),
270287
},
271288
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl PyNativeFunction {
6868
if self.value.flags.contains(PyMethodFlags::STATIC) {
6969
return None;
7070
}
71-
self.zelf.as_deref()
71+
self.zelf.as_deref().or(self.module_object.as_deref())
7272
}
7373

7474
pub const fn as_func(&self) -> &'static dyn PyNativeFn {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl PyInt {
589589

590590
#[pymethod]
591591
fn to_bytes(&self, args: IntToByteArgs, vm: &VirtualMachine) -> PyResult<PyBytes> {
592-
let signed = args.signed;
592+
let signed: bool = args.signed.into();
593593
let byte_len = args.length;
594594

595595
let value = self.as_bigint();
@@ -823,8 +823,9 @@ struct IntToByteArgs {
823823
// ArgByteOrder::Big is not the text 'big'.
824824
#[pyarg(any, default = ArgByteOrder::Big, py_default = "'big'")]
825825
byteorder: ArgByteOrder,
826-
#[pyarg(named, default = false)]
827-
signed: bool,
826+
// Any object is accepted by truthiness, so the Rust default is not a literal.
827+
#[pyarg(named, default = ArgIntoBool::FALSE, py_default = "False")]
828+
signed: ArgIntoBool,
828829
}
829830

830831
fn try_int_radix(obj: &PyObject, base: u32, vm: &VirtualMachine) -> PyResult<BigInt> {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,6 @@ pub(super) mod _os {
14901490
}
14911491

14921492
#[pyfunction]
1493-
#[pyfunction(name = "fstat")]
14941493
fn stat(args: StatArgs<'_>, vm: &VirtualMachine) -> PyResult {
14951494
let StatArgs {
14961495
path,
@@ -1500,6 +1499,22 @@ pub(super) mod _os {
15001499
stat_at(path, dir_fd, follow_symlinks, vm)
15011500
}
15021501

1502+
#[derive(FromArgs)]
1503+
struct FstatArgs<'a> {
1504+
#[pyarg(any)]
1505+
fd: crt_fd::Borrowed<'a>,
1506+
}
1507+
1508+
#[pyfunction]
1509+
fn fstat(args: FstatArgs<'_>, vm: &VirtualMachine) -> PyResult {
1510+
stat_at(
1511+
OsPathOrFd::Fd(args.fd),
1512+
DirFd::default(),
1513+
FollowSymlinks(true),
1514+
vm,
1515+
)
1516+
}
1517+
15031518
#[pyfunction]
15041519
fn lstat(
15051520
path: OsPath,

0 commit comments

Comments
 (0)