Skip to content

Commit dd675ba

Browse files
committed
Unify implemenation of Array::from_raw_parts and ::borrow_from_array
Both create an array from existing data, they just differ w.r.t. how the how the owner on the Python heap is handled.
1 parent f24bbdc commit dd675ba

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/array.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -432,31 +432,26 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
432432
PY_ARRAY_API.get_type_object(npyffi::NpyTypes::PyArray_Type),
433433
dims.ndim_cint(),
434434
dims.as_dims_ptr(),
435-
T::npy_type() as i32,
436-
strides as *mut _, // strides
437-
ptr::null_mut(), // data
438-
0, // itemsize
439-
flag, // flag
440-
ptr::null_mut(), // obj
435+
T::npy_type() as c_int,
436+
strides as *mut npy_intp, // strides
437+
ptr::null_mut(), // data
438+
0, // itemsize
439+
flag, // flag
440+
ptr::null_mut(), // obj
441441
);
442442
Self::from_owned_ptr(py, ptr)
443443
}
444444

445-
pub(crate) unsafe fn from_raw_parts<'py, ID, O>(
445+
unsafe fn new_with_data<'py, ID>(
446446
py: Python<'py>,
447447
dims: ID,
448448
strides: *const npy_intp,
449449
data_ptr: *const T,
450-
owner: O,
450+
owner: *mut PyAny,
451451
) -> &'py Self
452452
where
453453
ID: IntoDimension<Dim = D>,
454-
Owner: From<O>,
455454
{
456-
let owner = pyo3::PyClassInitializer::from(Owner::from(owner))
457-
.create_cell(py)
458-
.expect("Object creation failed.");
459-
460455
let dims = dims.into_dimension();
461456
let ptr = PY_ARRAY_API.PyArray_New(
462457
PY_ARRAY_API.get_type_object(npyffi::NpyTypes::PyArray_Type),
@@ -478,6 +473,24 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
478473
Self::from_owned_ptr(py, ptr)
479474
}
480475

476+
pub(crate) unsafe fn from_raw_parts<'py, ID, O>(
477+
py: Python<'py>,
478+
dims: ID,
479+
strides: *const npy_intp,
480+
data_ptr: *const T,
481+
owner: O,
482+
) -> &'py Self
483+
where
484+
ID: IntoDimension<Dim = D>,
485+
Owner: From<O>,
486+
{
487+
let owner = pyo3::PyClassInitializer::from(Owner::from(owner))
488+
.create_cell(py)
489+
.expect("Object creation failed.");
490+
491+
Self::new_with_data(py, dims, strides, data_ptr, owner as *mut PyAny)
492+
}
493+
481494
/// Creates a NumPy array backed by `array` and ties its ownership to the Python object `owner`.
482495
///
483496
/// # Safety
@@ -515,26 +528,15 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
515528
let (strides, dims) = (array.npy_strides(), array.raw_dim());
516529
let data_ptr = array.as_ptr();
517530

518-
let ptr = PY_ARRAY_API.PyArray_New(
519-
PY_ARRAY_API.get_type_object(npyffi::NpyTypes::PyArray_Type),
520-
dims.ndim_cint(),
521-
dims.as_dims_ptr(),
522-
T::npy_type() as c_int,
523-
strides.as_ptr() as *mut npy_intp, // strides
524-
data_ptr as *mut c_void, // data
525-
mem::size_of::<T>() as c_int, // itemsize
526-
0, // flag
527-
ptr::null_mut(), // obj
528-
);
529-
530531
mem::forget(owner.to_object(owner.py()));
531532

532-
PY_ARRAY_API.PyArray_SetBaseObject(
533-
ptr as *mut npyffi::PyArrayObject,
534-
owner as *const PyAny as *mut PyAny as *mut ffi::PyObject,
535-
);
536-
537-
Self::from_owned_ptr(owner.py(), ptr)
533+
Self::new_with_data(
534+
owner.py(),
535+
dims,
536+
strides.as_ptr(),
537+
data_ptr,
538+
owner as *const PyAny as *mut PyAny,
539+
)
538540
}
539541

540542
/// Construct a new nd-dimensional array filled with 0.

0 commit comments

Comments
 (0)