Use single-arg form of `#[pymodule]` function in docs and tests (#3899)

* Use single-arg form for `#[pymodule]` functions in docs and tests

* Update guide/src/function.md

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>

* Add test of two-argument module function

* Fix new test

---------

Co-authored-by: Icxolu <10486322+Icxolu@users.noreply.github.com>
This commit is contained in:
Matthew Neeley 2024-02-28 14:36:50 -08:00 committed by GitHub
parent a582fa0163
commit 68ec6de0c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
43 changed files with 108 additions and 86 deletions

View File

@ -86,7 +86,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to /// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module. /// import the module.
#[pymodule] #[pymodule]
fn string_sum(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(()) Ok(())
} }

View File

@ -60,7 +60,7 @@ impl PyCounter {
} }
#[pymodule] #[pymodule]
pub fn decorator(_py: Python<'_>, module: &PyModule) -> PyResult<()> { pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_class::<PyCounter>()?; module.add_class::<PyCounter>()?;
Ok(()) Ok(())
} }

View File

@ -75,7 +75,7 @@ impl ExampleContainer {
#[pymodule] #[pymodule]
#[pyo3(name = "getitem")] #[pyo3(name = "getitem")]
fn example(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn example(m: &Bound<'_, PyModule>) -> PyResult<()> {
// ? -https://github.com/PyO3/maturin/issues/475 // ? -https://github.com/PyO3/maturin/issues/475
m.add_class::<ExampleContainer>()?; m.add_class::<ExampleContainer>()?;
Ok(()) Ok(())

View File

@ -16,7 +16,7 @@ impl SubmoduleClass {
} }
#[pymodule] #[pymodule]
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?; m.add_class::<SubmoduleClass>()?;
Ok(()) Ok(())
} }

View File

@ -26,7 +26,7 @@ impl Gadget {
/// A Python module for plugin interface types /// A Python module for plugin interface types
#[pymodule] #[pymodule]
pub fn plugin_api(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn plugin_api(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Gadget>()?; m.add_class::<Gadget>()?;
Ok(()) Ok(())
} }

View File

@ -16,7 +16,7 @@ impl SubmoduleClass {
} }
#[pymodule] #[pymodule]
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?; m.add_class::<SubmoduleClass>()?;
Ok(()) Ok(())
} }

View File

@ -33,7 +33,7 @@ fn count_line(line: &str, needle: &str) -> usize {
} }
#[pymodule] #[pymodule]
fn word_count(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn word_count(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(search, m)?)?; m.add_function(wrap_pyfunction!(search, m)?)?;
m.add_function(wrap_pyfunction!(search_sequential, m)?)?; m.add_function(wrap_pyfunction!(search_sequential, m)?)?;
m.add_function(wrap_pyfunction!(search_sequential_allow_threads, m)?)?; m.add_function(wrap_pyfunction!(search_sequential_allow_threads, m)?)?;

View File

@ -181,7 +181,7 @@ The next step is to create the module initializer and add our class to it:
# struct Number(i32); # struct Number(i32);
# #
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?; m.add_class::<Number>()?;
Ok(()) Ok(())
} }

View File

@ -326,7 +326,7 @@ impl Number {
} }
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?; m.add_class::<Number>()?;
Ok(()) Ok(())
} }

View File

@ -18,7 +18,7 @@ impl Number {
} }
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?; m.add_class::<Number>()?;
Ok(()) Ok(())
} }
@ -295,7 +295,7 @@ impl Number {
} }
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?; m.add_class::<Number>()?;
Ok(()) Ok(())
} }

View File

@ -128,7 +128,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
} }
#[pymodule] #[pymodule]
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
Ok(()) Ok(())
@ -151,7 +151,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
} }
#[pymodule] #[pymodule]
fn my_async_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
Ok(()) Ok(())
} }
@ -475,7 +475,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
} }
#[pymodule] #[pymodule]
fn my_async_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_async_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(rust_sleep, m)?)?; m.add_function(wrap_pyfunction!(rust_sleep, m)?)?;
Ok(()) Ok(())

View File

@ -30,11 +30,11 @@ fn log_something() {
} }
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
// A good place to install the Rust -> Python logger. // A good place to install the Rust -> Python logger.
pyo3_log::init(); pyo3_log::init();
m.add_function(wrap_pyfunction!(log_something))?; m.add_function(wrap_pyfunction!(log_something, m)?)?;
Ok(()) Ok(())
} }
``` ```

View File

@ -13,7 +13,7 @@ fn double(x: usize) -> usize {
} }
#[pymodule] #[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?; m.add_function(wrap_pyfunction!(double, m)?)?;
Ok(()) Ok(())
} }
@ -55,7 +55,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
} }
#[pymodule] #[pymodule]
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(no_args_py, m)?)?; m.add_function(wrap_pyfunction!(no_args_py, m)?)?;
Ok(()) Ok(())
} }
@ -92,7 +92,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
} }
#[pymodule] #[pymodule]
fn module_with_fn(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_with_fn(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?) m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)
} }
``` ```
@ -166,8 +166,8 @@ Python argument passing convention.) It then embeds the call to the Rust functio
FFI-wrapper function. This wrapper handles extraction of the regular arguments and the keyword FFI-wrapper function. This wrapper handles extraction of the regular arguments and the keyword
arguments from the input `PyObject`s. arguments from the input `PyObject`s.
The `wrap_pyfunction` macro can be used to directly get a `PyCFunction` given a The `wrap_pyfunction` macro can be used to directly get a `Bound<PyCFunction>` given a
`#[pyfunction]` and a `PyModule`: `wrap_pyfunction!(rust_fun, module)`. `#[pyfunction]` and a `Bound<PyModule>`: `wrap_pyfunction!(rust_fun, module)`.
## `#[pyfn]` shorthand ## `#[pyfn]` shorthand
@ -197,7 +197,7 @@ documented in the rest of this chapter. The code above is expanded to the follow
use pyo3::prelude::*; use pyo3::prelude::*;
#[pymodule] #[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
#[pyfunction] #[pyfunction]
fn double(x: usize) -> usize { fn double(x: usize) -> usize {
x * 2 x * 2

View File

@ -21,7 +21,7 @@ fn num_kwds(kwds: Option<&PyDict>) -> usize {
} }
#[pymodule] #[pymodule]
fn module_with_functions(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(num_kwds, m)?).unwrap(); m.add_function(wrap_pyfunction!(num_kwds, m)?).unwrap();
Ok(()) Ok(())
} }

View File

@ -163,7 +163,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to /// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module. /// import the module.
#[pymodule] #[pymodule]
fn pyo3_example(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn pyo3_example(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(()) Ok(())
} }

View File

@ -12,7 +12,7 @@ fn double(x: usize) -> usize {
/// This module is implemented in Rust. /// This module is implemented in Rust.
#[pymodule] #[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?; m.add_function(wrap_pyfunction!(double, m)?)?;
Ok(()) Ok(())
} }
@ -34,7 +34,7 @@ fn double(x: usize) -> usize {
#[pymodule] #[pymodule]
#[pyo3(name = "custom_name")] #[pyo3(name = "custom_name")]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?; m.add_function(wrap_pyfunction!(double, m)?)?;
Ok(()) Ok(())
} }

View File

@ -283,7 +283,7 @@ fn add_one(x: i64) -> i64 {
} }
#[pymodule] #[pymodule]
fn foo(_py: Python<'_>, foo_module: &PyModule) -> PyResult<()> { fn foo(foo_module: &Bound<'_, PyModule>) -> PyResult<()> {
foo_module.add_function(wrap_pyfunction!(add_one, foo_module)?)?; foo_module.add_function(wrap_pyfunction!(add_one, foo_module)?)?;
Ok(()) Ok(())
} }

View File

@ -132,7 +132,7 @@ struct UserModel {
} }
#[pymodule] #[pymodule]
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<UserModel>()?; m.add_class::<UserModel>()?;
Ok(()) Ok(())
} }
@ -489,7 +489,7 @@ pub struct UserModel {
} }
#[pymodule] #[pymodule]
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<UserModel>()?; m.add_class::<UserModel>()?;
m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?; m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?;
Ok(()) Ok(())

View File

@ -79,7 +79,7 @@ impl FutureAwaitable {
} }
#[pymodule] #[pymodule]
pub fn awaitable(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<IterAwaitable>()?; m.add_class::<IterAwaitable>()?;
m.add_class::<FutureAwaitable>()?; m.add_class::<FutureAwaitable>()?;
Ok(()) Ok(())

View File

@ -48,7 +48,7 @@ fn return_memoryview(py: Python<'_>) -> PyResult<Bound<'_, PyMemoryView>> {
} }
#[pymodule] #[pymodule]
pub fn buf_and_str(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn buf_and_str(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<BytesExtractor>()?; m.add_class::<BytesExtractor>()?;
m.add_function(wrap_pyfunction!(return_memoryview, m)?)?; m.add_function(wrap_pyfunction!(return_memoryview, m)?)?;
Ok(()) Ok(())

View File

@ -101,7 +101,7 @@ impl OrderedDefaultNe {
} }
#[pymodule] #[pymodule]
pub fn comparisons(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Eq>()?; m.add_class::<Eq>()?;
m.add_class::<EqDefaultNe>()?; m.add_class::<EqDefaultNe>()?;
m.add_class::<Ordered>()?; m.add_class::<Ordered>()?;

View File

@ -205,7 +205,7 @@ impl TzClass {
} }
#[pymodule] #[pymodule]
pub fn datetime(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn datetime(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(make_date, m)?)?; m.add_function(wrap_pyfunction!(make_date, m)?)?;
m.add_function(wrap_pyfunction!(get_date_tuple, m)?)?; m.add_function(wrap_pyfunction!(get_date_tuple, m)?)?;
m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?; m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?;

View File

@ -3,7 +3,7 @@ use pyo3::prelude::*;
use pyo3::types::PyDict; use pyo3::types::PyDict;
#[pymodule] #[pymodule]
pub fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<DictSize>()?; m.add_class::<DictSize>()?;
Ok(()) Ok(())
} }

View File

@ -1,7 +1,7 @@
use pyo3::{pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction, PyResult, Python}; use pyo3::{pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction, Bound, PyResult};
#[pymodule] #[pymodule]
pub fn enums(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn enums(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SimpleEnum>()?; m.add_class::<SimpleEnum>()?;
m.add_class::<ComplexEnum>()?; m.add_class::<ComplexEnum>()?;
m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?; m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?;

View File

@ -31,7 +31,7 @@ fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny>
} }
#[pymodule] #[pymodule]
pub fn misc(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn misc(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(issue_219, m)?)?; m.add_function(wrap_pyfunction!(issue_219, m)?)?;
m.add_function(wrap_pyfunction!(get_type_full_name, m)?)?; m.add_function(wrap_pyfunction!(get_type_full_name, m)?)?;
m.add_function(wrap_pyfunction!(accepts_bool, m)?)?; m.add_function(wrap_pyfunction!(accepts_bool, m)?)?;

View File

@ -19,6 +19,6 @@ impl ObjStore {
} }
#[pymodule] #[pymodule]
pub fn objstore(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ObjStore>() m.add_class::<ObjStore>()
} }

View File

@ -29,7 +29,7 @@ fn double(x: i32) -> i32 {
} }
#[pymodule] #[pymodule]
pub fn othermod(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn othermod(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?; m.add_function(wrap_pyfunction!(double, m)?)?;
m.add_class::<ModClass>()?; m.add_class::<ModClass>()?;

View File

@ -12,7 +12,7 @@ fn take_pathbuf(path: PathBuf) -> PathBuf {
} }
#[pymodule] #[pymodule]
pub fn path(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn path(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(make_path, m)?)?; m.add_function(wrap_pyfunction!(make_path, m)?)?;
m.add_function(wrap_pyfunction!(take_pathbuf, m)?)?; m.add_function(wrap_pyfunction!(take_pathbuf, m)?)?;

View File

@ -80,7 +80,7 @@ impl AssertingBaseClassGilRef {
struct ClassWithoutConstructor; struct ClassWithoutConstructor;
#[pymodule] #[pymodule]
pub fn pyclasses(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<EmptyClass>()?; m.add_class::<EmptyClass>()?;
m.add_class::<PyClassIter>()?; m.add_class::<PyClassIter>()?;
m.add_class::<AssertingBaseClass>()?; m.add_class::<AssertingBaseClass>()?;

View File

@ -68,7 +68,7 @@ fn args_kwargs<'py>(
} }
#[pymodule] #[pymodule]
pub fn pyfunctions(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn pyfunctions(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(none, m)?)?; m.add_function(wrap_pyfunction!(none, m)?)?;
m.add_function(wrap_pyfunction!(simple, m)?)?; m.add_function(wrap_pyfunction!(simple, m)?)?;
m.add_function(wrap_pyfunction!(simple_args, m)?)?; m.add_function(wrap_pyfunction!(simple_args, m)?)?;

View File

@ -17,7 +17,7 @@ fn vec_to_vec_pystring(vec: Vec<&PyString>) -> Vec<&PyString> {
} }
#[pymodule] #[pymodule]
pub fn sequence(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn sequence(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(vec_to_vec_i32, m)?)?; m.add_function(wrap_pyfunction!(vec_to_vec_i32, m)?)?;
m.add_function(wrap_pyfunction!(array_to_array_i32, m)?)?; m.add_function(wrap_pyfunction!(array_to_array_i32, m)?)?;
m.add_function(wrap_pyfunction!(vec_to_vec_pystring, m)?)?; m.add_function(wrap_pyfunction!(vec_to_vec_pystring, m)?)?;

View File

@ -18,7 +18,7 @@ impl Subclassable {
} }
#[pymodule] #[pymodule]
pub fn subclassing(_py: Python<'_>, m: &PyModule) -> PyResult<()> { pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Subclassable>()?; m.add_class::<Subclassable>()?;
Ok(()) Ok(())
} }

View File

@ -71,7 +71,7 @@
//! } //! }
//! //!
//! #[pymodule] //! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { //! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?; //! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?;
//! Ok(()) //! Ok(())
//! } //! }

View File

@ -31,7 +31,7 @@
//! } //! }
//! //!
//! #[pymodule] //! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { //! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(add_one, m)?)?; //! m.add_function(wrap_pyfunction!(add_one, m)?)?;
//! Ok(()) //! Ok(())
//! } //! }

View File

@ -44,7 +44,7 @@
//! } //! }
//! //!
//! #[pymodule] //! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { //! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(get_eigenvalues, m)?)?; //! m.add_function(wrap_pyfunction!(get_eigenvalues, m)?)?;
//! Ok(()) //! Ok(())
//! } //! }
@ -57,7 +57,7 @@
//! # Python::with_gil(|py| -> PyResult<()> { //! # Python::with_gil(|py| -> PyResult<()> {
//! # let module = PyModule::new_bound(py, "my_module")?; //! # let module = PyModule::new_bound(py, "my_module")?;
//! # //! #
//! # module.add_function(&wrap_pyfunction!(get_eigenvalues, module.as_gil_ref())?.as_borrowed())?; //! # module.add_function(&wrap_pyfunction!(get_eigenvalues, module)?)?;
//! # //! #
//! # let m11 = PyComplex::from_doubles_bound(py, 0_f64, -1_f64); //! # let m11 = PyComplex::from_doubles_bound(py, 0_f64, -1_f64);
//! # let m12 = PyComplex::from_doubles_bound(py, 1_f64, 0_f64); //! # let m12 = PyComplex::from_doubles_bound(py, 1_f64, 0_f64);

View File

@ -30,7 +30,7 @@
//! } //! }
//! //!
//! #[pymodule] //! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { //! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(add_one, m)?)?; //! m.add_function(wrap_pyfunction!(add_one, m)?)?;
//! Ok(()) //! Ok(())
//! } //! }

View File

@ -164,9 +164,9 @@ macro_rules! import_exception {
/// } /// }
/// ///
/// #[pymodule] /// #[pymodule]
/// fn my_module(py: Python<'_>, m: &PyModule) -> PyResult<()> { /// fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
/// m.add("MyError", py.get_type_bound::<MyError>())?; /// m.add("MyError", m.py().get_type_bound::<MyError>())?;
/// m.add_function(wrap_pyfunction!(raise_myerror, py)?)?; /// m.add_function(wrap_pyfunction!(raise_myerror, m)?)?;
/// Ok(()) /// Ok(())
/// } /// }
/// # fn main() -> PyResult<()> { /// # fn main() -> PyResult<()> {

View File

@ -168,7 +168,7 @@
//! //!
//! /// A Python module implemented in Rust. //! /// A Python module implemented in Rust.
//! #[pymodule] //! #[pymodule]
//! fn string_sum(py: Python<'_>, m: &PyModule) -> PyResult<()> { //! fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; //! m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
//! //!
//! Ok(()) //! Ok(())

View File

@ -239,7 +239,7 @@ impl PyModule {
/// use pyo3::prelude::*; /// use pyo3::prelude::*;
/// ///
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add("c", 299_792_458)?; /// module.add("c", 299_792_458)?;
/// Ok(()) /// Ok(())
/// } /// }
@ -280,7 +280,7 @@ impl PyModule {
/// struct Foo { /* fields omitted */ } /// struct Foo { /* fields omitted */ }
/// ///
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_class::<Foo>()?; /// module.add_class::<Foo>()?;
/// Ok(()) /// Ok(())
/// } /// }
@ -377,7 +377,7 @@ impl PyModule {
/// println!("Hello world!") /// println!("Hello world!")
/// } /// }
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_function(wrap_pyfunction!(say_hello, module)?) /// module.add_function(wrap_pyfunction!(say_hello, module)?)
/// } /// }
/// ``` /// ```
@ -442,7 +442,7 @@ pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
/// use pyo3::prelude::*; /// use pyo3::prelude::*;
/// ///
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add("c", 299_792_458)?; /// module.add("c", 299_792_458)?;
/// Ok(()) /// Ok(())
/// } /// }
@ -481,7 +481,7 @@ pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
/// struct Foo { /* fields omitted */ } /// struct Foo { /* fields omitted */ }
/// ///
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_class::<Foo>()?; /// module.add_class::<Foo>()?;
/// Ok(()) /// Ok(())
/// } /// }
@ -570,7 +570,7 @@ pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
/// println!("Hello world!") /// println!("Hello world!")
/// } /// }
/// #[pymodule] /// #[pymodule]
/// fn my_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { /// fn my_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
/// module.add_function(wrap_pyfunction!(say_hello, module)?) /// module.add_function(wrap_pyfunction!(say_hello, module)?)
/// } /// }
/// ``` /// ```

View File

@ -8,8 +8,8 @@ fn foo() -> usize {
} }
#[pymodule] #[pymodule]
fn module_fn_with_functions(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_fn_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(foo, m)?).unwrap(); m.add_function(wrap_pyfunction!(foo, m)?)?;
Ok(()) Ok(())
} }

View File

@ -36,7 +36,7 @@ fn double(x: usize) -> usize {
/// This module is implemented in Rust. /// This module is implemented in Rust.
#[pymodule] #[pymodule]
fn module_with_functions(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_with_functions(m: &Bound<'_, PyModule>) -> PyResult<()> {
#[pyfn(m)] #[pyfn(m)]
#[pyo3(name = "no_parameters")] #[pyo3(name = "no_parameters")]
fn function_with_name() -> usize { fn function_with_name() -> usize {
@ -54,14 +54,14 @@ fn module_with_functions(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
v.value * 2 v.value * 2
} }
m.add_class::<AnonClass>().unwrap(); m.add_class::<AnonClass>()?;
m.add_class::<ValueClass>().unwrap(); m.add_class::<ValueClass>()?;
m.add_class::<LocatedClass>().unwrap(); m.add_class::<LocatedClass>()?;
m.add("foo", "bar").unwrap(); m.add("foo", "bar")?;
m.add_function(wrap_pyfunction!(double, m)?).unwrap(); m.add_function(wrap_pyfunction!(double, m)?)?;
m.add("also_double", wrap_pyfunction!(double, m)?).unwrap(); m.add("also_double", wrap_pyfunction!(double, m)?)?;
Ok(()) Ok(())
} }
@ -116,9 +116,31 @@ fn test_module_with_functions() {
}); });
} }
/// This module uses a legacy two-argument module function.
#[pymodule]
fn module_with_explicit_py_arg(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?;
Ok(())
}
#[test]
fn test_module_with_explicit_py_arg() {
use pyo3::wrap_pymodule;
Python::with_gil(|py| {
let d = [(
"module_with_explicit_py_arg",
wrap_pymodule!(module_with_explicit_py_arg)(py),
)]
.into_py_dict_bound(py);
py_assert!(py, *d, "module_with_explicit_py_arg.double(3) == 6");
});
}
#[pymodule] #[pymodule]
#[pyo3(name = "other_name")] #[pyo3(name = "other_name")]
fn some_name(_: Python<'_>, m: &PyModule) -> PyResult<()> { fn some_name(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("other_name", "other_name")?; m.add("other_name", "other_name")?;
Ok(()) Ok(())
} }
@ -166,7 +188,7 @@ fn r#move() -> usize {
} }
#[pymodule] #[pymodule]
fn raw_ident_module(_py: Python<'_>, module: &PyModule) -> PyResult<()> { fn raw_ident_module(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_function(wrap_pyfunction!(r#move, module)?) module.add_function(wrap_pyfunction!(r#move, module)?)
} }
@ -190,7 +212,7 @@ fn custom_named_fn() -> usize {
#[test] #[test]
fn test_custom_names() { fn test_custom_names() {
#[pymodule] #[pymodule]
fn custom_names(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn custom_names(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?; m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?;
Ok(()) Ok(())
} }
@ -206,7 +228,7 @@ fn test_custom_names() {
#[test] #[test]
fn test_module_dict() { fn test_module_dict() {
#[pymodule] #[pymodule]
fn module_dict(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_dict(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.dict().set_item("yay", "me")?; m.dict().set_item("yay", "me")?;
Ok(()) Ok(())
} }
@ -222,7 +244,7 @@ fn test_module_dict() {
fn test_module_dunder_all() { fn test_module_dunder_all() {
Python::with_gil(|py| { Python::with_gil(|py| {
#[pymodule] #[pymodule]
fn dunder_all(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn dunder_all(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.dict().set_item("yay", "me")?; m.dict().set_item("yay", "me")?;
m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?; m.add_function(wrap_pyfunction!(custom_named_fn, m)?)?;
Ok(()) Ok(())
@ -245,7 +267,7 @@ fn submodule(module: &Bound<'_, PyModule>) -> PyResult<()> {
} }
#[pymodule] #[pymodule]
fn submodule_with_init_fn(_py: Python<'_>, module: &PyModule) -> PyResult<()> { fn submodule_with_init_fn(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_function(wrap_pyfunction!(subfunction, module)?)?; module.add_function(wrap_pyfunction!(subfunction, module)?)?;
Ok(()) Ok(())
} }
@ -256,14 +278,14 @@ fn superfunction() -> String {
} }
#[pymodule] #[pymodule]
fn supermodule(py: Python<'_>, module: &PyModule) -> PyResult<()> { fn supermodule(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_function(wrap_pyfunction!(superfunction, module)?)?; module.add_function(wrap_pyfunction!(superfunction, module)?)?;
let module_to_add = PyModule::new_bound(py, "submodule")?; let module_to_add = PyModule::new_bound(module.py(), "submodule")?;
submodule(&module_to_add)?; submodule(&module_to_add)?;
module.add_submodule(module_to_add.as_gil_ref())?; module.add_submodule(&module_to_add)?;
let module_to_add = PyModule::new_bound(py, "submodule_with_init_fn")?; let module_to_add = PyModule::new_bound(module.py(), "submodule_with_init_fn")?;
submodule_with_init_fn(py, module_to_add.as_gil_ref())?; submodule_with_init_fn(&module_to_add)?;
module.add_submodule(module_to_add.as_gil_ref())?; module.add_submodule(&module_to_add)?;
Ok(()) Ok(())
} }
@ -300,7 +322,7 @@ fn ext_vararg_fn(py: Python<'_>, a: i32, args: &Bound<'_, PyTuple>) -> PyObject
} }
#[pymodule] #[pymodule]
fn vararg_module(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { fn vararg_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
#[pyfn(m, signature = (a=5, *args))] #[pyfn(m, signature = (a=5, *args))]
fn int_vararg_fn(py: Python<'_>, a: i32, args: &Bound<'_, PyTuple>) -> PyObject { fn int_vararg_fn(py: Python<'_>, a: i32, args: &Bound<'_, PyTuple>) -> PyObject {
ext_vararg_fn(py, a, args) ext_vararg_fn(py, a, args)
@ -328,7 +350,7 @@ fn test_module_with_constant() {
// Regression test for #1102 // Regression test for #1102
#[pymodule] #[pymodule]
fn module_with_constant(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_with_constant(m: &Bound<'_, PyModule>) -> PyResult<()> {
const ANON: AnonClass = AnonClass {}; const ANON: AnonClass = AnonClass {};
m.add("ANON", ANON)?; m.add("ANON", ANON)?;
@ -410,7 +432,7 @@ fn pyfunction_with_pass_module_in_attribute(module: &PyModule) -> PyResult<&str>
} }
#[pymodule] #[pymodule]
fn module_with_functions_with_module(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { fn module_with_functions_with_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)?; m.add_function(wrap_pyfunction!(pyfunction_with_module, m)?)?;
m.add_function(wrap_pyfunction!(pyfunction_with_module_gil_ref, m)?)?; m.add_function(wrap_pyfunction!(pyfunction_with_module_gil_ref, m)?)?;
m.add_function(wrap_pyfunction!(pyfunction_with_module_owned, m)?)?; m.add_function(wrap_pyfunction!(pyfunction_with_module_owned, m)?)?;
@ -475,7 +497,7 @@ fn test_module_doc_hidden() {
#[doc(hidden)] #[doc(hidden)]
#[allow(clippy::unnecessary_wraps)] #[allow(clippy::unnecessary_wraps)]
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, _m: &PyModule) -> PyResult<()> { fn my_module(_m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(()) Ok(())
} }

View File

@ -335,7 +335,7 @@ fn test_auto_test_signature_opt_out() {
#[test] #[test]
fn test_pyfn() { fn test_pyfn() {
#[pymodule] #[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
#[pyfn(m, signature = (a, b=None, *, c=42))] #[pyfn(m, signature = (a, b=None, *, c=42))]
#[pyo3(text_signature = "(a, b=None, *, c=42)")] #[pyo3(text_signature = "(a, b=None, *, c=42)")]
fn my_function(a: i32, b: Option<i32>, c: i32) { fn my_function(a: i32, b: Option<i32>, c: i32) {

View File

@ -1,7 +1,7 @@
use pyo3::prelude::*; use pyo3::prelude::*;
#[pymodule(some_arg)] #[pymodule(some_arg)]
fn module(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module(m: &Bound<'_, PyModule>) -> PyResult<()> {
Ok(()) Ok(())
} }