(
cls: &Py<PyType>,
(ArrayNewArgs { spec, init }, kwargs): Self::Args,
vm: &VirtualMachine,
)
| 634 | type Args = (ArrayNewArgs, KwArgs); |
| 635 | |
| 636 | fn py_new( |
| 637 | cls: &Py<PyType>, |
| 638 | (ArrayNewArgs { spec, init }, kwargs): Self::Args, |
| 639 | vm: &VirtualMachine, |
| 640 | ) -> PyResult<Self> { |
| 641 | let spec = spec.as_str().chars().exactly_one().map_err(|_| { |
| 642 | vm.new_type_error("array() argument 1 must be a unicode character, not str") |
| 643 | })?; |
| 644 | |
| 645 | if cls.is(Self::class(&vm.ctx)) && !kwargs.is_empty() { |
| 646 | return Err(vm.new_type_error("array.array() takes no keyword arguments")); |
| 647 | } |
| 648 | |
| 649 | if spec == 'u' { |
| 650 | _warnings::warn( |
| 651 | vm.ctx.exceptions.deprecation_warning, |
| 652 | "The 'u' type code is deprecated and will be removed in Python 3.16".to_owned(), |
| 653 | 1, |
| 654 | vm, |
| 655 | )?; |
| 656 | } |
| 657 | |
| 658 | let mut array = |
| 659 | ArrayContentType::from_char(spec).map_err(|err| vm.new_value_error(err))?; |
| 660 | |
| 661 | if let OptionalArg::Present(init) = init { |
| 662 | if let Some(init) = init.downcast_ref::<Self>() { |
| 663 | match (spec, init.read().typecode()) { |
| 664 | (spec, ch) if spec == ch => array.frombytes(&init.get_bytes()), |
| 665 | (spec, 'u') => { |
| 666 | return Err(vm.new_type_error(format!( |
| 667 | "cannot use a unicode array to initialize an array with typecode '{spec}'" |
| 668 | ))) |
| 669 | } |
| 670 | _ => { |
| 671 | for obj in init.read().iter(vm) { |
| 672 | array.push(obj?, vm)?; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | } else if let Some(wtf8) = init.downcast_ref::<PyStr>() { |
| 677 | if spec == 'u' { |
| 678 | let bytes = Self::_unicode_to_wchar_bytes(wtf8.as_wtf8(), array.itemsize()); |
| 679 | array.frombytes_move(bytes); |
| 680 | } else { |
| 681 | return Err(vm.new_type_error(format!( |
| 682 | "cannot use a str to initialize an array with typecode '{spec}'" |
| 683 | ))); |
| 684 | } |
| 685 | } else if init.downcastable::<PyBytes>() || init.downcastable::<PyByteArray>() { |
| 686 | init.try_bytes_like(vm, |x| array.frombytes(x))?; |
| 687 | } else if let Ok(iter) = ArgIterable::try_from_object(vm, init.clone()) { |
| 688 | for obj in iter.iter(vm)? { |
| 689 | array.push(obj?, vm)?; |
| 690 | } |
| 691 | } else { |
| 692 | init.try_bytes_like(vm, |x| array.frombytes(x))?; |
| 693 | } |
nothing calls this directly
no test coverage detected