Skip to content

Commit e4e33ab

Browse files
committed
No need to explicitly request zero objects (which would yield valid non-null pointers to the None object).
1 parent 4a8f79c commit e4e33ab

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

src/array.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,9 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
447447
/// If `is_fortran` is true, then
448448
/// a fortran order array is created, otherwise a C-order array is created.
449449
///
450+
/// For elements with `DATA_TYPE == DataType::Object`, this will fill the array
451+
/// valid pointers to objects of type `<class 'int'>` with value zero.
452+
///
450453
/// See also [PyArray_Zeros](https://numpy.org/doc/stable/reference/c-api/array.html#c.PyArray_Zeros)
451454
///
452455
/// # Example
@@ -776,14 +779,9 @@ impl<T: Element> PyArray<T, Ix1> {
776779
/// });
777780
/// ```
778781
pub fn from_exact_iter(py: Python<'_>, iter: impl ExactSizeIterator<Item = T>) -> &Self {
779-
// Use zero-initialized pointers for object arrays
780-
// so that partially initialized arrays can be dropped safely
781-
// in case the iterator implementation panics.
782-
let array = if T::DATA_TYPE == DataType::Object {
783-
Self::zeros(py, [iter.len()], false)
784-
} else {
785-
Self::new(py, [iter.len()], false)
786-
};
782+
// NumPy will always zero-initialize object pointers,
783+
// so the array can be dropped safely if the iterator panics.
784+
let array = Self::new(py, [iter.len()], false);
787785
unsafe {
788786
for (i, item) in iter.enumerate() {
789787
*array.uget_mut([i]) = item;
@@ -811,14 +809,9 @@ impl<T: Element> PyArray<T, Ix1> {
811809
let iter = iter.into_iter();
812810
let (min_len, max_len) = iter.size_hint();
813811
let mut capacity = max_len.unwrap_or_else(|| min_len.max(512 / mem::size_of::<T>()));
814-
// Use zero-initialized pointers for object arrays
815-
// so that partially initialized arrays can be dropped safely
816-
// in case the iterator implementation panics.
817-
let array = if T::DATA_TYPE == DataType::Object {
818-
Self::zeros(py, [capacity], false)
819-
} else {
820-
Self::new(py, [capacity], false)
821-
};
812+
// NumPy will always zero-initialize object pointers,
813+
// so the array can be dropped safely if the iterator panics.
814+
let array = Self::new(py, [capacity], false);
822815
let mut length = 0;
823816
unsafe {
824817
for (i, item) in iter.enumerate() {

0 commit comments

Comments
 (0)