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

Method get

src/npyffi/array.rs:86–144  ·  view source on GitHub ↗
(&self, py: Python<'py>, offset: isize)

Source from the content-addressed store, hash-verified

84
85impl PyArrayAPI {
86 pub(super) unsafe fn get<'py>(&self, py: Python<'py>, offset: isize) -> NonNull<*const c_void> {
87 let api = self
88 .0
89 .get_or_try_init(py, || -> PyResult<_> {
90 let api = get_numpy_api(py, mod_name(py)?, CAPSULE_NAME)?;
91
92 let module_version = {
93 // unsigned int PyArray_GetNDArrayCVersion();
94 let get_abi_version: extern "C" fn() -> c_uint =
95 api.add(0).cast().read();
96 get_abi_version()
97 };
98 if NPY_VERSION < module_version {
99 return Err(PyRuntimeError::new_err(format!(
100 "module compiled against ABI version 0x{:x} but this version of numpy is 0x{:x}",
101 NPY_VERSION, module_version
102 )));
103 }
104
105 let module_feature_version = unsafe {
106 // unsigned int PyArray_GetNDArrayCFeatureVersion();
107 let get_runtime_version: extern "C" fn() -> c_uint =
108 api.add(211).cast().read();
109 get_runtime_version()
110 };
111 if NPY_FEATURE_VERSION > module_feature_version {
112 return Err(PyRuntimeError::new_err(format!(
113 "module was compiled against NumPy C-API version 0x{:x} (NumPy {}) but the running NumPy has C-API version 0x{:x}",
114 NPY_FEATURE_VERSION, NPY_FEATURE_VERSION_STRING, module_feature_version
115 )));
116 }
117
118 let endianness = unsafe {
119 // int PyArray_GetEndianness();
120 let get_endianness: extern "C" fn() -> c_int =
121 api.add(210).cast().read();
122 get_endianness()
123 };
124
125 #[cfg(target_endian = "big")]
126 if endianness != NPY_CPU_BIG {
127 return Err(PyRuntimeError::new_err(
128 "module compiled as big endian, but detected different endianness at runtime",
129 ));
130 }
131
132 #[cfg(target_endian = "little")]
133 if endianness != NPY_CPU_LITTLE {
134 return Err(PyRuntimeError::new_err(
135 "module compiled as little endian, but detected different endianness at runtime",
136 ));
137 }
138
139 Ok(api)
140 })
141 .expect("Failed to access NumPy array API capsule");
142
143 api.offset(offset)

Callers

nothing calls this directly

Calls 3

get_numpy_apiFunction · 0.85
castMethod · 0.80
mod_nameFunction · 0.70

Tested by

no test coverage detected