| 1 |
use pyo3_ffi::*; |
| 2 |
use std::os::raw::c_char; |
| 3 |
|
| 4 |
#[allow(non_snake_case)] |
| 5 |
#[no_mangle] |
| 6 |
pub unsafe extern "C" fn PyInit_pyo3_ffi_pure() -> *mut PyObject { |
| 7 |
let module_name = "pyo3_ffi_pure\0".as_ptr() as *const c_char; |
| 8 |
let init = PyModuleDef { |
| 9 |
m_base: PyModuleDef_HEAD_INIT, |
| 10 |
m_name: module_name, |
| 11 |
m_doc: std::ptr::null(), |
| 12 |
m_size: 0, |
| 13 |
m_methods: std::ptr::null_mut(), |
| 14 |
m_slots: std::ptr::null_mut(), |
| 15 |
m_traverse: None, |
| 16 |
m_clear: None, |
| 17 |
m_free: None, |
| 18 |
}; |
| 19 |
let mptr = PyModule_Create(Box::into_raw(Box::new(init))); |
| 20 |
|
| 21 |
let wrapped_sum = PyMethodDef { |
| 22 |
ml_name: "sum\0".as_ptr() as *const c_char, |
| 23 |
ml_meth: PyMethodDefPointer { |
| 24 |
PyCFunctionWithKeywords: sum, |
| 25 |
}, |
| 26 |
ml_flags: METH_VARARGS | METH_KEYWORDS, |
| 27 |
ml_doc: std::ptr::null_mut(), |
| 28 |
}; |
| 29 |
PyModule_AddObject( |
| 30 |
mptr, |
| 31 |
"sum\0".as_ptr() as *const c_char, |
| 32 |
PyCFunction_NewEx( |
| 33 |
Box::into_raw(Box::new(wrapped_sum)), |
| 34 |
std::ptr::null_mut(), |
| 35 |
PyUnicode_InternFromString(module_name), |
| 36 |
), |
| 37 |
); |
| 38 |
|
| 39 |
mptr |
| 40 |
} |
| 41 |
|
| 42 |
#[no_mangle] |
| 43 |
pub unsafe extern "C" fn sum( |
| 44 |
_self: *mut PyObject, |
| 45 |
args: *mut PyObject, |
| 46 |
_kwds: *mut PyObject, |
| 47 |
) -> *mut PyObject { |
| 48 |
// this is a minimal test of compilation, not good example code |
| 49 |
let val_a = PyTuple_GetItem(args, 0); |
| 50 |
let val_b = PyTuple_GetItem(args, 1); |
| 51 |
let res: i64 = PyLong_AsLongLong(val_a) + PyLong_AsLongLong(val_b); |
| 52 |
PyLong_FromLongLong(res) |
| 53 |
} |