pyo3/src/lib.rs

59 lines
1.8 KiB
Rust
Raw Normal View History

#![feature(core)] // used for a lot of low-level stuff
#![feature(unsafe_no_drop_flag)] // crucial so that PyObject<'p> is binary compatible with *mut ffi::PyObject
#![feature(filling_drop)] // necessary to avoid segfault with unsafe_no_drop_flag
#![feature(optin_builtin_traits)] // for opting out of Sync/Send
#![feature(slice_patterns)] // for tuple_conversion macros
#![feature(utf8_error)] // for translating Utf8Error to python exception
2015-01-05 16:05:53 +00:00
#![allow(unused_imports, dead_code, unused_variables)]
extern crate libc;
2015-03-09 13:31:20 +00:00
extern crate python27_sys as ffi;
2015-01-05 16:05:53 +00:00
pub use ffi::Py_ssize_t;
pub use err::{PyErr, PyResult};
2015-01-05 16:02:30 +00:00
pub use objects::*;
2015-01-05 15:34:12 +00:00
pub use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject};
2015-01-05 16:05:53 +00:00
pub use conversion::{FromPyObject, ToPyObject};
2015-01-04 20:37:43 +00:00
pub use objectprotocol::{ObjectProtocol};
2015-03-08 14:29:44 +00:00
pub use std::ffi::CStr;
#[macro_export]
macro_rules! cstr(
($s: tt) => (
// TODO: verify that $s is a string literal without nuls
unsafe {
::std::ffi::CStr::from_ptr(concat!($s, "\0").as_ptr() as *const _)
}
);
);
2015-01-05 16:05:53 +00:00
mod python;
mod err;
mod conversion;
2015-01-05 16:02:30 +00:00
mod objects;
2015-01-04 20:37:43 +00:00
mod objectprotocol;
2015-01-05 16:05:53 +00:00
mod pythonrun;
2015-01-12 02:00:34 +00:00
#[macro_export]
macro_rules! py_module_initializer {
($name: tt, $init_funcname: ident, $init: expr) => {
#[no_mangle]
pub extern "C" fn $init_funcname() {
let py = unsafe { $crate::Python::assume_gil_acquired() };
2015-04-17 23:52:48 +00:00
match $crate::PyModule::_init(py, cstr!($name), $init) {
2015-01-12 02:00:34 +00:00
Ok(()) => (),
Err(e) => e.restore()
}
}
}
}
2015-01-05 16:05:53 +00:00
#[test]
fn it_works() {
2015-01-04 04:50:28 +00:00
let gil = Python::acquire_gil();
let py = gil.python();
2015-01-11 04:52:11 +00:00
let sys = PyModule::import(py, cstr!("sys")).unwrap();
2015-01-05 16:05:53 +00:00
let path = sys.as_object().getattr("path").unwrap();
println!("{0}", path);
}