Skip to content

Commit 6308be3

Browse files
Merge pull request RustPython#1559 from vazrupe/fix-bytes
Fix panic when using non-collection in bytes()
2 parents aed3aed + a1464b7 commit 6308be3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

tests/snippets/basic_types.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
b = bytes([1, 2, 3])
3535
assert a == b
3636

37-
try:
37+
with assert_raises(TypeError):
3838
bytes([object()])
39-
except TypeError:
40-
pass
39+
40+
with assert_raises(TypeError):
41+
bytes(1.0)
4142

4243
with assert_raises(ValueError):
4344
bytes(-1)

vm/src/obj/objbyteinner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ impl ByteInnerNewOptions {
105105
obj => {
106106
let elements = vm.extract_elements(&obj).or_else(|_| {
107107
Err(vm.new_type_error(format!(
108-
"cannot convert {} object to bytes",
108+
"cannot convert '{}' object to bytes",
109109
obj.class().name
110110
)))
111111
});
112112

113113
let mut data_bytes = vec![];
114-
for elem in elements.unwrap() {
114+
for elem in elements? {
115115
let v = objint::to_int(vm, &elem, &BigInt::from(10))?;
116116
if let Some(i) = v.to_u8() {
117117
data_bytes.push(i);

0 commit comments

Comments
 (0)