Rename #[pymodinit] to #[pymodule]
This commit is contained in:
parent
863ffb161f
commit
9c8c5a6063
|
@ -11,7 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|||
* Added a `wrap_module!` macro similar to the existing `wrap_function!` macro. Only available on python 3
|
||||
|
||||
### Changed
|
||||
|
||||
* Renamed `add_function` to `add_wrapped` as it now also supports modules.
|
||||
* Renamed `#[pymodinit]` to `#[pymodule]`.
|
||||
|
||||
### Removed
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
|||
}
|
||||
|
||||
/// This module is a python module implemented in Rust.
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn string_sum(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(sum_as_string))?;
|
||||
|
||||
|
|
|
@ -202,7 +202,7 @@ impl TzClass {
|
|||
}
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn datetime(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(make_date))?;
|
||||
m.add_wrapped(wrap_function!(get_date_tuple))?;
|
||||
|
|
|
@ -3,7 +3,7 @@ use pyo3::prelude::*;
|
|||
use pyo3::exceptions::RuntimeError;
|
||||
use pyo3::types::PyDict;
|
||||
|
||||
#[pymodinit(test_dict)]
|
||||
#[pymodule]
|
||||
fn test_dict(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<DictSize>()?;
|
||||
Ok(())
|
||||
|
|
|
@ -28,7 +28,7 @@ fn double(x: i32) -> i32 {
|
|||
x * 2
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn othermod(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(double))?;
|
||||
m.add_class::<ModClass>()?;
|
||||
|
|
|
@ -13,7 +13,7 @@ impl Subclassable {
|
|||
}
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn subclassing(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_class::<Subclassable>()?;
|
||||
Ok(())
|
||||
|
|
|
@ -78,7 +78,7 @@ fn count_line(line: &str, needle: &str) -> usize {
|
|||
total
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn word_count(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(count_line))?;
|
||||
m.add_class::<WordCounter>()?;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
## Macros
|
||||
|
||||
Pyo3's attributes, `#[pyclass]`, `#[pymodinit]`, etc. are [procedural macros](https://doc.rust-lang.org/unstable-book/language-features/proc-macro.html), which means that rewrite the source of the annotated item. You can view the generated source with the following command, which also expands a few other things:
|
||||
Pyo3's attributes, `#[pyclass]`, `#[pymodule]`, etc. are [procedural macros](https://doc.rust-lang.org/unstable-book/language-features/proc-macro.html), which means that rewrite the source of the annotated item. You can view the generated source with the following command, which also expands a few other things:
|
||||
|
||||
```bash
|
||||
cargo rustc --profile=check -- -Z unstable-options --pretty=expanded > expanded.rs; rustfmt expanded.rs
|
||||
|
|
|
@ -12,7 +12,7 @@ extern crate pyo3;
|
|||
use pyo3::prelude::*;
|
||||
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
// Note that the `#[pyfn()]` annotation automatically converts the arguments from
|
||||
|
@ -45,7 +45,7 @@ fn double(x: usize) -> usize {
|
|||
x * 2
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(double)).unwrap();
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ use pyo3::{PyResult, Python, PyModule};
|
|||
// 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.
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
// pyo3 aware function. All of our python interface could be declared in a separate module.
|
||||
|
@ -34,7 +34,7 @@ fn sum_as_string(a:i64, b:i64) -> String {
|
|||
# fn main() {}
|
||||
```
|
||||
|
||||
The `#[pymodinit]` procedural macro attribute takes care of exporting the initialization function of your module to Python. It takes one argument as the name of your module, which must be the name of the `.so` or `.pyd` file.
|
||||
The `#[pymodule]` procedural macro attribute takes care of exporting the initialization function of your module to Python. It takes one argument as the name of your module, which must be the name of the `.so` or `.pyd` file.
|
||||
|
||||
To import the module, either copy the shared library as described in [Get Started](./overview.md) or use a tool, e.g. `pyo3-pack develop` with [pyo3-pack](https://github.com/PyO3/pyo3-pack) or `python setup.py develop` with [setuptools-rust](https://github.com/PyO3/setuptools-rust).
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
|||
}
|
||||
|
||||
/// This module is a python moudle implemented in Rust.
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn rust_py(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
m.add_wrapped(wrap_function!(sum_as_string))?;
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ Then in the Python bridge, we have a function `search` exposed to Python runtime
|
|||
`Python::allow_threads` method to enable true parallelism:
|
||||
|
||||
```rust,ignore
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
fn word_count(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
||||
#[pyfn(m, "search")]
|
||||
|
|
|
@ -37,7 +37,7 @@ pub fn py2_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS
|
|||
}
|
||||
}
|
||||
|
||||
/// Finds and takes care of the #[pyfn(...)] in `#[pymodinit]`
|
||||
/// Finds and takes care of the #[pyfn(...)] in `#[pymodule]`
|
||||
pub fn process_functions_in_module(func: &mut syn::ItemFn) {
|
||||
let mut stmts: Vec<syn::Stmt> = Vec::new();
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ pub fn mod2init(
|
|||
input: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
// Parse the token stream into a syntax tree
|
||||
let mut ast: syn::ItemFn = syn::parse(input).expect("#[pymodinit] must be used on a function");
|
||||
let mut ast: syn::ItemFn = syn::parse(input).expect("#[pymodule] must be used on a function");
|
||||
|
||||
let modname: syn::Ident;
|
||||
if attr.is_empty() {
|
||||
|
@ -52,7 +52,7 @@ pub fn mod3init(
|
|||
) -> proc_macro::TokenStream {
|
||||
// Parse the token stream into a syntax tree
|
||||
let mut ast: syn::ItemFn =
|
||||
syn::parse(input).expect("#[pymodinit] must be used on a `fn` block");
|
||||
syn::parse(input).expect("#[pymodule] must be used on a `fn` block");
|
||||
|
||||
let modname: syn::Ident;
|
||||
if attr.is_empty() {
|
||||
|
|
|
@ -112,7 +112,7 @@ pub fn parse_fn_args<'p>(
|
|||
|
||||
#[cfg(Py_3)]
|
||||
#[doc(hidden)]
|
||||
/// Builds a module (or null) from a user given initializer. Used for `#[pymodinit]`.
|
||||
/// Builds a module (or null) from a user given initializer. Used for `#[pymodule]`.
|
||||
pub unsafe fn make_module(
|
||||
name: &str,
|
||||
doc: &str,
|
||||
|
@ -162,7 +162,7 @@ pub unsafe fn make_module(
|
|||
|
||||
#[cfg(not(Py_3))]
|
||||
#[doc(hidden)]
|
||||
/// Builds a module (or null) from a user given initializer. Used for `#[pymodinit]`.
|
||||
/// Builds a module (or null) from a user given initializer. Used for `#[pymodule]`.
|
||||
pub unsafe fn make_module(
|
||||
name: &str,
|
||||
doc: &str,
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -56,8 +56,8 @@
|
|||
//!
|
||||
//! To allow Python to load the rust code as a Python extension
|
||||
//! 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)]`.
|
||||
//! that is annotates with `#[pymodule]`. By default the function name will become the module name,
|
||||
//! but you can override that with `#[pymodule(name)]`.
|
||||
//!
|
||||
//! To creates a Python callable object that invokes a Rust function, specify rust
|
||||
//! function and decorate it with `#[pyfn()]` attribute. `pyfn()` accepts three parameters.
|
||||
|
@ -78,7 +78,7 @@
|
|||
//! // 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.
|
||||
//! #[pymodinit]
|
||||
//! #[pymodule]
|
||||
//! fn rust2py(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
//!
|
||||
//! #[pyfn(m, "sum_as_string")]
|
||||
|
@ -192,9 +192,9 @@ pub mod types;
|
|||
/// The proc macros, which are also part of the prelude
|
||||
pub mod proc_macro {
|
||||
#[cfg(not(Py_3))]
|
||||
pub use pyo3cls::mod2init as pymodinit;
|
||||
pub use pyo3cls::mod2init as pymodule;
|
||||
#[cfg(Py_3)]
|
||||
pub use pyo3cls::mod3init as pymodinit;
|
||||
pub use pyo3cls::mod3init as pymodule;
|
||||
/// The proc macro attributes
|
||||
pub use pyo3cls::{pyclass, pyfunction, pymethods, pyproto};
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ pub use crate::object::PyObject;
|
|||
pub use crate::objectprotocol::ObjectProtocol;
|
||||
pub use crate::python::Python;
|
||||
pub use crate::pythonrun::GILGuard;
|
||||
// This is only part of the prelude because we need it for the pymodinit function
|
||||
// This is only part of the prelude because we need it for the pymodule function
|
||||
pub use crate::types::PyModule;
|
||||
// This is required for the constructor
|
||||
pub use crate::PyRawObject;
|
||||
|
@ -26,7 +26,7 @@ pub use crate::PyRawObject;
|
|||
pub use pyo3cls::{pyclass, pyfunction, pymethods, pyproto};
|
||||
|
||||
#[cfg(Py_3)]
|
||||
pub use pyo3cls::mod3init as pymodinit;
|
||||
pub use pyo3cls::mod3init as pymodule;
|
||||
|
||||
#[cfg(not(Py_3))]
|
||||
pub use pyo3cls::mod2init as pymodinit;
|
||||
pub use pyo3cls::mod2init as pymodule;
|
||||
|
|
|
@ -27,7 +27,7 @@ static START_PYO3: sync::Once = sync::ONCE_INIT;
|
|||
/// thread (the thread which originally initialized Python) also initializes
|
||||
/// threading.
|
||||
///
|
||||
/// When writing an extension module, the `#[pymodinit]` macro
|
||||
/// When writing an extension module, the `#[pymodule]` macro
|
||||
/// will ensure that Python threading is initialized.
|
||||
///
|
||||
pub fn prepare_freethreaded_python() {
|
||||
|
|
|
@ -4,26 +4,33 @@
|
|||
extern crate pyo3;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
#[cfg(Py_3)]
|
||||
use pyo3::types::PyDict;
|
||||
|
||||
#[cfg(Py_3)]
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
#[pyclass]
|
||||
#[cfg(Py_3)]
|
||||
struct EmptyClass {}
|
||||
|
||||
#[cfg(Py_3)]
|
||||
fn sum_as_string(a: i64, b: i64) -> String {
|
||||
format!("{}", a + b).to_string()
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
#[cfg(Py_3)]
|
||||
/// Doubles the given value
|
||||
fn double(x: usize) -> usize {
|
||||
x * 2
|
||||
}
|
||||
|
||||
/// This module is implemented in Rust.
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
#[cfg(Py_3)]
|
||||
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
||||
#[pyfn(m, "sum_as_string")]
|
||||
fn sum_as_string_py(_py: Python, a: i64, b: i64) -> PyResult<String> {
|
||||
|
@ -47,6 +54,7 @@ fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(Py_3)]
|
||||
fn test_module_with_functions() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
@ -71,7 +79,7 @@ fn test_module_with_functions() {
|
|||
run("assert module_with_functions.also_double.__doc__ == 'Doubles the given value'");
|
||||
}
|
||||
|
||||
#[pymodinit(other_name)]
|
||||
#[pymodule(other_name)]
|
||||
fn some_name(_: Python, _: &PyModule) -> PyResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
@ -125,16 +133,19 @@ fn test_module_from_code() {
|
|||
}
|
||||
|
||||
#[pyfunction]
|
||||
#[cfg(Py_3)]
|
||||
fn r#move() -> usize {
|
||||
42
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[pymodule]
|
||||
#[cfg(Py_3)]
|
||||
fn raw_ident_module(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
module.add_wrapped(wrap_function!(r#move))
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(Py_3)]
|
||||
fn test_raw_idents() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
@ -150,21 +161,21 @@ fn subfunction() -> String {
|
|||
"Subfunction".to_string()
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[cfg(Py_3)]
|
||||
#[pymodule]
|
||||
fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
module.add_wrapped(wrap_function!(subfunction))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
#[cfg(Py_3)]
|
||||
#[pyfunction]
|
||||
fn superfunction() -> String {
|
||||
"Superfunction".to_string()
|
||||
}
|
||||
|
||||
#[pymodinit]
|
||||
#[cfg(Py_3)]
|
||||
#[pymodule]
|
||||
fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
||||
module.add_wrapped(wrap_function!(superfunction))?;
|
||||
module.add_wrapped(wrap_module!(submodule))?;
|
||||
|
|
Loading…
Reference in New Issue