(vm: &VirtualMachine, obj: &PyObject)
| 4 | use num_traits::ToPrimitive; |
| 5 | |
| 6 | pub fn bytes_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<Vec<u8>> { |
| 7 | if let Ok(elements) = obj.try_bytes_like(vm, |bytes| bytes.to_vec()) { |
| 8 | return Ok(elements); |
| 9 | } |
| 10 | |
| 11 | if !obj.fast_isinstance(vm.ctx.types.str_type) |
| 12 | && let Ok(elements) = vm.map_iterable_object(obj, |x| value_from_object(vm, &x)) |
| 13 | { |
| 14 | return elements; |
| 15 | } |
| 16 | |
| 17 | Err(vm.new_type_error("can assign only bytes, buffers, or iterables of ints in range(0, 256)")) |
| 18 | } |
| 19 | |
| 20 | pub fn value_from_object(vm: &VirtualMachine, obj: &PyObject) -> PyResult<u8> { |
| 21 | obj.try_index(vm)? |
no test coverage detected