Fix panic when using non-collection in bytes()

Fixes #1558
This commit is contained in:
HyeonGyu Lee (Vazrupe)
2019-10-23 18:23:30 +09:00
parent e4bded8ca3
commit a1464b7078
2 changed files with 6 additions and 5 deletions

View File

@@ -34,10 +34,11 @@ print(a)
b = bytes([1, 2, 3])
assert a == b
try:
with assert_raises(TypeError):
bytes([object()])
except TypeError:
pass
with assert_raises(TypeError):
bytes(1.0)
with assert_raises(ValueError):
bytes(-1)

View File

@@ -105,13 +105,13 @@ impl ByteInnerNewOptions {
obj => {
let elements = vm.extract_elements(&obj).or_else(|_| {
Err(vm.new_type_error(format!(
"cannot convert {} object to bytes",
"cannot convert '{}' object to bytes",
obj.class().name
)))
});
let mut data_bytes = vec![];
for elem in elements.unwrap() {
for elem in elements? {
let v = objint::to_int(vm, &elem, &BigInt::from(10))?;
if let Some(i) = v.to_u8() {
data_bytes.push(i);