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
use pyo3::ffi;
use std::ffi::CString;
use std::os::raw::c_void;
use std::ptr::null_mut;
fn get_numpy_api(module: &str, capsule: &str) -> *const *const c_void {
    let module = CString::new(module).unwrap();
    let capsule = CString::new(capsule).unwrap();
    unsafe fn get_capsule(capsule: *mut ffi::PyObject) -> *const *const c_void {
        ffi::PyCapsule_GetPointer(capsule, null_mut()) as *const *const c_void
    }
    unsafe {
        assert_ne!(
            ffi::Py_IsInitialized(),
            0,
            r"Numpy API is called before initializing Python!
Please make sure that you get gil, by `let gil = Python::acquire_gil();`"
        );
        let numpy = ffi::PyImport_ImportModule(module.as_ptr());
        assert!(!numpy.is_null(), "Failed to import numpy module");
        let capsule = ffi::PyObject_GetAttrString(numpy as *mut ffi::PyObject, capsule.as_ptr());
        assert!(!capsule.is_null(), "Failed to import numpy module");
        get_capsule(capsule)
    }
}
macro_rules! impl_api {
    [ $offset:expr; $fname:ident ( $($arg:ident : $t:ty),* ) $( -> $ret:ty )* ] => {
        #[allow(non_snake_case)]
        pub unsafe fn $fname(&self, $($arg : $t), *) $( -> $ret )* {
            let fptr = self.0.offset($offset)
                               as (*const extern fn ($($arg : $t), *) $( -> $ret )* );
            (*fptr)($($arg), *)
        }
    }
}
pub mod array;
pub mod objects;
pub mod types;
pub mod ufunc;
pub use self::array::*;
pub use self::objects::*;
pub use self::types::*;
pub use self::ufunc::*;