(args: ReconstructorArgs, vm: &VirtualMachine)
| 1603 | |
| 1604 | #[pyfunction] |
| 1605 | fn _array_reconstructor(args: ReconstructorArgs, vm: &VirtualMachine) -> PyResult<PyArrayRef> { |
| 1606 | let cls = check_array_type(args.arraytype, vm)?; |
| 1607 | let mut array = check_type_code(args.typecode, vm)?; |
| 1608 | let format = args.mformat_code; |
| 1609 | let bytes = args.items.as_bytes(); |
| 1610 | if !bytes.len().is_multiple_of(format.item_size()) { |
| 1611 | return Err(vm.new_value_error("bytes length not a multiple of item size")); |
| 1612 | } |
| 1613 | if MachineFormatCode::from_typecode(array.typecode()) == Some(format) { |
| 1614 | array.frombytes(bytes); |
| 1615 | return PyArray::from(array).into_ref_with_type(vm, cls); |
| 1616 | } |
| 1617 | if !matches!( |
| 1618 | format, |
| 1619 | MachineFormatCode::Utf16 { .. } | MachineFormatCode::Utf32 { .. } |
| 1620 | ) { |
| 1621 | array.reserve(bytes.len() / format.item_size()); |
| 1622 | } |
| 1623 | let mut chunks = bytes.chunks(format.item_size()); |
| 1624 | match format { |
| 1625 | MachineFormatCode::Ieee754Float { big_endian } => { |
| 1626 | chunks.try_for_each(|b| array.push(chunk_to_obj!(vm, b, f32, big_endian), vm))? |
| 1627 | } |
| 1628 | MachineFormatCode::Ieee754Double { big_endian } => { |
| 1629 | chunks.try_for_each(|b| array.push(chunk_to_obj!(vm, b, f64, big_endian), vm))? |
| 1630 | } |
| 1631 | MachineFormatCode::Int8 { signed } => chunks |
| 1632 | .try_for_each(|b| array.push(chunk_to_obj!(vm, b, i8, u8, signed, false), vm))?, |
| 1633 | MachineFormatCode::Int16 { signed, big_endian } => chunks.try_for_each(|b| { |
| 1634 | array.push(chunk_to_obj!(vm, b, i16, u16, signed, big_endian), vm) |
| 1635 | })?, |
| 1636 | MachineFormatCode::Int32 { signed, big_endian } => chunks.try_for_each(|b| { |
| 1637 | array.push(chunk_to_obj!(vm, b, i32, u32, signed, big_endian), vm) |
| 1638 | })?, |
| 1639 | MachineFormatCode::Int64 { signed, big_endian } => chunks.try_for_each(|b| { |
| 1640 | array.push(chunk_to_obj!(vm, b, i64, u64, signed, big_endian), vm) |
| 1641 | })?, |
| 1642 | MachineFormatCode::Utf16 { big_endian } => { |
| 1643 | let utf16: Vec<_> = chunks.map(|b| chunk_to_obj!(b, u16, big_endian)).collect(); |
| 1644 | let s = String::from_utf16(&utf16) |
| 1645 | .map_err(|_| vm.new_unicode_encode_error("items cannot decode as utf16"))?; |
| 1646 | let bytes = PyArray::_unicode_to_wchar_bytes((*s).as_ref(), array.itemsize()); |
| 1647 | array.frombytes_move(bytes); |
| 1648 | } |
| 1649 | MachineFormatCode::Utf32 { big_endian } => { |
| 1650 | let s: Wtf8Buf = chunks |
| 1651 | .map(|b| chunk_to_obj!(b, u32, big_endian)) |
| 1652 | .map(|ch| u32_to_char(ch).map_err(|msg| vm.new_value_error(msg))) |
| 1653 | .try_collect()?; |
| 1654 | let bytes = PyArray::_unicode_to_wchar_bytes(&s, array.itemsize()); |
| 1655 | array.frombytes_move(bytes); |
| 1656 | } |
| 1657 | }; |
| 1658 | PyArray::from(array).into_ref_with_type(vm, cls) |
| 1659 | } |
| 1660 | |
| 1661 | // Register array.array as collections.abc.MutableSequence |
| 1662 | pub(crate) fn module_exec( |
nothing calls this directly
no test coverage detected