|
1 | 1 | //! Safe interface for NumPy ndarray
|
2 |
| -use crate::npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API}; |
3 |
| -use ndarray::*; |
4 |
| -use num_traits::AsPrimitive; |
5 |
| -use pyo3::{ |
6 |
| - ffi, prelude::*, pyobject_native_type_info, pyobject_native_type_named, type_object, |
7 |
| - types::PyAny, AsPyPointer, PyDowncastError, PyNativeType, PyResult, |
8 |
| -}; |
9 | 2 | use std::{
|
10 | 3 | cell::Cell,
|
| 4 | + marker::PhantomData, |
11 | 5 | mem,
|
12 | 6 | os::raw::{c_int, c_void},
|
13 | 7 | ptr, slice,
|
14 | 8 | };
|
15 |
| -use std::{iter::ExactSizeIterator, marker::PhantomData}; |
| 9 | + |
| 10 | +use ndarray::{ |
| 11 | + Array, ArrayBase, ArrayView, ArrayViewMut, Axis, Data, Dim, Dimension, IntoDimension, Ix0, Ix1, |
| 12 | + Ix2, Ix3, Ix4, Ix5, Ix6, IxDyn, RawData, Shape, ShapeBuilder, StrideShape, |
| 13 | +}; |
| 14 | +use num_traits::AsPrimitive; |
| 15 | +use pyo3::{ |
| 16 | + ffi, pyobject_native_type_info, pyobject_native_type_named, type_object, types::PyModule, |
| 17 | + AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyDowncastError, PyErr, PyNativeType, PyObject, |
| 18 | + PyResult, Python, ToPyObject, |
| 19 | +}; |
16 | 20 |
|
17 | 21 | use crate::convert::{ArrayExt, IntoPyArray, NpyIndex, ToNpyDims, ToPyArray};
|
18 | 22 | use crate::dtype::Element;
|
19 | 23 | use crate::error::{DimensionalityError, FromVecError, NotContiguousError, TypeError};
|
| 24 | +use crate::npyffi::{self, npy_intp, NPY_ORDER, PY_ARRAY_API}; |
20 | 25 | use crate::slice_container::PySliceContainer;
|
21 | 26 |
|
22 | 27 | /// A safe, static-typed interface for
|
@@ -919,10 +924,15 @@ impl<T: Element> PyArray<T, Ix1> {
|
919 | 924 | // NumPy will always zero-initialize object pointers,
|
920 | 925 | // so the array can be dropped safely if the iterator panics.
|
921 | 926 | unsafe {
|
922 |
| - let array = Self::new(py, [iter.len()], false); |
923 |
| - for (i, item) in iter.enumerate() { |
924 |
| - array.uget_raw([i]).write(item); |
| 927 | + let len = iter.len(); |
| 928 | + let array = Self::new(py, [len], false); |
| 929 | + let mut idx = 0; |
| 930 | + for item in iter { |
| 931 | + assert!(idx < len); |
| 932 | + array.uget_raw([idx]).write(item); |
| 933 | + idx += 1; |
925 | 934 | }
|
| 935 | + assert!(idx == len); |
926 | 936 | array
|
927 | 937 | }
|
928 | 938 | }
|
|
0 commit comments