1pub(crate) use array::module_def;
4
5#[pymodule(name = "array")]
6pub mod array {
7 use crate::{
8 common::{
9 atomic::{self, AtomicUsize},
10 lock::{
11 PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock,
12 PyRwLockReadGuard, PyRwLockWriteGuard,
13 },
14 str::wchar_t,
15 },
16 vm::{
17 AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
18 atomic_func,
19 builtins::{
20 PositionIterInternal, PyByteArray, PyBytes, PyBytesRef, PyDictRef, PyFloat,
21 PyGenericAlias, PyInt, PyList, PyListRef, PyStr, PyStrRef, PyTupleRef, PyType,
22 PyTypeRef, PyUtf8Str, PyUtf8StrRef, builtins_iter, locked_next,
23 },
24 class_or_notimplemented,
25 convert::{ToPyObject, ToPyResult, TryFromBorrowedObject, TryFromObject},
26 function::{
27 ArgBytesLike, ArgIndex, ArgIntoFloat, ArgIterable, KwArgs, OptionalArg,
28 PyComparisonValue,
29 },
30 protocol::{
31 BufferDescriptor, BufferFlags, BufferMethods, BufferResizeGuard, PyBuffer,
32 PyIterReturn, PyMappingMethods, PySequenceMethods,
33 },
34 sequence::{SequenceExt, SequenceMutExt},
35 sliceable::{
36 SaturatedSlice, SequenceIndex, SequenceIndexOp, SliceableSequenceMutOp,
37 SliceableSequenceOp,
38 },
39 stdlib::_warnings,
40 types::{
41 AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext, Iterable,
42 PyComparisonOp, Representable, SelfIter,
43 },
44 },
45 };
46 use alloc::fmt;
47 use core::cmp::Ordering;
48 use itertools::Itertools;
49 use num_traits::{Signed, ToPrimitive};
50 use rustpython_common::wtf8::{CodePoint, Wtf8, Wtf8Buf};
51 use std::os::raw;
52 macro_rules! def_array_enum {
53 ($(($n:ident, $t:ty, $c:literal, $scode:literal)),*$(,)?) => {
54 #[derive(Debug, Clone)]
55 pub enum ArrayContentType {
56 $($n(Vec<$t>),)*
57 }
58
59 enum ArrayItem {
61 $($n($t),)*
62 }
63
64 impl ArrayContentType {
65 fn from_char(c: char) -> Result<Self, String> {
66 match c {
67 $($c => Ok(ArrayContentType::$n(Vec::new())),)*
68 _ => Err(
69 "bad typecode (must be b, B, u, w, h, H, i, I, l, L, q, Q, f or d)".into()
70 ),
71 }
72 }
73
74 const fn typecode(&self) -> char {
75 match self {
76 $(ArrayContentType::$n(_) => $c,)*
77 }
78 }
79
80 const fn typecode_str(&self) -> &'static str {
81 match self {
82 $(ArrayContentType::$n(_) => $scode,)*
83 }
84 }
85
86 const fn itemsize_of_typecode(c: char) -> Option<usize> {
87 match c {
88 $($c => Some(core::mem::size_of::<$t>()),)*
89 _ => None,
90 }
91 }
92
93 const fn itemsize(&self) -> usize {
94 match self {
95 $(ArrayContentType::$n(_) => core::mem::size_of::<$t>(),)*
96 }
97 }
98
99 fn addr(&self) -> usize {
100 match self {
101 $(ArrayContentType::$n(v) => v.as_ptr() as usize,)*
102 }
103 }
104
105 fn len(&self) -> usize {
106 match self {
107 $(ArrayContentType::$n(v) => v.len(),)*
108 }
109 }
110
111 fn reserve(&mut self, len: usize) {
112 match self {
113 $(ArrayContentType::$n(v) => v.reserve(len),)*
114 }
115 }
116
117 fn push(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
118 match self {
119 $(ArrayContentType::$n(v) => {
120 let val = <$t>::try_into_from_object(vm, obj)?;
121 v.push(val);
122 })*
123 }
124 Ok(())
125 }
126
127 fn pop(&mut self, i: isize, vm: &VirtualMachine) -> PyResult {
128 match self {
129 $(ArrayContentType::$n(v) => {
130 let i = v.wrap_index(i).ok_or_else(|| {
131 vm.new_index_error("pop index out of range".to_owned())
132 })?;
133 v.remove(i).to_pyresult(vm)
134 })*
135 }
136 }
137
138 fn push_item(&mut self, item: ArrayItem) {
139 match (self, item) {
140 $((Self::$n(v), ArrayItem::$n(value)) => v.push(value),)*
141 _ => unreachable!("item was converted for this array"),
142 }
143 }
144
145 fn insert_item(&mut self, i: isize, item: ArrayItem) {
146 match (self, item) {
147 $((Self::$n(v), ArrayItem::$n(value)) => {
148 v.insert(i.saturated_at(v.len()), value);
149 })*
150 _ => unreachable!("item was converted for this array"),
151 }
152 }
153
154 fn search_item(
155 typecode: char,
156 needle: &PyObject,
157 vm: &VirtualMachine,
158 ) -> Option<Option<ArrayItem>> {
159 match typecode {
160 $($c => <$t>::exact_search_item(needle, vm)
161 .map(|value| value.map(ArrayItem::$n)),)*
162 _ => unreachable!("array has a typecode"),
163 }
164 }
165
166 fn count_item(&self, item: &ArrayItem, vm: &VirtualMachine) -> PyResult<usize> {
167 match (self, item) {
168 $((Self::$n(v), ArrayItem::$n(value)) => {
169 <$t>::validate_slice(v, vm)?;
170 Ok(v.iter().filter(|element| *element == value).count())
171 })*
172 _ => unreachable!("item was converted for this array"),
173 }
174 }
175
176 fn find_item(
177 &self,
178 item: &ArrayItem,
179 start: usize,
180 stop: usize,
181 vm: &VirtualMachine,
182 ) -> PyResult<Option<usize>> {
183 match (self, item) {
184 $((Self::$n(v), ArrayItem::$n(value)) => {
185 let end = stop.min(v.len());
186 let start = start.min(end);
187 let items = &v[start..end];
188 let index = items.iter().position(|element| element == value);
189 let visited = index.map_or(items.len(), |index| index + 1);
191 <$t>::validate_slice(&items[..visited], vm)?;
192 Ok(index.map(|index| start + index))
193 })*
194 _ => unreachable!("item was converted for this array"),
195 }
196 }
197
198 fn clear(&mut self) -> PyResult<()>{
199 match self {
200 $(ArrayContentType::$n(v) => v.clear(),)*
201 };
202 Ok(())
203 }
204
205 fn frombytes_move(&mut self, b: Vec<u8>) {
206 match self {
207 $(ArrayContentType::$n(v) => {
208 if v.is_empty() {
209 let b = core::mem::ManuallyDrop::new(b);
212 let ptr = b.as_ptr() as *mut $t;
213 let len = b.len() / core::mem::size_of::<$t>();
214 let capacity = b.capacity() / core::mem::size_of::<$t>();
215 *v = unsafe { Vec::from_raw_parts(ptr, len, capacity) };
216 } else {
217 self.frombytes(&b);
218 }
219 })*
220 }
221 }
222
223 fn frombytes(&mut self, b: &[u8]) {
224 match self {
225 $(ArrayContentType::$n(v) => {
226 if b.len() > 0 {
229 let ptr = b.as_ptr() as *const $t;
230 let ptr_len = b.len() / core::mem::size_of::<$t>();
231 let slice = unsafe { core::slice::from_raw_parts(ptr, ptr_len) };
232 v.extend_from_slice(slice);
233 }
234 })*
235 }
236 }
237
238 fn fromlist(&mut self, list: &Py<PyList>, vm: &VirtualMachine) -> PyResult<()> {
239 match self {
240 $(ArrayContentType::$n(v) => {
241 let mut list: Vec<$t> = list
243 .borrow_vec()
244 .iter()
245 .cloned()
246 .map(|value| <$t>::try_into_from_object(vm, value))
247 .try_collect()?;
248 v.append(&mut list);
249 Ok(())
250 })*
251 }
252 }
253
254 fn get_bytes(&self) -> &[u8] {
255 match self {
256 $(ArrayContentType::$n(v) => {
257 let ptr = v.as_ptr() as *const u8;
259 let ptr_len = v.len() * core::mem::size_of::<$t>();
260 unsafe { core::slice::from_raw_parts(ptr, ptr_len) }
261 })*
262 }
263 }
264
265 fn get_bytes_mut(&mut self) -> &mut [u8] {
266 match self {
267 $(ArrayContentType::$n(v) => {
268 let ptr = v.as_ptr() as *mut u8;
270 let ptr_len = v.len() * core::mem::size_of::<$t>();
271 unsafe { core::slice::from_raw_parts_mut(ptr, ptr_len) }
272 })*
273 }
274 }
275
276 fn reverse(&mut self) {
277 match self {
278 $(ArrayContentType::$n(v) => v.reverse(),)*
279 }
280 }
281
282 fn get(
283 &self,
284 i: usize,
285 vm: &VirtualMachine
286 ) -> Option<PyResult> {
287 match self {
288 $(ArrayContentType::$n(v) => {
289 v.get(i).map(|x| x.to_pyresult(vm))
290 })*
291 }
292 }
293
294 fn getitem_by_index(&self, i: isize, vm: &VirtualMachine) -> PyResult {
295 match self {
296 $(ArrayContentType::$n(v) => {
297 v.getitem_by_index(vm, i).map(|x| x.to_pyresult(vm))?
298 })*
299 }
300 }
301
302 fn getitem_by_slice(&self, slice: SaturatedSlice, vm: &VirtualMachine) -> PyResult {
303 match self {
304 $(ArrayContentType::$n(v) => {
305 let r = v.getitem_by_slice(vm, slice)?;
306 let array = PyArray::from(ArrayContentType::$n(r));
307 array.to_pyresult(vm)
308 })*
309 }
310 }
311
312 fn item_from_object(
317 typecode: char,
318 value: PyObjectRef,
319 vm: &VirtualMachine
320 ) -> PyResult<ArrayItem> {
321 match typecode {
322 $($c => Ok(ArrayItem::$n(<$t>::try_into_from_object(vm, value)?)),)*
323 _ => unreachable!("array has a typecode"),
324 }
325 }
326
327 fn setitem_by_item(
328 &mut self,
329 i: isize,
330 item: ArrayItem,
331 vm: &VirtualMachine
332 ) -> PyResult<()> {
333 match (self, item) {
334 $((ArrayContentType::$n(v), ArrayItem::$n(value)) =>
335 v.setitem_by_index(vm, i, value),)*
336 _ => unreachable!("item was converted for this array"),
337 }
338 }
339
340 fn setitem_by_slice(
341 &mut self,
342 slice: SaturatedSlice,
343 items: &ArrayContentType,
344 vm: &VirtualMachine
345 ) -> PyResult<()> {
346 match self {
347 $(Self::$n(elements) => if let ArrayContentType::$n(items) = items {
348 elements.setitem_by_slice(vm, slice, items)
349 } else {
350 Err(vm.new_type_error(
351 "bad argument type for built-in operation".to_owned()
352 ))
353 },)*
354 }
355 }
356
357 fn setitem_by_slice_no_resize(
358 &mut self,
359 slice: SaturatedSlice,
360 items: &ArrayContentType,
361 vm: &VirtualMachine
362 ) -> PyResult<()> {
363 match self {
364 $(Self::$n(elements) => if let ArrayContentType::$n(items) = items {
365 elements.setitem_by_slice_no_resize(vm, slice, items)
366 } else {
367 Err(vm.new_type_error(
368 "bad argument type for built-in operation".to_owned()
369 ))
370 },)*
371 }
372 }
373
374 fn delitem_by_index(&mut self, i: isize, vm: &VirtualMachine) -> PyResult<()> {
375 match self {
376 $(ArrayContentType::$n(v) => {
377 v.delitem_by_index(vm, i)
378 })*
379 }
380 }
381
382 fn delitem_by_slice(&mut self, slice: SaturatedSlice, vm: &VirtualMachine) -> PyResult<()> {
383 match self {
384 $(ArrayContentType::$n(v) => {
385 v.delitem_by_slice(vm, slice)
386 })*
387 }
388 }
389
390 fn add(&self, other: &ArrayContentType, vm: &VirtualMachine) -> PyResult<Self> {
391 match self {
392 $(ArrayContentType::$n(v) => if let ArrayContentType::$n(other) = other {
393 let elements = v.iter().chain(other.iter()).cloned().collect();
394 Ok(ArrayContentType::$n(elements))
395 } else {
396 Err(vm.new_type_error(
397 "bad argument type for built-in operation".to_owned()
398 ))
399 },)*
400 }
401 }
402
403 fn iadd(&mut self, other: &ArrayContentType, vm: &VirtualMachine) -> PyResult<()> {
404 match self {
405 $(ArrayContentType::$n(v) => if let ArrayContentType::$n(other) = other {
406 v.extend(other);
407 Ok(())
408 } else {
409 Err(vm.new_type_error(
410 "can only extend with array of same kind".to_owned()
411 ))
412 },)*
413 }
414 }
415
416 fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<Self> {
417 match self {
418 $(ArrayContentType::$n(v) => {
419 let elements = v.mul(vm, value).map_err(|_| vm.no_memory_error())?;
422 Ok(ArrayContentType::$n(elements))
423 })*
424 }
425 }
426
427 fn imul(&mut self, value: isize, vm: &VirtualMachine) -> PyResult<()> {
428 match self {
429 $(ArrayContentType::$n(v) => {
430 v.imul(vm, value).map_err(|_| vm.no_memory_error())
433 })*
434 }
435 }
436
437 fn byteswap(&mut self) {
438 match self {
439 $(ArrayContentType::$n(v) => {
440 for element in v.iter_mut() {
441 let x = element.byteswap();
442 *element = x;
443 }
444 })*
445 }
446 }
447
448 fn repr(&self, class_name: &str, _vm: &VirtualMachine) -> PyResult<String> {
449 let s = match self {
451 $(ArrayContentType::$n(v) => {
452 if v.is_empty() {
453 format!("{}('{}')", class_name, $c)
454 } else {
455 format!("{}('{}', [{}])", class_name, $c, v.iter().format(", "))
456 }
457 })*
458 };
459 Ok(s)
460 }
461
462 fn iter<'a, 'vm: 'a>(
463 &'a self,
464 vm: &'vm VirtualMachine
465 ) -> impl Iterator<Item = PyResult> + 'a {
466 (0..self.len()).map(move |i| self.get(i, vm).unwrap())
467 }
468
469 fn cmp(&self, other: &ArrayContentType) -> Result<Option<Ordering>, ()> {
470 match self {
471 $(ArrayContentType::$n(v) => {
472 if let ArrayContentType::$n(other) = other {
473 Ok(PartialOrd::partial_cmp(v, other))
474 } else {
475 Err(())
476 }
477 })*
478 }
479 }
480
481 fn get_objects(&self, vm: &VirtualMachine) -> Vec<PyObjectRef> {
482 match self {
483 $(ArrayContentType::$n(v) => {
484 v.iter().map(|&x| x.to_object(vm)).collect()
485 })*
486 }
487 }
488 }
489 };
490 }
491
492 def_array_enum!(
493 (SignedByte, i8, 'b', "b"),
494 (UnsignedByte, u8, 'B', "B"),
495 (PyUnicode, WideChar, 'u', "u"),
496 (PyUcs4, Ucs4Char, 'w', "w"),
497 (SignedShort, raw::c_short, 'h', "h"),
498 (UnsignedShort, raw::c_ushort, 'H', "H"),
499 (SignedInt, raw::c_int, 'i', "i"),
500 (UnsignedInt, raw::c_uint, 'I', "I"),
501 (SignedLong, raw::c_long, 'l', "l"),
502 (UnsignedLong, raw::c_ulong, 'L', "L"),
503 (SignedLongLong, raw::c_longlong, 'q', "q"),
504 (UnsignedLongLong, raw::c_ulonglong, 'Q', "Q"),
505 (Float, f32, 'f', "f"),
506 (Double, f64, 'd', "d"),
507 );
508
509 #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
510 pub struct WideChar(wchar_t);
511
512 #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
515 pub struct Ucs4Char(u32);
516
517 trait ArrayElement: Sized {
518 fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>;
519 fn byteswap(self) -> Self;
520 fn to_object(self, vm: &VirtualMachine) -> PyObjectRef;
521
522 fn validate_slice(_items: &[Self], _vm: &VirtualMachine) -> PyResult<()> {
523 Ok(())
524 }
525
526 fn exact_search_item(_obj: &PyObject, _vm: &VirtualMachine) -> Option<Option<Self>> {
529 None
530 }
531 }
532
533 macro_rules! impl_int_element {
534 ($($t:ty,)*) => {$(
535 impl ArrayElement for $t {
536 fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
537 obj.try_index(vm)?.try_to_primitive_in_range(vm)
538 }
539 fn exact_search_item(obj: &PyObject, vm: &VirtualMachine) -> Option<Option<Self>> {
540 obj.class().is(vm.ctx.types.int_type).then(|| {
541 <$t>::try_from(obj.downcast_ref::<PyInt>().unwrap().as_bigint()).ok()
542 })
543 }
544 fn byteswap(self) -> Self {
545 <$t>::swap_bytes(self)
546 }
547 fn to_object(self, vm: &VirtualMachine) -> PyObjectRef {
548 self.to_pyobject(vm)
549 }
550 }
551 )*};
552 }
553
554 macro_rules! impl_float_element {
555 ($(($t:ty, $f_from:path, $f_swap:path, $f_to:path),)*) => {$(
556 impl ArrayElement for $t {
557 fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
558 $f_from(vm, obj)
559 }
560 fn exact_search_item(obj: &PyObject, vm: &VirtualMachine) -> Option<Option<Self>> {
561 obj.class().is(vm.ctx.types.float_type).then(|| {
562 let value = obj.downcast_ref::<PyFloat>().unwrap().to_f64();
563 let item = value as Self;
564 (item as f64 == value).then_some(item)
565 })
566 }
567 fn byteswap(self) -> Self {
568 $f_swap(self)
569 }
570 fn to_object(self, vm: &VirtualMachine) -> PyObjectRef {
571 $f_to(self).into_pyobject(vm)
572 }
573 }
574 )*};
575 }
576
577 impl_int_element!(i8, u8, i16, u16, i32, u32, i64, u64,);
578 impl_float_element!(
579 (
580 f32,
581 f32_try_into_from_object,
582 f32_swap_bytes,
583 pyfloat_from_f32
584 ),
585 (f64, f64_try_into_from_object, f64_swap_bytes, PyFloat::from),
586 );
587
588 const fn f32_swap_bytes(x: f32) -> f32 {
589 f32::from_bits(x.to_bits().swap_bytes())
590 }
591
592 const fn f64_swap_bytes(x: f64) -> f64 {
593 f64::from_bits(x.to_bits().swap_bytes())
594 }
595
596 fn f32_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f32> {
597 ArgIntoFloat::try_from_object(vm, obj).map(|x| x.into_float() as f32)
598 }
599
600 fn f64_try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<f64> {
601 ArgIntoFloat::try_from_object(vm, obj).map(|x| x.into_float())
602 }
603
604 fn pyfloat_from_f32(value: f32) -> PyFloat {
605 PyFloat::from(value as f64)
606 }
607
608 impl ArrayElement for WideChar {
609 fn exact_search_item(obj: &PyObject, vm: &VirtualMachine) -> Option<Option<Self>> {
610 if !obj.class().is(vm.ctx.types.str_type) {
611 return None;
612 }
613 let ch = obj
614 .downcast_ref::<PyStr>()
615 .unwrap()
616 .as_wtf8()
617 .code_points()
618 .exactly_one()
619 .ok()?;
620 num_traits::cast(ch.to_u32()).map(Self).map(Some)
621 }
622
623 fn validate_slice(items: &[Self], vm: &VirtualMachine) -> PyResult<()> {
624 let codepoints = || {
625 items
626 .iter()
627 .map(|item| num_traits::AsPrimitive::<u32>::as_(item.0))
628 };
629 if codepoints().max().unwrap_or(0) > 0x10ffff {
630 let invalid = codepoints().find(|&value| value > 0x10ffff).unwrap();
631 return Err(vm.new_value_error(u32_to_char(invalid).unwrap_err()));
632 }
633 Ok(())
634 }
635
636 fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
637 PyUtf8StrRef::try_from_object(vm, obj)?
638 .as_str()
639 .chars()
640 .exactly_one()
641 .map(|ch| Self(ch as _))
642 .map_err(|_| vm.new_type_error("array item must be unicode character"))
643 }
644 fn byteswap(self) -> Self {
645 Self(self.0.swap_bytes())
646 }
647 fn to_object(self, _vm: &VirtualMachine) -> PyObjectRef {
648 unreachable!()
649 }
650 }
651
652 impl ArrayElement for Ucs4Char {
653 fn exact_search_item(obj: &PyObject, vm: &VirtualMachine) -> Option<Option<Self>> {
654 if !obj.class().is(vm.ctx.types.str_type) {
655 return None;
656 }
657 let ch = obj
658 .downcast_ref::<PyStr>()
659 .unwrap()
660 .as_wtf8()
661 .code_points()
662 .exactly_one()
663 .ok()?;
664 Some(Some(Self(ch.to_u32())))
665 }
666
667 fn validate_slice(items: &[Self], vm: &VirtualMachine) -> PyResult<()> {
668 if items.iter().map(|item| item.0).max().unwrap_or(0) > 0x10ffff {
669 let invalid = items.iter().find(|item| item.0 > 0x10ffff).unwrap();
670 return Err(vm.new_value_error(u32_to_char(invalid.0).unwrap_err()));
671 }
672 Ok(())
673 }
674
675 fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
676 let s = obj.downcast::<PyStr>().map_err(|obj| {
677 vm.new_type_error(format!(
678 "array item must be a unicode character, not {}",
679 obj.class().name()
680 ))
681 })?;
682 s.as_wtf8()
683 .code_points()
684 .exactly_one()
685 .map(|ch| Self(ch.to_u32()))
686 .map_err(|e| {
687 vm.new_type_error(format!(
688 "array item must be a unicode character, not a string of length {}",
689 e.count()
690 ))
691 })
692 }
693 fn byteswap(self) -> Self {
694 Self(self.0.swap_bytes())
695 }
696 fn to_object(self, _vm: &VirtualMachine) -> PyObjectRef {
697 unreachable!()
698 }
699 }
700
701 impl ToPyResult for Ucs4Char {
702 fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
703 Ok(u32_to_char(self.0)
704 .map_err(|msg| vm.new_value_error(msg))?
705 .to_pyobject(vm))
706 }
707 }
708
709 impl fmt::Display for Ucs4Char {
710 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
711 unreachable!("`repr(array('w'))` calls `PyStr::repr`")
712 }
713 }
714
715 fn u32_to_char(ch: u32) -> Result<CodePoint, String> {
716 CodePoint::from_u32(ch)
717 .ok_or_else(|| format!("character U+{ch:4x} is not in range [U+0000; U+10ffff]"))
718 }
719
720 impl TryFrom<WideChar> for CodePoint {
721 type Error = String;
722
723 fn try_from(ch: WideChar) -> Result<Self, Self::Error> {
724 u32_to_char(ch.0 as _)
726 }
727 }
728
729 impl ToPyResult for WideChar {
730 fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
731 Ok(CodePoint::try_from(self)
732 .map_err(|e| vm.new_value_error(e))?
733 .to_pyobject(vm))
734 }
735 }
736
737 impl fmt::Display for WideChar {
738 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
739 unreachable!("`repr(array('u'))` calls `PyStr::repr`")
740 }
741 }
742
743 #[pyattr]
744 #[pyattr(name = "ArrayType")]
745 #[pyclass(name = "array", unhashable = true)]
746 #[derive(Debug, PyPayload)]
747 pub struct PyArray {
748 array: PyRwLock<ArrayContentType>,
749 exports: AtomicUsize,
750 }
751
752 pub type PyArrayRef = PyRef<PyArray>;
753
754 impl From<ArrayContentType> for PyArray {
755 fn from(array: ArrayContentType) -> Self {
756 Self {
757 array: PyRwLock::new(array),
758 exports: AtomicUsize::new(0),
759 }
760 }
761 }
762
763 #[derive(FromArgs)]
764 pub struct ArrayNewArgs {
765 #[pyarg(positional)]
766 spec: PyUtf8StrRef,
767 #[pyarg(positional, optional)]
768 init: OptionalArg<PyObjectRef>,
769 }
770
771 impl Constructor for PyArray {
772 type Args = (ArrayNewArgs, KwArgs);
773
774 fn py_new(
775 cls: &Py<PyType>,
776 (ArrayNewArgs { spec, init }, kwargs): Self::Args,
777 vm: &VirtualMachine,
778 ) -> PyResult<Self> {
779 let spec = spec.as_str().chars().exactly_one().map_err(|_| {
780 vm.new_type_error("array() argument 1 must be a unicode character, not str")
781 })?;
782
783 if cls.is(Self::class(&vm.ctx)) && !kwargs.is_empty() {
784 return Err(vm.new_type_error("array.array() takes no keyword arguments"));
785 }
786
787 if spec == 'u' {
788 _warnings::warn(
789 vm.ctx.exceptions.deprecation_warning,
790 "The 'u' type code is deprecated and will be removed in Python 3.16".to_owned(),
791 1,
792 vm,
793 )?;
794 }
795
796 let mut array =
797 ArrayContentType::from_char(spec).map_err(|err| vm.new_value_error(err))?;
798
799 if let OptionalArg::Present(init) = init {
800 if let Some(init) = init.downcast_ref::<Self>() {
801 let value = init.read().typecode();
802 match (spec, value) {
803 (spec, ch) if spec == ch => array.frombytes(&init.get_bytes()),
804 (spec, 'u' | 'w') if !matches!(spec, 'u' | 'w') => {
805 return Err(vm.new_type_error(format!(
806 "cannot use a unicode array to initialize an array with typecode '{spec}'"
807 )))
808 }
809 _ => {
810 for obj in init.read().iter(vm) {
811 array.push(obj?, vm)?;
812 }
813 }
814 }
815 } else if let Some(wtf8) = init.downcast_ref::<PyStr>() {
816 if matches!(spec, 'u' | 'w') {
817 let bytes = Self::_unicode_to_wchar_bytes(wtf8.as_wtf8(), array.itemsize());
818 array.frombytes_move(bytes);
819 } else {
820 return Err(vm.new_type_error(format!(
821 "cannot use a str to initialize an array with typecode '{spec}'"
822 )));
823 }
824 } else if init.downcastable::<PyBytes>() || init.downcastable::<PyByteArray>() {
825 init.try_bytes_like(vm, |x| array.frombytes(x))?;
826 } else {
827 let iter = ArgIterable::try_from_object(vm, init)?;
829 for obj in iter.iter(vm)? {
830 array.push(obj?, vm)?;
831 }
832 }
833 }
834
835 Ok(Self::from(array))
836 }
837 }
838
839 #[derive(FromArgs)]
840 struct PopArgs {
841 #[pyarg(positional, default = -1)]
842 i: isize,
843 }
844
845 #[derive(FromArgs)]
846 struct ArrayIndexArgs {
847 #[pyarg(positional, optional, py_default = "0")]
848 start: OptionalArg<ArgIndex>,
849 #[pyarg(positional, optional, py_default = "9223372036854775807")]
850 stop: OptionalArg<ArgIndex>,
851 }
852
853 #[pyclass(
854 flags(BASETYPE, HAS_WEAKREF),
855 with(
856 Comparable,
857 AsBuffer,
858 AsMapping,
859 AsSequence,
860 Iterable,
861 Constructor,
862 Representable
863 )
864 )]
865 impl PyArray {
866 fn read(&self) -> PyRwLockReadGuard<'_, ArrayContentType> {
867 self.array.read()
868 }
869
870 fn write(&self) -> PyRwLockWriteGuard<'_, ArrayContentType> {
871 self.array.write()
872 }
873
874 #[pygetset]
875 fn typecode(zelf: &Py<Self>, vm: &VirtualMachine) -> PyStrRef {
876 vm.ctx
877 .intern_str(zelf.read().typecode().to_string())
878 .to_owned()
879 }
880
881 #[pygetset]
882 fn itemsize(zelf: &Py<Self>) -> usize {
883 zelf.read().itemsize()
884 }
885
886 #[pymethod]
887 fn append(zelf: &Py<Self>, v: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
888 let typecode = zelf.read().typecode();
889 let item = ArrayContentType::item_from_object(typecode, v, vm)?;
890 zelf.try_resizable(vm)?.push_item(item);
891 Ok(())
892 }
893
894 #[pymethod]
895 fn clear(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<()> {
896 zelf.try_resizable(vm)?.clear()
897 }
898
899 #[pymethod]
900 fn buffer_info(zelf: &Py<Self>) -> (usize, usize) {
901 let array = zelf.read();
902 (array.addr(), array.len())
903 }
904
905 #[pymethod]
906 fn count(zelf: &Py<Self>, v: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
907 let array = zelf.read();
908 if let Some(item) = ArrayContentType::search_item(array.typecode(), &v, vm) {
909 return item.map_or(Ok(0), |item| array.count_item(&item, vm));
910 }
911 drop(array);
912 let mut count = 0;
913 for index in 0..usize::MAX {
914 let item = zelf.read().get(index, vm);
915 let Some(item) = item else { break };
916 if item?.rich_compare_bool(&v, PyComparisonOp::Eq, vm)? {
917 count += 1;
918 }
919 }
920 Ok(count)
921 }
922
923 fn find(
924 &self,
925 value: &PyObject,
926 start: usize,
927 stop: usize,
928 vm: &VirtualMachine,
929 ) -> PyResult<Option<usize>> {
930 let array = self.read();
931 if let Some(item) = ArrayContentType::search_item(array.typecode(), value, vm) {
932 return item.map_or(Ok(None), |item| array.find_item(&item, start, stop, vm));
933 }
934 drop(array);
935 for index in start..stop {
936 let item = self.read().get(index, vm);
937 let Some(item) = item else { break };
938 if item?.rich_compare_bool(value, PyComparisonOp::Eq, vm)? {
939 return Ok(Some(index));
940 }
941 }
942 Ok(None)
943 }
944
945 #[pymethod]
946 fn remove(zelf: &Py<Self>, v: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
947 let index = zelf
948 .find(&v, 0, usize::MAX, vm)?
949 .ok_or_else(|| vm.new_value_error("array.remove(x): x not in array"))?;
950 if index < zelf.__len__() {
953 let mut array = zelf.try_resizable(vm)?;
954 if index < array.len() {
955 array.delitem_by_index(index as isize, vm)?;
956 }
957 }
958 Ok(())
959 }
960
961 #[pymethod]
962 fn extend(zelf: &Py<Self>, bb: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
963 if zelf.is(&bb) {
964 zelf.try_resizable(vm)?.imul(2, vm)
965 } else if let Some(array) = bb.downcast_ref::<Self>() {
966 zelf.try_resizable(vm)?.iadd(&array.read(), vm)
967 } else {
968 let iter = ArgIterable::try_from_object(vm, bb)?;
969 for obj in iter.iter(vm)? {
970 Self::append(zelf, obj?, vm)?;
971 }
972 Ok(())
973 }
974 }
975
976 fn _wchar_bytes_to_string(
977 bytes: &[u8],
978 item_size: usize,
979 vm: &VirtualMachine,
980 ) -> PyResult<Wtf8Buf> {
981 if item_size == 2 {
982 let utf16 = unsafe {
984 core::slice::from_raw_parts(
985 bytes.as_ptr() as *const u16,
986 bytes.len() / core::mem::size_of::<u16>(),
987 )
988 };
989 Ok(Wtf8Buf::from_wide(utf16))
990 } else {
991 let chars = unsafe {
993 core::slice::from_raw_parts(
994 bytes.as_ptr() as *const u32,
995 bytes.len() / core::mem::size_of::<u32>(),
996 )
997 };
998 chars
999 .iter()
1000 .map(|&ch| {
1001 u32_to_char(ch).map_err(|msg| vm.new_value_error(msg))
1003 })
1004 .try_collect()
1005 }
1006 }
1007
1008 fn _unicode_to_wchar_bytes(wtf8: &Wtf8, item_size: usize) -> Vec<u8> {
1009 if item_size == 2 {
1010 wtf8.encode_wide().flat_map(|ch| ch.to_ne_bytes()).collect()
1011 } else {
1012 wtf8.code_points()
1013 .flat_map(|ch| ch.to_u32().to_ne_bytes())
1014 .collect()
1015 }
1016 }
1017
1018 #[pymethod]
1019 fn fromunicode(zelf: &Py<Self>, ustr: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1020 let wtf8: &Wtf8 = ustr.try_to_value(vm).map_err(|_| {
1021 vm.new_type_error(format!(
1022 "fromunicode() argument must be str, not {}",
1023 ustr.class().name()
1024 ))
1025 })?;
1026 if !matches!(zelf.read().typecode(), 'u' | 'w') {
1027 return Err(vm.new_value_error(
1028 "fromunicode() may only be called on unicode type arrays ('u' or 'w')",
1029 ));
1030 }
1031 let mut w = zelf.try_resizable(vm)?;
1032 let bytes = Self::_unicode_to_wchar_bytes(wtf8, w.itemsize());
1033 w.frombytes_move(bytes);
1034 Ok(())
1035 }
1036
1037 #[pymethod]
1038 fn tounicode(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
1039 let array = zelf.array.read();
1040 if !matches!(array.typecode(), 'u' | 'w') {
1041 return Err(vm.new_value_error(
1042 "tounicode() may only be called on unicode type arrays ('u' or 'w')",
1043 ));
1044 }
1045 let bytes = array.get_bytes();
1046 Self::_wchar_bytes_to_string(bytes, Self::itemsize(zelf), vm)
1047 }
1048
1049 fn _from_bytes(&self, b: &[u8], itemsize: usize, vm: &VirtualMachine) -> PyResult<()> {
1050 if !b.len().is_multiple_of(itemsize) {
1051 return Err(vm.new_value_error("bytes length not a multiple of item size"));
1052 }
1053 if b.len() / itemsize > 0 {
1054 self.try_resizable(vm)?.frombytes(b);
1055 }
1056 Ok(())
1057 }
1058
1059 #[pymethod]
1060 fn frombytes(zelf: &Py<Self>, buffer: ArgBytesLike, vm: &VirtualMachine) -> PyResult<()> {
1061 if buffer.itemsize() != 1 {
1064 return Err(vm.new_type_error("a bytes-like object is required"));
1065 }
1066 let buffer = buffer.borrow_buf();
1067 let itemsize = zelf.read().itemsize();
1068 zelf._from_bytes(&buffer, itemsize, vm)
1069 }
1070
1071 #[pymethod]
1072 fn fromfile(
1073 zelf: &Py<Self>,
1074 f: PyObjectRef,
1075 n: isize,
1076 vm: &VirtualMachine,
1077 ) -> PyResult<()> {
1078 let itemsize = Self::itemsize(zelf);
1079 if n < 0 {
1080 return Err(vm.new_value_error("negative count"));
1081 }
1082 let n = vm.check_repeat_or_overflow_error(itemsize, n)?;
1083 let n_bytes = n * itemsize;
1084
1085 let b = vm.call_method(&f, "read", (n_bytes,))?;
1086 let b = b
1087 .downcast::<PyBytes>()
1088 .map_err(|_| vm.new_type_error("read() didn't return bytes"))?;
1089
1090 let not_enough_bytes = b.as_bytes().len() != n_bytes;
1091
1092 zelf._from_bytes(b.as_bytes(), itemsize, vm)?;
1093
1094 if not_enough_bytes {
1095 Err(vm.new_eof_error("read() didn't return enough bytes"))
1096 } else {
1097 Ok(())
1098 }
1099 }
1100
1101 #[pymethod]
1102 fn byteswap(zelf: &Py<Self>) {
1103 zelf.write().byteswap();
1104 }
1105
1106 #[pymethod]
1107 fn index(
1108 zelf: &Py<Self>,
1109 v: PyObjectRef,
1110 ArrayIndexArgs { start, stop }: ArrayIndexArgs,
1111 vm: &VirtualMachine,
1112 ) -> PyResult<usize> {
1113 let len = zelf.__len__();
1114 let bound = |value: ArgIndex| {
1115 let value = value.as_ref().as_bigint();
1116 value.saturated_at(if value.is_negative() {
1117 len
1118 } else {
1119 isize::MAX as usize
1120 })
1121 };
1122 let start = start.map_or(0, bound);
1123 let stop = stop.map_or(isize::MAX as usize, bound);
1124 zelf.find(&v, start, stop, vm)?
1125 .ok_or_else(|| vm.new_value_error("array.index(x): x not in array"))
1126 }
1127
1128 #[pymethod]
1129 fn insert(zelf: &Py<Self>, i: isize, v: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1130 let typecode = zelf.read().typecode();
1131 let item = ArrayContentType::item_from_object(typecode, v, vm)?;
1132 zelf.try_resizable(vm)?.insert_item(i, item);
1133 Ok(())
1134 }
1135
1136 #[pymethod]
1137 fn pop(zelf: &Py<Self>, args: PopArgs, vm: &VirtualMachine) -> PyResult {
1138 let mut w = zelf.try_resizable(vm)?;
1139 if w.len() == 0 {
1140 Err(vm.new_index_error("pop from empty array"))
1141 } else {
1142 w.pop(args.i, vm)
1143 }
1144 }
1145
1146 #[pymethod]
1147 pub(crate) fn tobytes(zelf: &Py<Self>) -> Vec<u8> {
1148 zelf.read().get_bytes().to_vec()
1149 }
1150
1151 #[pymethod]
1152 fn tofile(zelf: &Py<Self>, f: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1153 const BLOCKSIZE: usize = 64 * 1024;
1156
1157 let bytes = {
1158 let bytes = zelf.read();
1159 bytes.get_bytes().to_vec()
1160 };
1161
1162 for b in bytes.chunks(BLOCKSIZE) {
1163 let b = PyBytes::from(b.to_vec()).into_ref(&vm.ctx);
1164 vm.call_method(&f, "write", (b,))?;
1165 }
1166 Ok(())
1167 }
1168
1169 pub(crate) fn get_bytes(&self) -> PyMappedRwLockReadGuard<'_, [u8]> {
1170 PyRwLockReadGuard::map(self.read(), |a| a.get_bytes())
1171 }
1172
1173 pub(crate) fn get_bytes_mut(&self) -> PyMappedRwLockWriteGuard<'_, [u8]> {
1174 PyRwLockWriteGuard::map(self.write(), |a| a.get_bytes_mut())
1175 }
1176
1177 #[pymethod]
1178 fn tolist(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Vec<PyObjectRef>> {
1179 let array = zelf.read();
1180 let mut v = Vec::with_capacity(array.len());
1181 for obj in array.iter(vm) {
1182 v.push(obj?);
1183 }
1184 Ok(v)
1185 }
1186
1187 #[pymethod]
1188 fn fromlist(zelf: &Py<Self>, list: PyListRef, vm: &VirtualMachine) -> PyResult<()> {
1189 zelf.try_resizable(vm)?.fromlist(&list, vm)
1190 }
1191
1192 #[pymethod]
1193 fn reverse(zelf: &Py<Self>) {
1194 zelf.write().reverse()
1195 }
1196
1197 #[pymethod]
1198 fn __copy__(zelf: &Py<Self>) -> Self {
1199 zelf.array.read().clone().into()
1200 }
1201
1202 #[pymethod]
1203 fn __deepcopy__(zelf: &Py<Self>, _unused: PyObjectRef) -> Self {
1204 Self::__copy__(zelf)
1205 }
1206
1207 fn getitem_inner(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
1208 match SequenceIndex::try_from_borrowed_object(vm, needle, "array")? {
1209 SequenceIndex::Int(i) => self.read().getitem_by_index(i, vm),
1210 SequenceIndex::Slice(slice) => self.read().getitem_by_slice(slice, vm),
1211 }
1212 }
1213
1214 fn __getitem__(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult {
1215 self.getitem_inner(needle, vm)
1216 }
1217
1218 fn setitem_inner(
1219 zelf: &Py<Self>,
1220 needle: &PyObject,
1221 value: PyObjectRef,
1222 vm: &VirtualMachine,
1223 ) -> PyResult<()> {
1224 match SequenceIndex::try_from_borrowed_object(vm, needle, "array")? {
1225 SequenceIndex::Int(i) => {
1226 let typecode = zelf.read().typecode();
1227 let item = ArrayContentType::item_from_object(typecode, value, vm)?;
1228 zelf.write().setitem_by_item(i, item, vm)
1229 }
1230 SequenceIndex::Slice(slice) => {
1231 let cloned;
1232 let guard;
1233 let items = if zelf.is(&value) {
1234 cloned = zelf.read().clone();
1235 &cloned
1236 } else {
1237 match value.downcast_ref::<Self>() {
1238 Some(array) => {
1239 guard = array.read();
1240 &*guard
1241 }
1242 None => {
1243 return Err(vm.new_type_error(format!(
1244 "can only assign array (not \"{}\") to array slice",
1245 value.class()
1246 )));
1247 }
1248 }
1249 };
1250 if let Ok(mut w) = zelf.try_resizable(vm) {
1251 w.setitem_by_slice(slice, items, vm)
1252 } else {
1253 zelf.write().setitem_by_slice_no_resize(slice, items, vm)
1254 }
1255 }
1256 }
1257 }
1258
1259 fn __setitem__(
1260 zelf: &Py<Self>,
1261 needle: &PyObject,
1262 value: PyObjectRef,
1263 vm: &VirtualMachine,
1264 ) -> PyResult<()> {
1265 Self::setitem_inner(zelf, needle, value, vm)
1266 }
1267
1268 fn delitem_inner(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
1269 match SequenceIndex::try_from_borrowed_object(vm, needle, "array")? {
1270 SequenceIndex::Int(i) => self.try_resizable(vm)?.delitem_by_index(i, vm),
1271 SequenceIndex::Slice(slice) => self.try_resizable(vm)?.delitem_by_slice(slice, vm),
1272 }
1273 }
1274
1275 fn __delitem__(&self, needle: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
1276 self.delitem_inner(needle, vm)
1277 }
1278
1279 fn __add__(&self, other: &PyObject, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1280 if let Some(other) = other.downcast_ref::<Self>() {
1281 self.read()
1282 .add(&other.read(), vm)
1283 .map(|array| Self::from(array).into_ref(&vm.ctx))
1284 } else {
1285 Err(vm.new_type_error(format!(
1286 "can only append array (not \"{}\") to array",
1287 other.class().name()
1288 )))
1289 }
1290 }
1291
1292 fn __iadd__(
1293 zelf: PyRef<Self>,
1294 other: &PyObject,
1295 vm: &VirtualMachine,
1296 ) -> PyResult<PyRef<Self>> {
1297 if zelf.is(other) {
1298 zelf.try_resizable(vm)?.imul(2, vm)?;
1299 } else if let Some(other) = other.downcast_ref::<Self>() {
1300 zelf.try_resizable(vm)?.iadd(&other.read(), vm)?;
1301 } else {
1302 return Err(vm.new_type_error(format!(
1303 "can only extend array with array (not \"{}\")",
1304 other.class().name()
1305 )));
1306 }
1307 Ok(zelf)
1308 }
1309
1310 fn __mul__(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1311 self.read()
1312 .mul(value, vm)
1313 .map(|x| Self::from(x).into_ref(&vm.ctx))
1314 }
1315
1316 fn __imul__(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
1317 zelf.try_resizable(vm)?.imul(value, vm)?;
1318 Ok(zelf)
1319 }
1320
1321 pub(crate) fn __len__(&self) -> usize {
1322 self.read().len()
1323 }
1324
1325 fn array_eq(&self, other: &Self, vm: &VirtualMachine) -> PyResult<bool> {
1326 if self.__len__() != other.__len__() {
1329 return Ok(false);
1330 }
1331 let array_a = self.read();
1332 let array_b = other.read();
1333
1334 if let Ok(ord) = array_a.cmp(&array_b) {
1336 return Ok(ord == Some(Ordering::Equal));
1337 }
1338
1339 let iter = Iterator::zip(array_a.iter(vm), array_b.iter(vm));
1340
1341 for (a, b) in iter {
1342 if !vm.bool_eq(&*a?, &*b?)? {
1343 return Ok(false);
1344 }
1345 }
1346 Ok(true)
1347 }
1348
1349 #[pymethod]
1350 fn __reduce_ex__(
1351 zelf: &Py<Self>,
1352 value: usize,
1353 vm: &VirtualMachine,
1354 ) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
1355 if value < 3 {
1356 return Self::__reduce__(zelf, vm);
1357 }
1358 let array = zelf.read();
1359 let cls = zelf.class().to_owned();
1360 let typecode = vm.ctx.new_str(array.typecode_str());
1361 let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
1362 let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
1363 let code = PyInt::from(u8::from(code)).into_pyobject(vm);
1364 let module = vm.import("array", 0)?;
1365 let func = module.get_attr("_array_reconstructor", vm)?;
1366 Ok((
1367 func,
1368 vm.new_tuple((cls, typecode, code, bytes)),
1369 zelf.as_object().dict(),
1370 ))
1371 }
1372
1373 #[pymethod]
1374 fn __reduce__(
1375 zelf: &Py<Self>,
1376 vm: &VirtualMachine,
1377 ) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
1378 let array = zelf.read();
1379 let cls = zelf.class().to_owned();
1380 let typecode = vm.ctx.new_str(array.typecode_str());
1381 let values = if matches!(array.typecode(), 'u' | 'w') {
1382 let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?;
1383 s.code_points().map(|x| x.to_pyobject(vm)).collect()
1384 } else {
1385 array.get_objects(vm)
1386 };
1387 let values = vm.ctx.new_list(values);
1388 Ok((
1389 cls.into(),
1390 vm.new_tuple((typecode, values)),
1391 zelf.as_object().dict(),
1392 ))
1393 }
1394
1395 fn __contains__(&self, value: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
1396 self.find(value, 0, usize::MAX, vm)
1397 .map(|index| index.is_some())
1398 }
1399
1400 #[pyclassmethod]
1401 fn __class_getitem__(
1402 cls: PyTypeRef,
1403 object: PyObjectRef,
1404 vm: &VirtualMachine,
1405 ) -> PyResult<PyGenericAlias> {
1406 PyGenericAlias::from_args(cls, object, vm)
1407 }
1408 }
1409
1410 impl Comparable for PyArray {
1411 fn cmp(
1412 zelf: &Py<Self>,
1413 other: &PyObject,
1414 op: PyComparisonOp,
1415 vm: &VirtualMachine,
1416 ) -> PyResult<PyComparisonValue> {
1417 let other = class_or_notimplemented!(Self, other);
1422
1423 if let PyComparisonValue::Implemented(x) =
1424 op.eq_only(|| Ok(zelf.array_eq(other, vm)?.into()))?
1425 {
1426 return Ok(x.into());
1427 }
1428
1429 let array_a = zelf.read();
1430 let array_b = other.read();
1431
1432 let res = match array_a.cmp(&array_b) {
1433 Ok(partial_ord) => partial_ord.is_some_and(|ord| op.eval_ord(ord)),
1435 Err(()) => {
1436 let iter = Iterator::zip(array_a.iter(vm), array_b.iter(vm));
1437
1438 for (a, b) in iter {
1439 let ret = match op {
1440 PyComparisonOp::Lt | PyComparisonOp::Le => {
1441 vm.bool_seq_lt(&*a?, &*b?)?
1442 }
1443 PyComparisonOp::Gt | PyComparisonOp::Ge => {
1444 vm.bool_seq_gt(&*a?, &*b?)?
1445 }
1446 _ => unreachable!(),
1447 };
1448 if let Some(v) = ret {
1449 return Ok(PyComparisonValue::Implemented(v));
1450 }
1451 }
1452
1453 op.eval_ord(array_a.len().cmp(&array_b.len()))
1455 }
1456 };
1457
1458 Ok(res.into())
1459 }
1460 }
1461
1462 impl PyArray {
1463 fn buffer_desc(&self) -> BufferDescriptor {
1464 let array = self.read();
1465 BufferDescriptor::format(
1466 array.len() * array.itemsize(),
1467 false,
1468 array.itemsize(),
1469 array.typecode_str().into(),
1470 )
1471 }
1472 }
1473
1474 impl AsBuffer for PyArray {
1475 const RELEASE_BUFFER: bool = true;
1476
1477 fn slot_as_buffer(
1480 zelf: &PyObject,
1481 flags: BufferFlags,
1482 vm: &VirtualMachine,
1483 ) -> PyResult<PyBuffer> {
1484 let zelf = zelf
1485 .downcast_ref::<Self>()
1486 .ok_or_else(|| vm.new_type_error("unexpected payload for as_buffer"))?;
1487 let desc = zelf.buffer_desc().projected(flags);
1488 flags.check_writable(desc.readonly, "Object is not writable.", vm)?;
1489 Ok(PyBuffer::new(zelf.to_owned().into(), desc, &BUFFER_METHODS))
1490 }
1491
1492 fn as_buffer(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
1493 Ok(PyBuffer::new(
1494 zelf.to_owned().into(),
1495 zelf.buffer_desc(),
1496 &BUFFER_METHODS,
1497 ))
1498 }
1499 }
1500
1501 impl Representable for PyArray {
1502 #[inline]
1503 fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
1504 let class = zelf.class();
1505 let class_name = class.name();
1506 let typecode = zelf.read().typecode();
1507 if matches!(typecode, 'u' | 'w') {
1508 if zelf.__len__() == 0 {
1509 return Ok(format!("{class_name}('{typecode}')"));
1510 }
1511 let to_unicode = Self::tounicode(zelf, vm)?;
1512 let escape = crate::vm::literal::escape::UnicodeEscape::new_repr(&to_unicode);
1513 return Ok(format!("{class_name}('{typecode}', {})", escape.str_repr()));
1514 }
1515 zelf.read().repr(&class_name, vm)
1516 }
1517 }
1518
1519 static BUFFER_METHODS: BufferMethods = BufferMethods {
1520 obj_bytes: |buffer| buffer.obj_as::<PyArray>().get_bytes().into(),
1521 obj_bytes_mut: |buffer| buffer.obj_as::<PyArray>().get_bytes_mut().into(),
1522 release: |buffer| {
1523 buffer
1524 .obj_as::<PyArray>()
1525 .exports
1526 .fetch_sub(1, atomic::Ordering::Release);
1527 },
1528 retain: |buffer| {
1529 buffer
1530 .obj_as::<PyArray>()
1531 .exports
1532 .fetch_add(1, atomic::Ordering::Release);
1533 },
1534 };
1535
1536 impl AsMapping for PyArray {
1537 fn as_mapping() -> &'static PyMappingMethods {
1538 static AS_MAPPING: PyMappingMethods = PyMappingMethods {
1539 length: atomic_func!(|mapping, _vm| Ok(
1540 PyArray::mapping_downcast(mapping).__len__()
1541 )),
1542 subscript: atomic_func!(|mapping, needle, vm| {
1543 PyArray::mapping_downcast(mapping).getitem_inner(needle, vm)
1544 }),
1545 ass_subscript: atomic_func!(|mapping, needle, value, vm| {
1546 let zelf = PyArray::mapping_downcast(mapping);
1547 if let Some(value) = value {
1548 PyArray::setitem_inner(zelf, needle, value, vm)
1549 } else {
1550 zelf.delitem_inner(needle, vm)
1551 }
1552 }),
1553 };
1554 &AS_MAPPING
1555 }
1556 }
1557
1558 impl AsSequence for PyArray {
1559 fn as_sequence() -> &'static PySequenceMethods {
1560 static AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
1561 length: atomic_func!(|seq, _vm| Ok(PyArray::sequence_downcast(seq).__len__())),
1562 concat: atomic_func!(|seq, other, vm| {
1563 let zelf = PyArray::sequence_downcast(seq);
1564 PyArray::__add__(zelf, other, vm).map(|x| x.into())
1565 }),
1566 repeat: atomic_func!(|seq, n, vm| {
1567 PyArray::sequence_downcast(seq)
1568 .__mul__(n, vm)
1569 .map(|x| x.into())
1570 }),
1571 item: atomic_func!(|seq, i, vm| {
1572 PyArray::sequence_downcast(seq)
1573 .read()
1574 .getitem_by_index(i, vm)
1575 }),
1576 ass_item: atomic_func!(|seq, i, value, vm| {
1577 let zelf = PyArray::sequence_downcast(seq);
1578 if let Some(value) = value {
1579 let typecode = zelf.read().typecode();
1580 let item = ArrayContentType::item_from_object(typecode, value, vm)?;
1581 zelf.write().setitem_by_item(i, item, vm)
1582 } else {
1583 zelf.write().delitem_by_index(i, vm)
1584 }
1585 }),
1586 contains: atomic_func!(|seq, target, vm| {
1587 let zelf = PyArray::sequence_downcast(seq);
1588 zelf.__contains__(target, vm)
1589 }),
1590 inplace_concat: atomic_func!(|seq, other, vm| {
1591 let zelf = PyArray::sequence_downcast(seq).to_owned();
1592 PyArray::__iadd__(zelf, other, vm).map(|x| x.into())
1593 }),
1594 inplace_repeat: atomic_func!(|seq, n, vm| {
1595 let zelf = PyArray::sequence_downcast(seq).to_owned();
1596 PyArray::__imul__(zelf, n, vm).map(|x| x.into())
1597 }),
1598 };
1599 &AS_SEQUENCE
1600 }
1601 }
1602
1603 impl Iterable for PyArray {
1604 fn iter(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
1605 Ok(PyArrayIter {
1606 internal: PyMutex::new(PositionIterInternal::new(zelf, 0)),
1607 }
1608 .into_pyobject(vm))
1609 }
1610 }
1611
1612 impl BufferResizeGuard for PyArray {
1613 type Resizable<'a> = PyRwLockWriteGuard<'a, ArrayContentType>;
1614
1615 fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
1616 (self.exports.load(atomic::Ordering::SeqCst) == 0).then(|| self.write())
1619 }
1620
1621 fn try_resizable(&self, vm: &VirtualMachine) -> PyResult<Self::Resizable<'_>> {
1622 self.try_resizable_opt().ok_or_else(|| {
1623 vm.new_buffer_error("cannot resize an array that is exporting buffers")
1624 })
1625 }
1626 }
1627
1628 #[pyattr]
1629 #[pyclass(name = "arrayiterator", traverse)]
1630 #[derive(Debug, PyPayload)]
1631 pub struct PyArrayIter {
1632 internal: PyMutex<PositionIterInternal<PyArrayRef>>,
1633 }
1634
1635 #[pyclass(with(IterNext, Iterable), flags(HAS_DICT, DISALLOW_INSTANTIATION))]
1636 impl PyArrayIter {
1637 #[pymethod]
1638 fn __setstate__(zelf: &Py<Self>, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
1639 zelf.internal
1640 .lock()
1641 .set_state(&state, |obj, pos| pos.min(obj.__len__()), vm)
1642 }
1643
1644 #[pymethod]
1645 fn __reduce__(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyTupleRef> {
1646 let func = builtins_iter(vm)?;
1647 Ok(zelf.internal.lock().reduce(
1648 func,
1649 |x| x.clone().into(),
1650 |vm| vm.ctx.empty_tuple.clone().into(),
1651 vm,
1652 ))
1653 }
1654 }
1655
1656 impl SelfIter for PyArrayIter {}
1657
1658 impl IterNext for PyArrayIter {
1659 fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
1660 locked_next(&zelf.internal, |array, pos| {
1661 let value = array.read().get(pos, vm);
1662 Ok(if let Some(item) = value {
1663 PyIterReturn::Return(item?)
1664 } else {
1665 PyIterReturn::StopIteration(None)
1666 })
1667 })
1668 }
1669 }
1670
1671 #[derive(FromArgs)]
1672 struct ReconstructorArgs {
1673 #[pyarg(positional)]
1674 arraytype: PyTypeRef,
1675 #[pyarg(positional)]
1676 typecode: PyUtf8StrRef,
1677 #[pyarg(positional)]
1678 mformat_code: MachineFormatCode,
1679 #[pyarg(positional)]
1680 items: PyBytesRef,
1681 }
1682
1683 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
1684 #[repr(u8)]
1685 enum MachineFormatCode {
1686 Int8 { signed: bool }, Int16 { signed: bool, big_endian: bool }, Int32 { signed: bool, big_endian: bool }, Int64 { signed: bool, big_endian: bool }, Ieee754Float { big_endian: bool }, Ieee754Double { big_endian: bool }, Utf16 { big_endian: bool }, Utf32 { big_endian: bool }, }
1695
1696 impl From<MachineFormatCode> for u8 {
1697 fn from(code: MachineFormatCode) -> Self {
1698 match code {
1699 MachineFormatCode::Int8 { signed } => signed as Self,
1700 MachineFormatCode::Int16 { signed, big_endian } => {
1701 2 + signed as Self * 2 + big_endian as Self
1702 }
1703 MachineFormatCode::Int32 { signed, big_endian } => {
1704 6 + signed as Self * 2 + big_endian as Self
1705 }
1706 MachineFormatCode::Int64 { signed, big_endian } => {
1707 10 + signed as Self * 2 + big_endian as Self
1708 }
1709 MachineFormatCode::Ieee754Float { big_endian } => 14 + big_endian as Self,
1710 MachineFormatCode::Ieee754Double { big_endian } => 16 + big_endian as Self,
1711 MachineFormatCode::Utf16 { big_endian } => 18 + big_endian as Self,
1712 MachineFormatCode::Utf32 { big_endian } => 20 + big_endian as Self,
1713 }
1714 }
1715 }
1716
1717 impl TryFrom<u8> for MachineFormatCode {
1718 type Error = u8;
1719
1720 fn try_from(code: u8) -> Result<Self, Self::Error> {
1721 let big_endian = !code.is_multiple_of(2);
1722 let signed = match code {
1723 0 | 1 => code != 0,
1724 2..=13 => (code - 2) % 4 >= 2,
1725 _ => false,
1726 };
1727 match code {
1728 0..=1 => Ok(Self::Int8 { signed }),
1729 2..=5 => Ok(Self::Int16 { signed, big_endian }),
1730 6..=9 => Ok(Self::Int32 { signed, big_endian }),
1731 10..=13 => Ok(Self::Int64 { signed, big_endian }),
1732 14..=15 => Ok(Self::Ieee754Float { big_endian }),
1733 16..=17 => Ok(Self::Ieee754Double { big_endian }),
1734 18..=19 => Ok(Self::Utf16 { big_endian }),
1735 20..=21 => Ok(Self::Utf32 { big_endian }),
1736 _ => Err(code),
1737 }
1738 }
1739 }
1740
1741 impl<'a> TryFromBorrowedObject<'a> for MachineFormatCode {
1742 fn try_from_borrowed_object(vm: &VirtualMachine, obj: &'a PyObject) -> PyResult<Self> {
1743 obj.try_to_ref::<PyInt>(vm)
1744 .map_err(|_| {
1745 vm.new_type_error(format!(
1746 "an integer is required (got type {})",
1747 obj.class().name()
1748 ))
1749 })?
1750 .try_to_primitive::<i32>(vm)?
1751 .to_u8()
1752 .unwrap_or(u8::MAX)
1753 .try_into()
1754 .map_err(|_| {
1755 vm.new_value_error("third argument must be a valid machine format code.")
1756 })
1757 }
1758 }
1759
1760 impl MachineFormatCode {
1761 fn from_typecode(code: char) -> Option<Self> {
1762 use core::mem::size_of;
1763 let signed = code.is_ascii_uppercase();
1764 let big_endian = cfg!(target_endian = "big");
1765 let int_size = match code {
1766 'b' | 'B' => return Some(Self::Int8 { signed }),
1767 'u' => {
1768 return match size_of::<wchar_t>() {
1769 2 => Some(Self::Utf16 { big_endian }),
1770 4 => Some(Self::Utf32 { big_endian }),
1771 _ => None,
1772 };
1773 }
1774 'w' => return Some(Self::Utf32 { big_endian }),
1775 'f' => {
1776 const Y: f32 = 16711938.0;
1778 return match &Y.to_ne_bytes() {
1779 b"\x4b\x7f\x01\x02" => Some(Self::Ieee754Float { big_endian: true }),
1780 b"\x02\x01\x7f\x4b" => Some(Self::Ieee754Float { big_endian: false }),
1781 _ => None,
1782 };
1783 }
1784 'd' => {
1785 const Y: f64 = 9006104071832581.0;
1787 return match &Y.to_ne_bytes() {
1788 b"\x43\x3f\xff\x01\x02\x03\x04\x05" => {
1789 Some(Self::Ieee754Double { big_endian: true })
1790 }
1791 b"\x05\x04\x03\x02\x01\xff\x3f\x43" => {
1792 Some(Self::Ieee754Double { big_endian: false })
1793 }
1794 _ => None,
1795 };
1796 }
1797 _ => ArrayContentType::itemsize_of_typecode(code)? as u8,
1798 };
1799 match int_size {
1800 2 => Some(Self::Int16 { signed, big_endian }),
1801 4 => Some(Self::Int32 { signed, big_endian }),
1802 8 => Some(Self::Int64 { signed, big_endian }),
1803 _ => None,
1804 }
1805 }
1806 const fn item_size(self) -> usize {
1807 match self {
1808 Self::Int8 { .. } => 1,
1809 Self::Int16 { .. } | Self::Utf16 { .. } => 2,
1810 Self::Int32 { .. } | Self::Utf32 { .. } | Self::Ieee754Float { .. } => 4,
1811 Self::Int64 { .. } | Self::Ieee754Double { .. } => 8,
1812 }
1813 }
1814 }
1815
1816 fn check_array_type(typ: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyTypeRef> {
1817 if !typ.fast_issubclass(PyArray::class(&vm.ctx)) {
1818 return Err(
1819 vm.new_type_error(format!("{} is not a subtype of array.array", typ.name()))
1820 );
1821 }
1822 Ok(typ)
1823 }
1824
1825 fn check_type_code(spec: &Py<PyUtf8Str>, vm: &VirtualMachine) -> PyResult<ArrayContentType> {
1826 let spec = spec.as_str().chars().exactly_one().map_err(|_| {
1827 vm.new_type_error(
1828 "_array_reconstructor() argument 2 must be a unicode character, not str",
1829 )
1830 })?;
1831 ArrayContentType::from_char(spec)
1832 .map_err(|_| vm.new_value_error("second argument must be a valid type code"))
1833 }
1834
1835 macro_rules! chunk_to_obj {
1836 ($BYTE:ident, $TY:ty, $BIG_ENDIAN:ident) => {{
1837 let b = <[u8; ::core::mem::size_of::<$TY>()]>::try_from($BYTE).unwrap();
1838 if $BIG_ENDIAN {
1839 <$TY>::from_be_bytes(b)
1840 } else {
1841 <$TY>::from_le_bytes(b)
1842 }
1843 }};
1844 ($VM:ident, $BYTE:ident, $TY:ty, $BIG_ENDIAN:ident) => {
1845 chunk_to_obj!($BYTE, $TY, $BIG_ENDIAN).to_pyobject($VM)
1846 };
1847 ($VM:ident, $BYTE:ident, $SIGNED_TY:ty, $UNSIGNED_TY:ty, $SIGNED:ident, $BIG_ENDIAN:ident) => {{
1848 let b = <[u8; ::core::mem::size_of::<$SIGNED_TY>()]>::try_from($BYTE).unwrap();
1849 match ($SIGNED, $BIG_ENDIAN) {
1850 (false, false) => <$UNSIGNED_TY>::from_le_bytes(b).to_pyobject($VM),
1851 (false, true) => <$UNSIGNED_TY>::from_be_bytes(b).to_pyobject($VM),
1852 (true, false) => <$SIGNED_TY>::from_le_bytes(b).to_pyobject($VM),
1853 (true, true) => <$SIGNED_TY>::from_be_bytes(b).to_pyobject($VM),
1854 }
1855 }};
1856 }
1857
1858 #[pyfunction]
1859 fn _array_reconstructor(args: ReconstructorArgs, vm: &VirtualMachine) -> PyResult<PyArrayRef> {
1860 let cls = check_array_type(args.arraytype, vm)?;
1861 let mut array = check_type_code(&args.typecode, vm)?;
1862 let format = args.mformat_code;
1863 let bytes = args.items.as_bytes();
1864 if !bytes.len().is_multiple_of(format.item_size()) {
1865 return Err(vm.new_value_error("bytes length not a multiple of item size"));
1866 }
1867 if MachineFormatCode::from_typecode(array.typecode()) == Some(format) {
1868 array.frombytes(bytes);
1869 return PyArray::from(array).into_ref_with_type(vm, cls);
1870 }
1871 if !matches!(
1872 format,
1873 MachineFormatCode::Utf16 { .. } | MachineFormatCode::Utf32 { .. }
1874 ) {
1875 array.reserve(bytes.len() / format.item_size());
1876 }
1877 let mut chunks = bytes.chunks(format.item_size());
1878 match format {
1879 MachineFormatCode::Ieee754Float { big_endian } => {
1880 chunks.try_for_each(|b| array.push(chunk_to_obj!(vm, b, f32, big_endian), vm))?
1881 }
1882 MachineFormatCode::Ieee754Double { big_endian } => {
1883 chunks.try_for_each(|b| array.push(chunk_to_obj!(vm, b, f64, big_endian), vm))?
1884 }
1885 MachineFormatCode::Int8 { signed } => chunks
1886 .try_for_each(|b| array.push(chunk_to_obj!(vm, b, i8, u8, signed, false), vm))?,
1887 MachineFormatCode::Int16 { signed, big_endian } => chunks.try_for_each(|b| {
1888 array.push(chunk_to_obj!(vm, b, i16, u16, signed, big_endian), vm)
1889 })?,
1890 MachineFormatCode::Int32 { signed, big_endian } => chunks.try_for_each(|b| {
1891 array.push(chunk_to_obj!(vm, b, i32, u32, signed, big_endian), vm)
1892 })?,
1893 MachineFormatCode::Int64 { signed, big_endian } => chunks.try_for_each(|b| {
1894 array.push(chunk_to_obj!(vm, b, i64, u64, signed, big_endian), vm)
1895 })?,
1896 MachineFormatCode::Utf16 { big_endian } => {
1897 let utf16: Vec<_> = chunks.map(|b| chunk_to_obj!(b, u16, big_endian)).collect();
1898 let s = String::from_utf16(&utf16).map_err(|_| {
1899 let (index, reason) = invalid_utf16(&utf16).unwrap();
1900 vm.new_unicode_decode_error(
1901 vm.ctx
1902 .new_str(if big_endian { "utf-16-be" } else { "utf-16-le" }),
1903 args.items.clone(),
1904 index * 2,
1905 index * 2 + 2,
1906 vm.ctx.new_str(reason),
1907 )
1908 })?;
1909 let bytes = PyArray::_unicode_to_wchar_bytes((*s).as_ref(), array.itemsize());
1910 array.frombytes_move(bytes);
1911 }
1912 MachineFormatCode::Utf32 { big_endian } => {
1913 let s: Wtf8Buf = chunks
1914 .map(|b| chunk_to_obj!(b, u32, big_endian))
1915 .map(|ch| u32_to_char(ch).map_err(|msg| vm.new_value_error(msg)))
1916 .try_collect()?;
1917 let bytes = PyArray::_unicode_to_wchar_bytes(&s, array.itemsize());
1918 array.frombytes_move(bytes);
1919 }
1920 };
1921 PyArray::from(array).into_ref_with_type(vm, cls)
1922 }
1923
1924 fn invalid_utf16(units: &[u16]) -> Option<(usize, &'static str)> {
1925 let mut index = 0;
1926 while index < units.len() {
1927 let unit = units[index];
1928 if (0xd800..=0xdbff).contains(&unit) {
1929 match units.get(index + 1) {
1930 Some(next) if (0xdc00..=0xdfff).contains(next) => index += 2,
1931 Some(_) => return Some((index, "illegal UTF-16 surrogate")),
1932 None => return Some((index, "unexpected end of data")),
1933 }
1934 } else if (0xdc00..=0xdfff).contains(&unit) {
1935 return Some((index, "illegal encoding"));
1936 } else {
1937 index += 1;
1938 }
1939 }
1940 None
1941 }
1942
1943 pub(crate) fn module_exec(
1945 vm: &VirtualMachine,
1946 module: &Py<crate::vm::builtins::PyModule>,
1947 ) -> PyResult<()> {
1948 __module_exec(vm, module);
1949
1950 let array_type = module
1951 .get_attr("array", vm)
1952 .expect("array module has array type");
1953
1954 let collections_abc = vm.import("collections.abc", 0)?;
1956 let abc = collections_abc.get_attr("abc", vm)?;
1957 let mutable_sequence = abc.get_attr("MutableSequence", vm)?;
1958 let register = mutable_sequence.get_attr("register", vm)?;
1959 register.call((array_type,), vm)?;
1960
1961 Ok(())
1962 }
1963}