-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathmod.rs
59 lines (50 loc) · 1.5 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! Low-Level bindings for NumPy C API.
//!
//! <https://numpy.org/doc/stable/reference/c-api>
#![allow(
non_camel_case_types,
missing_docs,
missing_debug_implementations,
clippy::too_many_arguments,
clippy::missing_safety_doc
)]
use std::mem::forget;
use std::os::raw::c_void;
use pyo3::{
types::{PyCapsule, PyModule},
Py, PyResult, PyTryInto, Python,
};
fn get_numpy_api<'py>(
py: Python<'py>,
module: &str,
capsule: &str,
) -> PyResult<*const *const c_void> {
let module = PyModule::import(py, module)?;
let capsule: &PyCapsule = PyTryInto::try_into(module.getattr(capsule)?)?;
let api = capsule.pointer() as *const *const c_void;
// Intentionally leak a reference to the capsule
// so we can safely cache a pointer into its interior.
forget(Py::<PyCapsule>::from(capsule));
Ok(api)
}
// Implements wrappers for NumPy's Array and UFunc API
macro_rules! impl_api {
[$offset: expr; $fname: ident ( $($arg: ident : $t: ty),* $(,)?) $( -> $ret: ty )* ] => {
#[allow(non_snake_case)]
pub unsafe fn $fname<'py>(&self, py: Python<'py>, $($arg : $t), *) $( -> $ret )* {
let fptr = self.get(py, $offset)
as *const extern fn ($($arg : $t), *) $( -> $ret )*;
(*fptr)($($arg), *)
}
};
}
pub mod array;
pub mod flags;
pub mod objects;
pub mod types;
pub mod ufunc;
pub use self::array::*;
pub use self::flags::*;
pub use self::objects::*;
pub use self::types::*;
pub use self::ufunc::*;