MCPcopy Create free account
hub / github.com/PyO3/rust-numpy / extract

Method extract

src/array.rs:144–179  ·  view source on GitHub ↗
(
        ob: &'a Bound<'py, PyAny>,
        check: unsafe fn(Python<'py>, *mut ffi::PyObject) -> c_int,
    )

Source from the content-addressed store, hash-verified

142
143impl<T: Element, D: Dimension> PyArray<T, D> {
144 fn extract<'a, 'py, E>(
145 ob: &'a Bound<'py, PyAny>,
146 check: unsafe fn(Python<'py>, *mut ffi::PyObject) -> c_int,
147 ) -> Result<&'a Bound<'py, Self>, E>
148 where
149 E: From<CastError<'a, 'py>> + From<DimensionalityError> + From<TypeError<'py>>,
150 {
151 // Check if the object is an array.
152 let array = unsafe {
153 if check(ob.py(), ob.as_ptr()) == 0 {
154 return Err(CastError::new(
155 ob.as_borrowed(),
156 <Self as PyTypeCheck>::classinfo_object(ob.py()),
157 )
158 .into());
159 }
160 ob.cast_unchecked::<Self>()
161 };
162
163 // Check if the dimensionality matches `D`.
164 let src_ndim = array.ndim();
165 if let Some(dst_ndim) = D::NDIM {
166 if src_ndim != dst_ndim {
167 return Err(DimensionalityError::new(src_ndim, dst_ndim).into());
168 }
169 }
170
171 // Check if the element type matches `T`.
172 let src_dtype = array.dtype();
173 let dst_dtype = T::get_dtype(ob.py());
174 if !src_dtype.is_equiv_to(&dst_dtype) {
175 return Err(TypeError::new(src_dtype, dst_dtype).into());
176 }
177
178 Ok(array)
179 }
180
181 /// Creates a new uninitialized NumPy array.
182 ///

Callers 9

extract_array_like_1Function · 0.45
extract_array_like_2Function · 0.45
extract_array_like_3Function · 0.45
innerFunction · 0.45
dotFunction · 0.45
einsumFunction · 0.45
shapeMethod · 0.45
namesMethod · 0.45
get_fieldMethod · 0.45

Calls 4

checkFunction · 0.85
dtypeMethod · 0.80
is_equiv_toMethod · 0.80
ndimMethod · 0.45

Tested by

no test coverage detected