2018-08-19 18:42:17 +00:00
#![ feature(specialization) ]
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
//!
2019-02-01 13:01:18 +00:00
//! Look at [the guide](https://pyo3.rs/) for a detailed introduction.
//!
2015-04-18 22:39:04 +00:00
//! # Ownership and Lifetimes
2019-02-01 13:01:18 +00:00
//!
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
//!
2019-02-01 13:01:18 +00:00
//! ## Using rust from python
2018-07-08 21:33:48 +00:00
//!
2019-02-01 13:01:18 +00:00
//! Pyo3 can be used to generate a native python module.
2015-04-18 20:20:19 +00:00
//!
2019-02-01 13:01:18 +00:00
//! **`Cargo.toml`**
2017-01-26 20:35:16 +00:00
//!
2019-02-01 13:01:18 +00:00
//! ```toml
//! [package]
//! name = "string-sum"
//! version = "0.1.0"
//! edition = "2018"
2017-01-26 20:35:16 +00:00
//!
2019-02-01 13:01:18 +00:00
//! [lib]
//! name = "string_sum"
//! crate-type = ["cdylib"]
2017-01-26 20:35:16 +00:00
//!
2019-02-01 13:01:18 +00:00
//! [dependencies.pyo3]
2019-04-23 16:55:35 +00:00
//! version = "0.7.0-alpha.1"
2019-02-01 13:01:18 +00:00
//! features = ["extension-module"]
2015-04-18 20:20:19 +00:00
//! ```
2017-06-11 23:35:24 +00:00
//!
2019-02-01 13:01:18 +00:00
//! **`src/lib.rs`**
2017-06-15 08:06:04 +00:00
//!
//! ```rust
2018-07-03 18:42:02 +00:00
//! use pyo3::prelude::*;
2019-02-01 13:01:18 +00:00
//! use pyo3::wrap_pyfunction;
2017-06-11 23:35:24 +00:00
//!
2019-02-01 13:01:18 +00:00
//! #[pyfunction]
//! /// Formats the sum of two numbers as string
//! fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
//! Ok((a + b).to_string())
//! }
2017-06-14 21:08:30 +00:00
//!
2019-02-01 13:01:18 +00:00
//! /// This module is a python module implemented in Rust.
//! #[pymodule]
//! fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
//! m.add_wrapped(wrap_pyfunction!(sum_as_string))?;
2017-06-14 21:08:30 +00:00
//!
2017-06-11 23:35:24 +00:00
//! Ok(())
//! }
//! ```
//!
2019-02-01 13:01:18 +00:00
//! On windows and linux, you can build normally with `cargo build --release`. On macOS, 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
//!
2019-02-01 13:01:18 +00:00
//! For developing, you can copy and rename the shared library from the target folder: On macOS, rename `libstring_sum.dylib` to `string_sum.so`, on windows `libstring_sum.dll` to `string_sum.pyd` and on linux `libstring_sum.so` to `string_sum.so`. Then open a python shell in the same folder and you'll be able to `import string_sum`.
2017-06-11 23:35:24 +00:00
//!
2019-02-01 13:01:18 +00:00
//! To build, test and publish your crate as python module, you can use [pyo3-pack](https://github.com/PyO3/pyo3-pack) or [setuptools-rust](https://github.com/PyO3/setuptools-rust). You can find an example for setuptools-rust in [examples/word-count](examples/word-count), while pyo3-pack should work on your crate without any configuration.
//!
//! ## Using python from rust
//!
//! Add `pyo3` this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
2019-04-23 16:55:35 +00:00
//! pyo3 = "0.7.0-alpha.1"
2019-02-01 13:01:18 +00:00
//! ```
//!
//! Example program displaying the value of `sys.version`:
//!
//! ```rust
//! use pyo3::prelude::*;
2019-03-20 18:37:27 +00:00
//! use pyo3::types::IntoPyDict;
2019-02-01 13:01:18 +00:00
//!
//! fn main() -> PyResult<()> {
//! let gil = Python::acquire_gil();
//! let py = gil.python();
//! let sys = py.import("sys")?;
//! let version: String = sys.get("version")?.extract()?;
//!
2019-03-20 18:37:27 +00:00
//! let locals = [("os", py.import("os")?)].into_py_dict(py);
2019-02-01 13:01:18 +00:00
//! let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
//! let user: String = py.eval(code, None, Some(&locals))?.extract()?;
//!
//! println!("Hello {}, I'm Python {}", user, version);
//! Ok(())
//! }
//! ```
2019-04-14 10:58:12 +00:00
use doc_comment ::doctest ;
// Test readme and user guide
doctest! ( " ../README.md " , readme_md ) ;
doctest! ( " ../guide/src/advanced.md " , guide_advanced_md ) ;
doctest! (
" ../guide/src/building_and_distribution.md " ,
guide_building_and_distribution_md
) ;
doctest! ( " ../guide/src/class.md " , guide_class_md ) ;
doctest! ( " ../guide/src/conversions.md " , guide_conversions_md ) ;
doctest! ( " ../guide/src/debugging.md " , guide_debugging_md ) ;
doctest! ( " ../guide/src/exception.md " , guide_exception_md ) ;
doctest! ( " ../guide/src/function.md " , guide_function_md ) ;
doctest! ( " ../guide/src/get_started.md " , guide_get_started_md ) ;
doctest! ( " ../guide/src/module.md " , guide_module_md ) ;
doctest! ( " ../guide/src/parallelism.md " , guide_parallelism_md ) ;
2019-04-24 20:44:47 +00:00
doctest! ( " ../guide/src/pypy.md " , guide_pypy_md ) ;
2019-04-14 10:58:12 +00:00
doctest! ( " ../guide/src/rust_cpython.md " , guide_rust_cpython_md ) ;
2015-05-19 21:32:32 +00:00
2018-10-31 10:43:21 +00:00
pub use crate ::class ::* ;
pub use crate ::conversion ::{
2019-03-29 09:57:05 +00:00
AsPyPointer , FromPy , FromPyObject , FromPyPointer , IntoPy , IntoPyObject , IntoPyPointer ,
PyTryFrom , PyTryInto , ToBorrowedObject , ToPyObject ,
2018-07-30 21:01:46 +00:00
} ;
2018-10-31 10:43:21 +00:00
pub use crate ::err ::{ PyDowncastError , PyErr , PyErrArguments , PyErrValue , PyResult } ;
2019-02-23 17:01:22 +00:00
pub use crate ::gil ::{ init_once , GILGuard , GILPool } ;
pub use crate ::instance ::{ AsPyRef , ManagedPyRef , Py , PyNativeType , PyRef , PyRefMut } ;
2018-10-31 10:43:21 +00:00
pub use crate ::object ::PyObject ;
pub use crate ::objectprotocol ::ObjectProtocol ;
2019-02-23 17:01:22 +00:00
pub use crate ::python ::{ prepare_freethreaded_python , Python } ;
pub use crate ::type_object ::{ PyObjectAlloc , PyRawObject , PyTypeInfo } ;
2015-03-08 14:29:44 +00:00
2019-04-17 17:06:12 +00:00
// Re-exported for wrap_function
2019-02-01 13:01:18 +00:00
#[ doc(hidden) ]
pub use mashup ;
2019-04-17 17:06:12 +00:00
// Re-exported for pymethods
2019-02-01 13:49:25 +00:00
#[ doc(hidden) ]
pub use inventory ;
2019-04-17 17:06:12 +00:00
// Re-exported for the `__wrap` functions
#[ doc(hidden) ]
pub use libc ;
2019-02-01 13:01:18 +00:00
2019-02-23 17:01:22 +00:00
/// Raw ffi declarations for the c interface of python
2018-09-21 21:32:48 +00:00
pub mod ffi ;
2017-06-11 23:35:24 +00:00
2018-09-21 21:32:48 +00:00
mod ffi3 ;
pub mod buffer ;
#[ doc(hidden) ]
pub mod callback ;
2019-03-18 10:01:55 +00:00
pub mod class ;
2018-09-21 21:32:48 +00:00
mod conversion ;
#[ doc(hidden) ]
pub mod derive_utils ;
mod err ;
2019-02-23 17:01:22 +00:00
pub mod exceptions ;
2018-09-21 21:32:48 +00:00
pub mod freelist ;
2019-02-23 17:01:22 +00:00
mod gil ;
2018-09-21 21:32:48 +00:00
mod instance ;
mod object ;
mod objectprotocol ;
pub mod prelude ;
2019-02-23 17:01:22 +00:00
mod python ;
pub mod type_object ;
2018-09-21 21:32:48 +00:00
pub mod types ;
/// The proc macros, which are also part of the prelude
pub mod proc_macro {
2019-03-22 13:07:33 +00:00
pub use pyo3cls ::pymodule ;
2018-09-21 21:32:48 +00:00
/// The proc macro attributes
pub use pyo3cls ::{ pyclass , pyfunction , pymethods , pyproto } ;
}
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
///
2018-11-12 21:25:45 +00:00
/// Use this together with `#[pyfunction]` and [types::PyModule::add_wrapped].
2018-04-30 21:17:09 +00:00
#[ macro_export ]
2019-02-01 13:01:18 +00:00
macro_rules ! wrap_pyfunction {
2018-07-18 21:14:10 +00:00
( $function_name :ident ) = > { {
// Get the mashup macro and its helpers into scope
2019-02-01 13:49:25 +00:00
use pyo3 ::mashup ::* ;
2018-07-18 21:14:10 +00:00
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
}
2018-11-12 21:25:45 +00:00
/// Returns a function that takes a [Python] instance and returns a python module.
///
/// Use this together with `#[pymodule]` and [types::PyModule::add_wrapped].
#[ macro_export ]
2019-02-01 13:01:18 +00:00
macro_rules ! wrap_pymodule {
2018-11-12 21:25:45 +00:00
( $module_name :ident ) = > { {
2019-02-01 13:49:25 +00:00
use pyo3 ::mashup ::* ;
2018-11-12 21:25:45 +00:00
mashup! {
m [ " method " ] = PyInit_ $module_name ;
}
m! {
2019-03-22 13:07:33 +00:00
& | py | unsafe { pyo3 ::PyObject ::from_owned_ptr ( py , " method " ( ) ) }
2018-11-12 21:25:45 +00:00
}
} } ;
}