Skip to content

Commit f50601c

Browse files
committed
Return the correct bytes reconstruction arguments
Assisted-by: Codex:GPT-6
1 parent e5554e2 commit f50601c

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,12 +560,6 @@ impl Py<PyBytes> {
560560
fn title(&self) -> PyBytes {
561561
self.inner.title().into()
562562
}
563-
564-
#[pymethod]
565-
fn __getnewargs__(&self, vm: &VirtualMachine) -> PyTupleRef {
566-
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
567-
PyTuple::new_ref(param, &vm.ctx)
568-
}
569563
}
570564

571565
#[pyclass]
@@ -579,6 +573,11 @@ impl PyRef<PyBytes> {
579573
}
580574
}
581575

576+
#[pymethod]
577+
fn __getnewargs__(self, vm: &VirtualMachine) -> PyTupleRef {
578+
vm.new_tuple((self.result_from_slice(self.as_bytes(), vm),))
579+
}
580+
582581
#[pymethod]
583582
fn strip(self, options: ByteInnerStripOptions, vm: &VirtualMachine) -> Self {
584583
self.result_from_slice(self.inner.strip(options.bytes), vm)

‎extra_tests/snippets/builtin_bytes.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,13 @@ def out_of_range(fn, message):
931931
out_of_range(lambda: bytearray([256]), "byte must be in range(0, 256)")
932932
out_of_range(lambda: bytearray(iter([256])), "byte must be in range(0, 256)")
933933
out_of_range(lambda: bytearray().extend([256]), "byte must be in range(0, 256)")
934+
935+
936+
class NewArgsBytes(bytes):
937+
pass
938+
939+
940+
for bytes_type in (bytes, NewArgsBytes):
941+
newargs = bytes_type(b"a\x00b").__getnewargs__()
942+
assert newargs == (b"a\x00b",)
943+
assert type(newargs[0]) is bytes

0 commit comments

Comments
 (0)