Skip to content

Commit 7103208

Browse files
committed
Keep bytearray protocol default on its native omission path
The earlier review-driven literal default in 27db316 creates a Python integer even when proto is omitted. Under the clarified runtime-cost requirement, restore OptionalArg and its direct native zero fallback, keeping supplied ArgIndex conversion and overflow order unchanged. Retain the existing metadata test with the specifically approved Raw("0") expectation. The take_bytes Option None cleanup and other builtin signature fixes remain unchanged. AI-assisted cost audit and implementation by OpenAI Codex. Assisted-by: OpenAI Codex:model version not exposed
1 parent 5642ff8 commit 7103208

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121
},
2222
convert::{ToPyObject, ToPyResult},
2323
function::{
24-
ArgBytesLike, ArgIndex, ArgumentError, FromArgs, FuncArgs, Param, PyComparisonValue,
25-
PySsize,
24+
ArgBytesLike, ArgIndex, ArgumentError, FromArgs, FuncArgs, OptionalArg, Param,
25+
PyComparisonValue, PySsize,
2626
},
2727
protocol::{
2828
BufferDescriptor, BufferFlags, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
@@ -496,16 +496,16 @@ impl PyByteArray {}
496496

497497
#[derive(FromArgs)]
498498
struct ByteArrayReduceExArgs {
499-
#[pyarg(positional, default = 0)]
500-
proto: ArgIndex,
499+
#[pyarg(positional, optional, py_default = "0")]
500+
proto: OptionalArg<ArgIndex>,
501501
}
502502

503503
#[cfg(test)]
504504
#[test]
505505
fn reduce_ex_default_metadata_comes_from_its_value() {
506506
use crate::function::{DefaultRepr, FromArgs};
507507
let params = ByteArrayReduceExArgs::PARAMS.expect("known reduce-ex parameters");
508-
assert!(matches!(params[0].default, Some(DefaultRepr::Int(0))));
508+
assert!(matches!(params[0].default, Some(DefaultRepr::Raw("0"))));
509509
}
510510

511511
#[derive(FromArgs)]
@@ -700,12 +700,14 @@ impl Py<PyByteArray> {
700700
args: ByteArrayReduceExArgs,
701701
vm: &VirtualMachine,
702702
) -> PyResult<(PyTypeRef, PyTupleRef, PyObjectRef)> {
703-
let proto = args
704-
.proto
705-
.as_ref()
706-
.as_bigint()
707-
.to_i32()
708-
.ok_or_else(|| vm.new_overflow_error("Python int too large to convert to C int"))?;
703+
let proto = match args.proto {
704+
OptionalArg::Present(proto) => {
705+
proto.as_ref().as_bigint().to_i32().ok_or_else(|| {
706+
vm.new_overflow_error("Python int too large to convert to C int")
707+
})?
708+
}
709+
OptionalArg::Missing => 0,
710+
};
709711
self.common_reduce(proto, vm)
710712
}
711713

0 commit comments

Comments
 (0)