Skip to content

Commit f329f0b

Browse files
authored
Report bytes out-of-range elements as CPython does (#8679)
* Report `bytes` out-of-range elements as CPython does `collect_bytes` is shared by the `bytes` and `bytearray` entry points and hardcoded one element error, so `bytes([256])` said "byte must be in range(0, 256)" where CPython says "bytes". Every `bytearray` path does say "byte", so only the one caller was wrong. The element error is now a parameter, alongside the type error this function already varies per caller. Verified against CPython 3.14 for all seven paths. Assisted-by: Claude Code:claude-opus-5 * Keep the bytearray wording in PyByteArray_FromObject It called `bytes_from_object`, so the previous commit made it report "bytes must be in range(0, 256)" where CPython's `PyByteArray_FromObject` says "byte". It is the C-level bytearray constructor, so it now uses `bytearray_from_object`, which carries that wording and the constructor's unsized iteration. Assisted-by: Claude Code:claude-opus-5
1 parent 8195385 commit f329f0b

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

‎crates/capi/src/bytearrayobject.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::object::define_py_check;
33
use crate::pystate::with_vm;
44
use core::ffi::c_char;
55
use rustpython_vm::builtins::PyByteArray;
6-
use rustpython_vm::byte::bytes_from_object;
6+
use rustpython_vm::byte::bytearray_from_object;
77

88
define_py_check!(fn PyByteArray_Check, types.bytearray_type);
99

@@ -41,7 +41,7 @@ pub unsafe extern "C" fn PyByteArray_FromStringAndSize(
4141
pub unsafe extern "C" fn PyByteArray_FromObject(obj: *mut PyObject) -> *mut PyObject {
4242
with_vm(|vm| {
4343
let obj = unsafe { &*obj };
44-
let data = bytes_from_object(vm, obj)?;
44+
let data = bytearray_from_object(vm, obj)?;
4545
Ok(vm.ctx.new_bytearray(data))
4646
})
4747
}

‎crates/vm/src/byte.rs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ use crate::{
77
protocol::{BufferFlags, PyBuffer},
88
};
99

10+
/// The element error `bytes` reports, which is the one entry point here that does
11+
/// not name a single byte: `bytes([256])` says "bytes must be in range(0, 256)"
12+
/// where every `bytearray` path says "byte".
13+
const BYTES_ELEMENT_ERROR: &str = "bytes must be in range(0, 256)";
14+
const BYTE_ELEMENT_ERROR: &str = "byte must be in range(0, 256)";
15+
1016
// PyBytes_FromObject
1117
pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
12-
collect_bytes(vm, obj, true, |name| {
18+
collect_bytes(vm, obj, true, BYTES_ELEMENT_ERROR, |name| {
1319
format!("cannot convert '{name}' object to bytes")
1420
})
1521
}
@@ -18,25 +24,27 @@ pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8
1824
/// slice of one, which run the iterator without asking the object they were
1925
/// handed how long it is.
2026
pub fn bytearray_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
21-
collect_bytes(vm, obj, false, |name| {
27+
collect_bytes(vm, obj, false, BYTE_ELEMENT_ERROR, |name| {
2228
format!("cannot convert '{name}' object to bytearray")
2329
})
2430
}
2531

2632
/// [`bytes_from_object`] for `bytearray_extend()`, which names what it was
2733
/// doing rather than what it was converting to.
2834
pub fn bytearray_extend_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> {
29-
collect_bytes(vm, obj, true, |name| {
35+
collect_bytes(vm, obj, true, BYTE_ELEMENT_ERROR, |name| {
3036
format!("can't extend bytearray with {name}")
3137
})
3238
}
3339

34-
/// `measured` is whether the object is asked how long it is; `unusable` names,
35-
/// from the class name, what could not be done with one that is not iterable.
40+
/// `measured` is whether the object is asked how long it is; `element` is the error
41+
/// for a value outside `range(0, 256)`; `unusable` names, from the class name, what
42+
/// could not be done with one that is not iterable.
3643
fn collect_bytes(
3744
vm: &VirtualMachine,
3845
obj: &PyObject,
3946
measured: bool,
47+
element: &'static str,
4048
unusable: impl FnOnce(&str) -> String,
4149
) -> PyResult<Vec<u8>> {
4250
if obj.check_buffer() {
@@ -52,7 +60,7 @@ fn collect_bytes(
5260
if cls.slots.iter.load().is_none() && !cls.has_attr(identifier!(vm, __getitem__)) {
5361
return Err(vm.new_type_error(unusable(&cls.name())));
5462
}
55-
let value = |x: PyObjectRef| value_from_object(vm, &x);
63+
let value = |x: PyObjectRef| value_from_object_with(vm, &x, element);
5664
let elements = if measured {
5765
vm.map_iterable_object_sized(obj, value)
5866
} else {
@@ -67,8 +75,16 @@ fn collect_bytes(
6775
}
6876

6977
pub fn value_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> {
78+
value_from_object_with(vm, obj, BYTE_ELEMENT_ERROR)
79+
}
80+
81+
fn value_from_object_with(
82+
vm: &VirtualMachine,
83+
obj: &PyObject,
84+
element: &'static str,
85+
) -> PyResult<u8> {
7086
obj.try_index(vm)?
7187
.as_bigint()
7288
.to_u8()
73-
.ok_or_else(|| vm.new_value_error("byte must be in range(0, 256)"))
89+
.ok_or_else(|| vm.new_value_error(element))
7490
}

‎extra_tests/snippets/builtin_bytes.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,22 @@ def cannot(fn, message):
813813
lambda: bytearray(b"ab").__setitem__(slice(0, 2), "ab"),
814814
"can assign only bytes, buffers, or iterables of ints in range(0, 256)",
815815
)
816+
817+
818+
def out_of_range(fn, message):
819+
try:
820+
fn()
821+
except ValueError as e:
822+
assert str(e) == message, e
823+
else:
824+
raise AssertionError(f"expected ValueError: {message}")
825+
826+
827+
# `bytes` is the one entry point that does not name a single byte, and both the
828+
# sized and unsized iterator paths report it that way.
829+
out_of_range(lambda: bytes([256]), "bytes must be in range(0, 256)")
830+
out_of_range(lambda: bytes(iter([256])), "bytes must be in range(0, 256)")
831+
out_of_range(lambda: bytes([-1]), "bytes must be in range(0, 256)")
832+
out_of_range(lambda: bytearray([256]), "byte must be in range(0, 256)")
833+
out_of_range(lambda: bytearray(iter([256])), "byte must be in range(0, 256)")
834+
out_of_range(lambda: bytearray().extend([256]), "byte must be in range(0, 256)")

0 commit comments

Comments
 (0)