2017-06-02 16:23:48 +00:00
|
|
|
#![feature(specialization, const_fn, proc_macro)]
|
2016-03-06 12:33:57 +00:00
|
|
|
|
2015-06-27 20:45:35 +00:00
|
|
|
//! Rust bindings to the Python interpreter.
|
2015-04-18 20:20:19 +00:00
|
|
|
//!
|
2015-04-18 22:39:04 +00:00
|
|
|
//! # Ownership and Lifetimes
|
2015-06-27 20:45:35 +00:00
|
|
|
//! In Python, all objects are implicitly reference counted.
|
|
|
|
//! In rust, we will use the `PyObject` type to represent a reference to a Python object.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
2015-10-26 22:52:18 +00:00
|
|
|
//! The method `clone_ref()` (from trait `PyClone`) can be used to create additional
|
|
|
|
//! references to the same Python object.
|
|
|
|
//!
|
2016-01-29 03:13:39 +00:00
|
|
|
//! Because all Python objects potentially have multiple owners, the
|
2015-10-26 22:52:18 +00:00
|
|
|
//! concept of Rust mutability does not apply to Python objects.
|
2015-06-27 20:45:35 +00:00
|
|
|
//! As a result, this API will allow mutating Python objects even if they are not stored
|
2015-10-26 22:52:18 +00:00
|
|
|
//! in a mutable Rust variable.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
2015-06-27 20:45:35 +00:00
|
|
|
//! The Python interpreter uses a global interpreter lock (GIL)
|
2015-04-18 22:39:04 +00:00
|
|
|
//! to ensure thread-safety.
|
2015-10-26 22:52:18 +00:00
|
|
|
//! This API uses a zero-sized `struct Python<'p>` as a token to indicate
|
|
|
|
//! that a function can assume that the GIL is held.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
2015-10-26 22:52:18 +00:00
|
|
|
//! You obtain a `Python` instance by acquiring the GIL,
|
|
|
|
//! and have to pass it into all operations that call into the Python runtime.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
|
|
|
//! # Error Handling
|
2015-10-26 22:52:18 +00:00
|
|
|
//! The vast majority of operations in this library will return `PyResult<...>`.
|
|
|
|
//! This is an alias for the type `Result<..., PyErr>`.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
2017-06-11 15:46:23 +00:00
|
|
|
//! A `PyErr` represents a Python exception. Errors within the `PyO3` library are
|
2015-06-27 20:45:35 +00:00
|
|
|
//! also exposed as Python exceptions.
|
2015-04-18 22:39:04 +00:00
|
|
|
//!
|
2015-04-18 20:20:19 +00:00
|
|
|
//! # Example
|
|
|
|
//! ```
|
2017-05-13 05:43:17 +00:00
|
|
|
//! extern crate pyo3;
|
2015-04-18 20:20:19 +00:00
|
|
|
//!
|
2017-05-13 05:43:17 +00:00
|
|
|
//! use pyo3::{Python, PyDict, PyResult};
|
2017-01-26 20:35:16 +00:00
|
|
|
//!
|
2015-04-18 20:20:19 +00:00
|
|
|
//! fn main() {
|
2015-07-27 18:56:59 +00:00
|
|
|
//! let gil = Python::acquire_gil();
|
2016-12-17 14:04:39 +00:00
|
|
|
//! hello(gil.python()).unwrap();
|
|
|
|
//! }
|
2017-01-26 20:35:16 +00:00
|
|
|
//!
|
2016-12-17 14:04:39 +00:00
|
|
|
//! fn hello(py: Python) -> PyResult<()> {
|
|
|
|
//! let sys = py.import("sys")?;
|
2017-06-03 01:58:16 +00:00
|
|
|
//! let version: String = sys.get(py, "version")?.extract(py)?;
|
2017-01-26 20:35:16 +00:00
|
|
|
//!
|
2016-12-17 14:04:39 +00:00
|
|
|
//! let locals = PyDict::new(py);
|
2017-06-03 01:58:16 +00:00
|
|
|
//! locals.set_item(py, "os", py.import("os")?)?;
|
|
|
|
//! let user: String = py.eval("os.getenv('USER') or os.getenv('USERNAME')", None, Some(&locals))?.extract(py)?;
|
2017-01-26 20:35:16 +00:00
|
|
|
//!
|
2015-07-27 18:56:59 +00:00
|
|
|
//! println!("Hello {}, I'm Python {}", user, version);
|
2016-12-17 14:04:39 +00:00
|
|
|
//! Ok(())
|
2015-04-18 20:20:19 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
2017-06-11 23:35:24 +00:00
|
|
|
//!
|
|
|
|
//! Expands to an `extern "C"` function that allows Python to load
|
|
|
|
//! the rust code as a Python extension module.
|
|
|
|
//!
|
|
|
|
//! Macro syntax: `#[py::modinit(name)]`
|
|
|
|
//!
|
|
|
|
//! 1. `name`: The module name as a Rust identifier
|
|
|
|
//! 2. Decorate init function `Fn(Python, &PyModule) -> PyResult<()>`.
|
|
|
|
//! This function will be called when the module is imported, and is responsible
|
|
|
|
//! for adding the module's members.
|
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//! ```
|
|
|
|
//! #![feature(proc_macro)]
|
|
|
|
//! #![macro_use] extern crate pyo3;
|
|
|
|
//! use pyo3::{py, Python, PyResult, PyObject, PyModule};
|
|
|
|
//!
|
|
|
|
//! #[py::modinit(hello)]
|
|
|
|
//! fn init_module(py: Python, m: &PyModule) {
|
|
|
|
//! m.add(py, "__doc__", "Module documentation string")?;
|
|
|
|
//! m.add(py, "run", py_fn!(py, run()))?;
|
|
|
|
//! Ok(())
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn run(py: Python) -> PyResult<PyObject> {
|
|
|
|
//! println!("Rust says: Hello Python!");
|
|
|
|
//! Ok(py.None())
|
|
|
|
//! }
|
|
|
|
//! # fn main() {}
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! In your `Cargo.toml`, use the `extension-module` feature for the `pyo3` dependency:
|
|
|
|
//! ```cargo
|
|
|
|
//! [dependencies.pyo3]
|
|
|
|
//! version = "*"
|
|
|
|
//! features = ["extension-module"]
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! The full example project can be found at:
|
|
|
|
//! https://github.com/PyO3/setuptools-rust/tree/master/example/extensions
|
|
|
|
//!
|
|
|
|
//! Rust will compile the code into a file named `libhello.so`, but we have to
|
|
|
|
//! rename the file in order to use it with Python:
|
|
|
|
//!
|
|
|
|
//! ```bash
|
|
|
|
//! cp ./target/debug/libhello.so ./hello.so
|
|
|
|
//! ```
|
|
|
|
//! (Note: on Mac OS you will have to rename `libhello.dynlib` to `libhello.so`)
|
|
|
|
//!
|
|
|
|
//! The extension module can then be imported into Python:
|
|
|
|
//!
|
|
|
|
//! ```python1
|
|
|
|
//! >>> import hello
|
|
|
|
//! >>> hello.run()
|
|
|
|
//! Rust says: Hello Python!
|
|
|
|
//! ```
|
2015-04-18 20:20:19 +00:00
|
|
|
|
2015-01-05 16:05:53 +00:00
|
|
|
extern crate libc;
|
2017-06-07 02:26:59 +00:00
|
|
|
extern crate backtrace;
|
2017-06-11 23:35:24 +00:00
|
|
|
extern crate pyo3cls;
|
2017-06-06 03:25:00 +00:00
|
|
|
#[macro_use] extern crate log;
|
2015-05-19 21:32:32 +00:00
|
|
|
|
2017-06-11 23:35:24 +00:00
|
|
|
#[cfg(Py_3)]
|
2017-05-13 05:05:00 +00:00
|
|
|
pub mod ffi;
|
2017-06-11 23:35:24 +00:00
|
|
|
|
|
|
|
#[cfg(not(Py_3))]
|
|
|
|
mod ffi2;
|
|
|
|
|
|
|
|
#[cfg(not(Py_3))]
|
|
|
|
pub mod ffi {
|
|
|
|
pub use ffi2::*;
|
|
|
|
}
|
|
|
|
|
2017-05-16 23:54:27 +00:00
|
|
|
pub use ffi::{Py_ssize_t, Py_hash_t};
|
2017-05-22 05:22:45 +00:00
|
|
|
|
2017-05-30 23:29:13 +00:00
|
|
|
pub mod pointers;
|
2017-06-04 00:27:26 +00:00
|
|
|
pub use pointers::PyPtr;
|
2017-05-22 05:22:45 +00:00
|
|
|
|
2017-05-29 04:19:29 +00:00
|
|
|
mod token;
|
2017-06-06 03:25:00 +00:00
|
|
|
pub use token::{PyToken, PyObjectWithToken, ToInstancePtr, InstancePtr};
|
2017-05-29 04:19:29 +00:00
|
|
|
|
2017-06-08 01:40:30 +00:00
|
|
|
pub use err::{PyErr, PyResult, PyDowncastError, ToPyErr};
|
2015-01-05 16:02:30 +00:00
|
|
|
pub use objects::*;
|
2017-05-28 15:57:34 +00:00
|
|
|
pub use objectprotocol::ObjectProtocol;
|
2017-06-06 03:25:00 +00:00
|
|
|
pub use python::{Python, ToPyPointer, IntoPyPointer, PyClone,
|
|
|
|
PyMutDowncastFrom, PyDowncastFrom, PyDowncastInto};
|
2015-06-21 22:35:01 +00:00
|
|
|
pub use pythonrun::{GILGuard, GILProtected, prepare_freethreaded_python};
|
2017-06-06 03:25:00 +00:00
|
|
|
pub use conversion::{FromPyObject, RefFromPyObject, ToPyObject, IntoPyObject, IntoPyTuple};
|
2017-05-17 06:43:39 +00:00
|
|
|
pub use class::{CompareOp};
|
2017-05-25 03:31:51 +00:00
|
|
|
pub mod class;
|
|
|
|
pub use class::*;
|
|
|
|
pub use self::typeob::PyTypeObject;
|
2015-03-08 14:29:44 +00:00
|
|
|
|
2017-05-18 18:15:06 +00:00
|
|
|
pub mod py {
|
|
|
|
pub use pyo3cls::*;
|
2017-06-11 23:35:24 +00:00
|
|
|
|
|
|
|
#[cfg(Py_3)]
|
|
|
|
pub use pyo3cls::mod3init as modinit;
|
|
|
|
|
|
|
|
#[cfg(not(Py_3))]
|
|
|
|
pub use pyo3cls::mod2init as modinit;
|
2017-05-18 18:15:06 +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
|
|
|
|
2016-03-06 12:33:57 +00:00
|
|
|
// AST coercion macros (https://danielkeep.github.io/tlborm/book/blk-ast-coercion.html)
|
|
|
|
#[macro_export] #[doc(hidden)]
|
|
|
|
macro_rules! py_coerce_expr { ($s:expr) => {$s} }
|
|
|
|
|
2016-03-07 22:22:44 +00:00
|
|
|
#[macro_export] #[doc(hidden)]
|
|
|
|
macro_rules! py_replace_expr {
|
|
|
|
($_t:tt $sub:expr) => {$sub};
|
|
|
|
}
|
2016-03-06 12:33:57 +00:00
|
|
|
|
2017-05-25 05:43:07 +00:00
|
|
|
pub mod python;
|
2017-06-01 17:35:02 +00:00
|
|
|
mod fmt;
|
2015-01-05 16:05:53 +00:00
|
|
|
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;
|
2017-05-25 05:43:07 +00:00
|
|
|
pub mod callback;
|
|
|
|
pub mod typeob;
|
2015-08-02 22:06:15 +00:00
|
|
|
pub mod argparse;
|
2017-05-18 07:05:49 +00:00
|
|
|
pub mod function;
|
2017-05-31 08:07:33 +00:00
|
|
|
pub mod buffer;
|
2017-06-09 21:27:37 +00:00
|
|
|
pub mod freelist;
|
2015-01-05 16:05:53 +00:00
|
|
|
|
2017-05-14 21:42:56 +00:00
|
|
|
// re-export for simplicity
|
|
|
|
pub use std::os::raw::*;
|