2018-07-18 11:08:05 +00:00
#![ feature(specialization, use_extern_macros) ]
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
//!
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-06-15 08:06:04 +00:00
//!
//! ```rust
2018-07-18 11:08:05 +00:00
//! #![feature(use_extern_macros, specialization)]
2018-07-08 21:33:48 +00:00
//!
2017-05-13 05:43:17 +00:00
//! extern crate pyo3;
2015-04-18 20:20:19 +00:00
//!
2018-07-08 21:33:48 +00:00
//! use pyo3::prelude::*;
2017-01-26 20:35:16 +00:00
//!
2018-07-08 21:33:48 +00:00
//! fn main() -> PyResult<()> {
2015-07-27 18:56:59 +00:00
//! let gil = Python::acquire_gil();
2018-07-08 21:33:48 +00:00
//! let py = gil.python();
2016-12-17 14:04:39 +00:00
//! let sys = py.import("sys")?;
2017-06-22 17:26:07 +00:00
//! let version: String = sys.get("version")?.extract()?;
2017-01-26 20:35:16 +00:00
//!
2016-12-17 14:04:39 +00:00
//! let locals = PyDict::new(py);
2017-06-21 21:08:16 +00:00
//! locals.set_item("os", py.import("os")?)?;
2018-07-08 21:33:48 +00:00
//! let user: String = py.eval("os.getenv('USER') or os.getenv('USERNAME')", None, Some(&locals))?.extract()?;
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
//!
2017-06-15 18:13:58 +00:00
//! # Python extension
//!
//! To allow Python to load the rust code as a Python extension
2018-07-09 22:13:02 +00:00
//! module, you need an initialization function with `Fn(Python, &PyModule) -> PyResult<()>`
//! that is annotates with `#[pymodinit]`. By default the function name will become the module name,
//! but you can override that with `#[pymodinit(name)]`.
2017-06-11 23:35:24 +00:00
//!
2017-06-14 21:08:30 +00:00
//! To creates a Python callable object that invokes a Rust function, specify rust
2017-06-15 21:20:30 +00:00
//! function and decorate it with `#[pyfn()]` attribute. `pyfn()` accepts three parameters.
2017-06-14 21:08:30 +00:00
//!
//! 1. `m`: The module name.
2017-06-15 21:20:30 +00:00
//! 2. name of function visible to Python code.
2017-06-23 17:38:04 +00:00
//! 3. comma separated arguments, i.e. param="None", "*", param3="55"
2017-06-14 21:08:30 +00:00
//!
//!
2017-06-11 23:35:24 +00:00
//! # Example
2017-06-15 08:06:04 +00:00
//!
//! ```rust
2018-07-18 11:08:05 +00:00
//! #![feature(use_extern_macros, specialization)]
2017-06-15 08:06:04 +00:00
//!
2017-06-14 21:42:05 +00:00
//! extern crate pyo3;
2018-07-03 18:42:02 +00:00
//! use pyo3::prelude::*;
2017-06-11 23:35:24 +00:00
//!
2018-07-08 21:33:48 +00:00
//! // Add bindings to the generated python module
//! // N.B: names: "librust2py" must be the name of the `.so` or `.pyd` file
//! /// This module is implemented in Rust.
2018-07-09 22:13:02 +00:00
//! #[pymodinit]
//! fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {
2017-06-14 21:08:30 +00:00
//!
2018-07-08 21:33:48 +00:00
//! #[pyfn(m, "sum_as_string")]
//! // ``#[pyfn()]` converts the arguments from Python objects to Rust values
//! // and the Rust return value back into a Python object.
//! fn sum_as_string_py(a:i64, b:i64) -> PyResult<String> {
//! let out = sum_as_string(a, b);
//! Ok(out)
2017-06-14 21:08:30 +00:00
//! }
//!
2017-06-11 23:35:24 +00:00
//! Ok(())
//! }
//!
2018-07-08 21:33:48 +00:00
//! // The logic can be implemented as a normal rust function
//! fn sum_as_string(a:i64, b:i64) -> String {
//! format!("{}", a + b).to_string()
//! }
//!
2017-06-11 23:35:24 +00:00
//! # fn main() {}
//! ```
//!
//! In your `Cargo.toml`, use the `extension-module` feature for the `pyo3` dependency:
2017-06-15 16:11:19 +00:00
//!
2017-06-11 23:35:24 +00:00
//! ```cargo
//! [dependencies.pyo3]
//! version = "*"
//! features = ["extension-module"]
//! ```
//!
2018-07-08 21:33:48 +00:00
//! On windows and linux, you can build normally with `cargo build --release`. On Mac Os, you need to set additional linker arguments. One option is to compile with `cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup`, the other is to create a `.cargo/config` with the following content:
2017-06-11 23:35:24 +00:00
//!
2018-07-08 21:33:48 +00:00
//! ```toml
//! [target.x86_64-apple-darwin]
//! rustflags = [
//! "-C", "link-arg=-undefined",
//! "-C", "link-arg=dynamic_lookup",
//! ]
2017-06-11 23:35:24 +00:00
//! ```
2017-10-22 03:17:35 +00:00
//!
2018-07-08 21:33:48 +00:00
//! Also on macOS, you will need to rename the output from \*.dylib to \*.so. On Windows, you will need to rename the output from \*.dll to \*.pyd.
2017-06-11 23:35:24 +00:00
//!
2018-07-08 21:33:48 +00:00
//! [`setuptools-rust`](https://github.com/PyO3/setuptools-rust) can be used to generate a python package and includes the commands above by default. See [examples/word-count](examples/word-count) and the associated setup.py.
2015-04-18 20:20:19 +00:00
2015-01-05 16:05:53 +00:00
extern crate libc ;
2017-06-11 23:35:24 +00:00
extern crate pyo3cls ;
2018-07-30 21:01:46 +00:00
extern crate spin ;
2018-07-18 21:14:10 +00:00
// We need that reexport for wrap_function
#[ doc(hidden) ]
pub extern crate mashup ;
2015-05-19 21:32:32 +00:00
2017-06-11 23:35:24 +00:00
#[ cfg(not(Py_3)) ]
mod ffi2 ;
2017-06-11 23:47:27 +00:00
#[ cfg(Py_3) ]
mod ffi3 ;
2017-06-24 08:47:36 +00:00
/// Rust FFI declarations for Python
2017-06-11 23:35:24 +00:00
pub mod ffi {
2017-06-11 23:47:27 +00:00
#[ cfg(not(Py_3)) ]
2017-06-11 23:35:24 +00:00
pub use ffi2 ::* ;
2017-06-11 23:47:27 +00:00
#[ cfg(Py_3) ]
pub use ffi3 ::* ;
2017-06-11 23:35:24 +00:00
}
2018-07-30 21:01:46 +00:00
pub use conversion ::{
FromPyObject , IntoPyObject , IntoPyTuple , PyTryFrom , PyTryInto , ReturnTypeIntoPyResult ,
ToBorrowedObject , ToPyObject ,
} ;
pub use err ::{ PyDowncastError , PyErr , PyErrArguments , PyErrValue , PyResult } ;
pub use instance ::{ AsPyRef , Py , PyNativeType , PyObjectWithToken , PyToken } ;
2017-08-03 22:01:52 +00:00
pub use noargs ::NoArgs ;
2018-07-30 21:01:46 +00:00
pub use object ::PyObject ;
pub use objectprotocol ::ObjectProtocol ;
pub use objects ::* ;
pub use python ::{ IntoPyDictPointer , IntoPyPointer , Python , ToPyPointer } ;
pub use pythonrun ::{ init_once , prepare_freethreaded_python , GILGuard , GILPool } ;
pub use typeob ::{ PyObjectAlloc , PyRawObject , PyTypeInfo } ;
2017-05-25 03:31:51 +00:00
pub mod class ;
pub use class ::* ;
2015-03-08 14:29:44 +00:00
2018-07-18 11:08:05 +00:00
/// The proc macro attributes
pub mod proc_macro {
2018-07-30 21:01:46 +00:00
pub use pyo3cls ::{ pyclass , pyfunction , pymethods , pyproto } ;
2017-06-11 23:35:24 +00:00
2018-07-18 11:08:05 +00:00
#[ cfg(Py_3) ]
pub use pyo3cls ::mod3init as pymodinit ;
2017-06-11 23:35:24 +00:00
2018-07-18 11:08:05 +00:00
#[ cfg(not(Py_3)) ]
pub use pyo3cls ::mod2init as pymodinit ;
}
2017-05-18 18:15:06 +00:00
2015-04-18 20:20:19 +00:00
/// Constructs a `&'static CStr` literal.
2018-04-30 21:17:09 +00:00
macro_rules ! cstr {
2018-07-30 21:01:46 +00:00
( $s : tt ) = > {
2015-03-08 14:29:44 +00:00
// TODO: verify that $s is a string literal without nuls
2018-07-30 21:01:46 +00:00
unsafe { ::std ::ffi ::CStr ::from_ptr ( concat! ( $s , " \0 " ) . as_ptr ( ) as * const _ ) }
} ;
2018-04-30 21:17:09 +00:00
}
2018-07-18 21:14:10 +00:00
/// Returns a function that takes a [Python] instance and returns a python function.
2018-05-02 17:26:54 +00:00
///
/// Use this together with `#[function]` and [PyModule::add_function].
2018-04-30 21:17:09 +00:00
#[ macro_export ]
2018-07-18 21:14:10 +00:00
macro_rules ! wrap_function {
( $function_name :ident ) = > { {
// Get the mashup macro and its helpers into scope
use $crate ::mashup ::* ;
mashup! {
// Make sure this ident matches the one in function_wrapper_ident
m [ " method " ] = __pyo3_get_function_ $function_name ;
}
m! {
& " method "
}
2018-07-30 21:01:46 +00:00
} } ;
2018-07-18 21:14:10 +00:00
}
2015-01-05 16:05:53 +00:00
2018-07-30 21:01:46 +00:00
#[ doc(hidden) ]
pub mod argparse ;
pub mod buffer ;
#[ doc(hidden) ]
pub mod callback ;
2015-01-04 23:07:31 +00:00
mod conversion ;
2018-07-30 21:01:46 +00:00
mod err ;
pub mod freelist ;
2017-06-22 08:16:22 +00:00
mod instance ;
2018-07-30 21:01:46 +00:00
mod noargs ;
2017-07-13 20:05:50 +00:00
mod object ;
2015-01-04 20:37:43 +00:00
mod objectprotocol ;
2018-07-30 21:01:46 +00:00
mod objects ;
pub mod prelude ;
pub mod python ;
2015-01-05 16:05:53 +00:00
mod pythonrun ;
2017-05-25 05:43:07 +00:00
pub mod typeob ;
2015-01-05 16:05:53 +00:00
2017-05-14 21:42:56 +00:00
// re-export for simplicity
2017-06-15 16:11:19 +00:00
#[ doc(hidden) ]
2017-05-14 21:42:56 +00:00
pub use std ::os ::raw ::* ;