Skip to content

Instantly share code, notes, and snippets.

@andreypopp
Created December 12, 2013 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreypopp/7936635 to your computer and use it in GitHub Desktop.
Save andreypopp/7936635 to your computer and use it in GitHub Desktop.
#[crate_type = "lib"];
#[allow(ctypes)];
use std::c_str;
use std::libc;
use std::ptr;
use std::vec;
struct PyObject;
struct PyMethodDef {
ml_name: *libc::c_char,
ml_meth: extern "C" fn(*PyObject, *PyObject) -> *PyObject,
ml_flags: libc::c_int,
ml_doc: *libc::c_char,
}
#[link(name = "dl")]
#[link(name = "CoreFoundation", kind = "framework")]
#[link(name = "python2.7")]
extern {
fn PyArg_ParseTuple(args: *PyObject, format: c_str::CString) -> libc::c_int;
fn Py_InitModule4_64(name: *libc::c_char,
methods: *PyMethodDef,
doc: *libc::c_char,
_self: *PyObject,
apiver: libc::c_int) -> *PyObject;
}
#[no_split_stack]
#[no_mangle]
pub extern fn add(module: *PyObject, args: *PyObject) -> *PyObject {
module
}
#[no_split_stack]
#[no_mangle]
pub extern fn initmylib() {
unsafe {
let methods = [
PyMethodDef {
ml_name: "add".to_c_str().unwrap(),
ml_meth: add,
ml_flags: 0x0001,
ml_doc: "Sum two numbers".to_c_str().unwrap()
},
PyMethodDef {
ml_name: ptr::null(),
ml_meth: add,
ml_flags: 0,
ml_doc: ptr::null()
}
];
Py_InitModule4_64(
"mylib".to_c_str().unwrap(),
vec::raw::to_ptr(methods),
ptr::null(),
ptr::null(),
1013);
}
return ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment