2015-04-17 20:13:15 +00:00
|
|
|
#![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-04-18 20:25:03 +00:00
|
|
|
#![allow(unused_imports, unused_variables)]
|
2015-01-05 16:05:53 +00:00
|
|
|
|
2015-04-18 20:20:19 +00:00
|
|
|
//! Rust bindings to the python interpreter.
|
|
|
|
//!
|
2015-04-18 22:39:04 +00:00
|
|
|
//! # Ownership and Lifetimes
|
|
|
|
//! In python, all objects are implicitly reference counted.
|
|
|
|
//! In rust, we will use the `PyObject` type to represent a reference to a python object.
|
|
|
|
//!
|
|
|
|
//! Because all python objects potentially have multiple owners, the concept
|
|
|
|
//! concept of rust mutability does not apply to python objects.
|
|
|
|
//! As a result, this API will allow mutating python objects even if they are not stored
|
|
|
|
//! in a mutable rust variable.
|
|
|
|
//!
|
|
|
|
//! The python interpreter uses a global interpreter lock (GIL)
|
|
|
|
//! to ensure thread-safety.
|
|
|
|
//! This API uses the lifetime parameter `PyObject<'p>` to ensure that python objects cannot
|
|
|
|
//! be accessed without holding the GIL.
|
|
|
|
//! Throughout this library, the lifetime `'p` always refers to the lifetime of the python interpreter.
|
|
|
|
//!
|
|
|
|
//! When accessing existing objects, the lifetime on `PyObject<'p>` is sufficient to ensure that the GIL
|
|
|
|
//! is held by the current code. But we also need to ensure that the GIL is held when creating new objects.
|
|
|
|
//! For this purpose, this library uses the marker type `Python<'p>`,
|
|
|
|
//! which acts like a reference to the whole python interpreter.
|
|
|
|
//!
|
|
|
|
//! You can obtain a `Python<'p>` instance by acquiring the GIL, or by calling `python()`
|
|
|
|
//! on any existing python object.
|
|
|
|
//!
|
|
|
|
//! # Error Handling
|
|
|
|
//! The vast majority of operations in this library will return `PyResult<'p, ...>`.
|
|
|
|
//! This is an alias for the type `Result<..., PyErr<'p>>`.
|
|
|
|
//!
|
|
|
|
//! A `PyErr` represents a python exception. Errors within the rust-cpython library are
|
|
|
|
//! also exposed as python exceptions.
|
|
|
|
//!
|
2015-04-18 20:20:19 +00:00
|
|
|
//! # Example
|
|
|
|
//! ```
|
2015-04-18 22:39:04 +00:00
|
|
|
//! extern crate cpython;
|
2015-04-18 20:20:19 +00:00
|
|
|
//!
|
2015-04-18 22:39:04 +00:00
|
|
|
//! use cpython::{PythonObject, Python};
|
2015-04-18 20:20:19 +00:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
2015-04-18 22:39:04 +00:00
|
|
|
//! let gil_guard = Python::acquire_gil();
|
|
|
|
//! let py = gil_guard.python();
|
2015-04-18 20:20:19 +00:00
|
|
|
//! let sys = py.import("sys").unwrap();
|
2015-04-18 22:39:04 +00:00
|
|
|
//! let version = sys.get("version").unwrap().extract::<String>().unwrap();
|
2015-04-18 20:20:19 +00:00
|
|
|
//! println!("Hello Python {}", version);
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2015-01-05 16:05:53 +00:00
|
|
|
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-04-18 20:20:19 +00:00
|
|
|
pub use python::{Python, PythonObject, PythonObjectWithCheckedDowncast, PythonObjectWithTypeObject, ToPythonPointer};
|
2015-04-18 18:17:25 +00:00
|
|
|
pub use pythonrun::{GILGuard, prepare_freethreaded_python};
|
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
|
|
|
|
2015-04-18 20:20:19 +00:00
|
|
|
/// Constructs a `&'static CStr` literal.
|
2015-03-08 14:29:44 +00:00
|
|
|
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;
|
2015-01-04 23:07:31 +00:00
|
|
|
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-04-18 20:20:19 +00:00
|
|
|
/// Private re-exports for macros. Do not use.
|
2015-04-18 22:39:04 +00:00
|
|
|
#[doc(hidden)]
|
2015-04-18 18:17:25 +00:00
|
|
|
pub mod _detail {
|
|
|
|
pub use ffi;
|
|
|
|
pub use libc;
|
|
|
|
pub use err::from_owned_ptr_or_panic;
|
|
|
|
}
|
|
|
|
|
2015-04-18 20:20:19 +00:00
|
|
|
/// Expands to an `extern "C"` function that allows python to load
|
|
|
|
/// the rust code as a python extension module.
|
|
|
|
///
|
|
|
|
/// The macro takes three arguments:
|
|
|
|
///
|
|
|
|
/// 1. The module name as a string literal.
|
|
|
|
/// 2. The name of the init function as an identifier.
|
|
|
|
/// The function must be named `init$module_name` so that python 2.7 can load the module.
|
|
|
|
/// Note: this parameter will be removed in a future version
|
|
|
|
/// (once Rust supports `concat_ident!` as function name).
|
|
|
|
/// 3. A function or lambda of type `Fn(Python<'p>, &PyModule<'p>) -> PyResult<'p, ()>`.
|
|
|
|
/// This function will be called when the module is imported, and is responsible
|
|
|
|
/// for adding the module's members.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// #![crate_type = "dylib"]
|
|
|
|
/// #[macro_use] extern crate cpython;
|
|
|
|
/// use cpython::{Python, PyResult, PyObject, PyTuple};
|
|
|
|
///
|
|
|
|
/// py_module_initializer!("example", initexample, |py, m| {
|
|
|
|
/// try!(m.add("__doc__", "Module documentation string"));
|
|
|
|
/// try!(m.add("run", py_func!(py, run)));
|
|
|
|
/// Ok(())
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// fn run<'p>(py: Python<'p>, args: &PyTuple<'p>) -> PyResult<'p, PyObject<'p>> {
|
|
|
|
/// println!("Rust says: Hello Python!");
|
|
|
|
/// Ok(py.None())
|
|
|
|
/// }
|
|
|
|
/// # fn main() {}
|
|
|
|
/// ```
|
|
|
|
/// The code must be compiled into a file `example.so`.
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// rustc example.rs -o example.so
|
|
|
|
/// ```
|
|
|
|
/// It can then be imported into python:
|
|
|
|
///
|
|
|
|
/// ```python
|
|
|
|
/// >>> import example
|
|
|
|
/// >>> example.run()
|
|
|
|
/// Rust says: Hello Python!
|
|
|
|
/// ```
|
|
|
|
///
|
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-18 20:20:19 +00:00
|
|
|
let name = unsafe { ::std::ffi::CStr::from_ptr(concat!($name, "\0").as_ptr() as *const _) };
|
|
|
|
match $crate::PyModule::_init(py, name, $init) {
|
2015-01-12 02:00:34 +00:00
|
|
|
Ok(()) => (),
|
|
|
|
Err(e) => e.restore()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 22:39:04 +00:00
|
|
|
/// Creates a python callable object that invokes a Rust function.
|
|
|
|
///
|
|
|
|
/// Arguments:
|
|
|
|
///
|
|
|
|
/// 1. The `Python<'p>` marker, to ensure this macro is only used while holding the GIL.
|
|
|
|
/// 2. A Rust function with the signature `<'p>(Python<'p>, &PyTuple<'p>) -> PyResult<'p, T>`
|
|
|
|
/// for some `T` that implements `ToPyObject`.
|
|
|
|
///
|
|
|
|
/// See `py_module_initializer!` for example usage.
|
|
|
|
///
|
|
|
|
/// # Panic
|
|
|
|
/// May panic when python runs out of memory.
|
2015-04-18 18:17:25 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! py_func {
|
|
|
|
($py: expr, $f: expr) => ({
|
|
|
|
unsafe extern "C" fn wrap_py_func
|
|
|
|
(_slf: *mut $crate::_detail::ffi::PyObject, args: *mut $crate::_detail::ffi::PyObject)
|
|
|
|
-> *mut $crate::_detail::ffi::PyObject {
|
|
|
|
let py = $crate::Python::assume_gil_acquired();
|
|
|
|
let args = $crate::PyObject::from_borrowed_ptr(py, args);
|
2015-04-18 20:20:19 +00:00
|
|
|
let args: &$crate::PyTuple = $crate::PythonObject::unchecked_downcast_borrow_from(&args);
|
2015-04-18 23:07:14 +00:00
|
|
|
match $f(py, args) {
|
|
|
|
Ok(val) => {
|
|
|
|
let obj = $crate::ToPyObject::into_py_object(val, py);
|
|
|
|
return $crate::ToPythonPointer::steal_ptr(obj);
|
|
|
|
}
|
2015-04-18 22:39:04 +00:00
|
|
|
Err(e) => {
|
|
|
|
e.restore();
|
2015-04-18 23:07:14 +00:00
|
|
|
return ::std::ptr::null_mut();
|
2015-04-18 22:39:04 +00:00
|
|
|
}
|
2015-04-18 18:17:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
static mut method_def: $crate::_detail::ffi::PyMethodDef = $crate::_detail::ffi::PyMethodDef {
|
|
|
|
//ml_name: bytes!(stringify!($f), "\0"),
|
|
|
|
ml_name: b"<rust function>\0" as *const u8 as *const $crate::_detail::libc::c_char,
|
|
|
|
ml_meth: Some(wrap_py_func),
|
|
|
|
ml_flags: $crate::_detail::ffi::METH_VARARGS,
|
|
|
|
ml_doc: 0 as *const $crate::_detail::libc::c_char
|
|
|
|
};
|
|
|
|
let py: Python = $py;
|
|
|
|
unsafe {
|
|
|
|
let obj = $crate::_detail::ffi::PyCFunction_New(&mut method_def, ::std::ptr::null_mut());
|
|
|
|
$crate::_detail::from_owned_ptr_or_panic(py, obj)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|