mylibrary/
lib.rs

1use libc;
2use python3_sys::{METH_VARARGS, Py_BuildValue, PyArg_ParseTuple, PyMethodDef, PyModule_Create, PyModuleDef, PyModuleDef_HEAD_INIT, PyObject, PyCapsule_GetPointer};
3use crate::npyffi::{PyArrayObject, NPY_ARRAY_CARRAY, PyArray_Sort};
4use core::slice;
5use std::os::raw::{c_void};
6use std::ptr::null_mut;
7use std::time::Instant;
8
9pub mod npyffi;
10
11
12#[inline]
13const fn c_str(value: &str) -> *const libc::c_char {
14    value.as_ptr() as *const libc::c_char
15}
16
17unsafe extern "C" fn sum_as_string_py(_: *mut PyObject, args: *mut PyObject) -> *mut PyObject {
18    let a: i64 = 0;
19    let b: i64 = 0;
20    if PyArg_ParseTuple(args, c_str("ii\0"), &a, &b) == 0 {
21        return 0 as *mut _;
22    }
23    println!("Values after: {0}, {1}", a, b);
24    let result: f64 = (&a + &b) as f64 + 0.1;
25    println!("Values result: {0}", result);
26    Py_BuildValue(c_str("d\0"), result)
27}
28
29
30unsafe extern "C" fn try_numpy(_: *mut PyObject, args: *mut PyObject) -> *mut PyObject {
31    let a: i64 = 0;
32    let py_array: *mut PyArrayObject = 0 as *mut PyArrayObject;
33    let capsule: *mut PyObject = null_mut();
34
35    println!("Values before: {0}", a);
36    if PyArg_ParseTuple(args, c_str("iOO\0"), &a, &py_array, &capsule) == 0 {
37        println!("Values return: {0}, {1}", a, a);
38        /*if PyArray_Check(py_object_ptr as *mut PyObject) == 0 {
39            println!("py array check return: {0}, {1}", a, a);
40        }*/
41        return null_mut();
42    }
43
44    let now = Instant::now();
45
46
47    let pointer = PyCapsule_GetPointer(capsule, null_mut()) as *const *const c_void;
48
49    let numpy = npyffi::NumpyModuleReference::from(capsule);
50
51
52
53    /*
54    module.PyArray_Sort(py_array, 0, npyffi::types::NPY_SORTKIND::NPY_QUICKSORT);
55    module.PyArray_Sort(py_array, 0, npyffi::types::NPY_SORTKIND::NPY_QUICKSORT);
56    */
57
58    let secs = now.elapsed().as_secs_f64();
59    println!("secs: {}", secs);
60
61    (numpy.PyArray_Sort)(py_array, 0, npyffi::types::NPY_SORTKIND::NPY_QUICKSORT);
62    numpy.PyArray_Sort2(py_array, 0, npyffi::types::NPY_SORTKIND::NPY_QUICKSORT);
63
64    let py_object = *py_array;
65
66    let descr = *py_object.descr;
67    let data = py_object.data as *mut i64;
68
69    let sli = slice::from_raw_parts_mut(data, 3);
70    println!("len: {}", sli.len());
71    for item in sli {
72        println!("item: {}", item);
73    }
74
75    println!("Values after: {0}, nd={1}, type_={2}", a, py_object.nd, descr.type_num);
76
77
78
79    let arr = (numpy.PyArray_New)(numpy.PyArray_Type, 1, py_object.dimensions, 7, 0 as *mut isize, (*py_array).data as *mut c_void, 0, NPY_ARRAY_CARRAY, 0 as *mut _);
80
81    Py_BuildValue(c_str("O\0"), arr)
82}
83
84const METHODS: [PyMethodDef; 3] = [
85    PyMethodDef {
86        ml_name: c_str("sum_as_string\0"),
87        ml_meth: Some(sum_as_string_py),
88        ml_flags: METH_VARARGS,
89        ml_doc: c_str("...\0"),
90    },
91    PyMethodDef {
92        ml_name: c_str("try_numpy\0"),
93        ml_meth: Some(try_numpy),
94        ml_flags: METH_VARARGS,
95        ml_doc: c_str("...\0"),
96    },
97    PyMethodDef {
98        ml_name: 0 as *const libc::c_char,
99        ml_meth: None,
100        ml_flags: 0,
101        ml_doc: 0 as *const libc::c_char,
102    }
103];
104
105static mut DEFINITION: PyModuleDef = PyModuleDef {
106    m_base: PyModuleDef_HEAD_INIT,
107    m_name: c_str("_mylibrary\0"),
108    m_doc: c_str("...\0"),
109    m_size: 0,
110    m_methods: METHODS.as_ptr() as *mut _,
111    m_slots: 0 as *mut _,
112    m_traverse: None,
113    m_clear: None,
114    m_free: None,
115};
116
117pub fn my_function() -> i64 {
118    4
119}
120
121#[no_mangle]
122#[allow(non_snake_case)]
123pub unsafe extern "C" fn PyInit__mylibrary() -> *mut PyObject {
124    PyModule_Create(&mut DEFINITION)
125}
126
127#[cfg(test)]
128mod tests {
129    use crate::my_function;
130
131    #[test]
132    fn it_works() {
133        assert_eq!(2 + 2, my_function())
134    }
135}