Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// import the module.
#[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)?)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/decorator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl PyCounter {
}

#[pymodule]
pub fn decorator(_py: Python<'_>, module: &PyModule) -> PyResult<()> {
pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_class::<PyCounter>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/getitem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ExampleContainer {

#[pymodule]
#[pyo3(name = "getitem")]
fn example(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn example(m: &Bound<'_, PyModule>) -> PyResult<()> {
// ? -https://github.com/PyO3/maturin/issues/475
m.add_class::<ExampleContainer>()?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/maturin-starter/src/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl SubmoduleClass {
}

#[pymodule]
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/plugin/plugin_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Gadget {

/// A Python module for plugin interface types
#[pymodule]
pub fn plugin_api(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn plugin_api(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Gadget>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/setuptools-rust-starter/src/submodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl SubmoduleClass {
}

#[pymodule]
pub fn submodule(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn count_line(line: &str, needle: &str) -> usize {
}

#[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_sequential, m)?)?;
m.add_function(wrap_pyfunction!(search_sequential_allow_threads, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ The next step is to create the module initializer and add our class to it:
# struct Number(i32);
#
#[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion guide/src/class/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Number {
}

#[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions guide/src/class/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Number {
}

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

#[pymodule]
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Number>()?;
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions guide/src/ecosystem/async-await.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
}

#[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)?)?;

Ok(())
Expand All @@ -151,7 +151,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
}

#[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)?)?;
Ok(())
}
Expand Down Expand Up @@ -475,7 +475,7 @@ fn rust_sleep(py: Python<'_>) -> PyResult<&PyAny> {
}

#[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)?)?;

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions guide/src/ecosystem/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ fn log_something() {
}

#[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.
pyo3_log::init();

m.add_function(wrap_pyfunction!(log_something))?;
m.add_function(wrap_pyfunction!(log_something, m)?)?;
Ok(())
}
```
Expand Down
12 changes: 6 additions & 6 deletions guide/src/function.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn double(x: usize) -> usize {
}

#[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(double, m)?)?;
Ok(())
}
Expand Down Expand Up @@ -55,7 +55,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
}

#[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)?)?;
Ok(())
}
Expand Down Expand Up @@ -92,7 +92,7 @@ The `#[pyo3]` attribute can be used to modify properties of the generated Python
}

#[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)?)
}
```
Expand Down Expand Up @@ -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
arguments from the input `PyObject`s.

The `wrap_pyfunction` macro can be used to directly get a `PyCFunction` given a
`#[pyfunction]` and a `PyModule`: `wrap_pyfunction!(rust_fun, module)`.
The `wrap_pyfunction` macro can be used to directly get a `Bound<PyCFunction>` given a
`#[pyfunction]` and a `Bound<PyModule>`: `wrap_pyfunction!(rust_fun, module)`.

## `#[pyfn]` shorthand

Expand Down Expand Up @@ -197,7 +197,7 @@ documented in the rest of this chapter. The code above is expanded to the follow
use pyo3::prelude::*;

#[pymodule]
fn my_extension(py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn my_extension(m: &Bound<'_, PyModule>) -> PyResult<()> {
#[pyfunction]
fn double(x: usize) -> usize {
x * 2
Expand Down
2 changes: 1 addition & 1 deletion guide/src/function/signature.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn num_kwds(kwds: Option<&PyDict>) -> usize {
}

#[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();
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion guide/src/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// import the module.
#[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)?)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions guide/src/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn double(x: usize) -> usize {

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

#[pymodule]
#[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)?)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion guide/src/python_from_rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ fn add_one(x: i64) -> i64 {
}

#[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)?)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions guide/src/trait_bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ struct UserModel {
}

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

#[pymodule]
fn trait_exposure(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
fn trait_exposure(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<UserModel>()?;
m.add_function(wrap_pyfunction!(solve_wrapper, m)?)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/awaitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl FutureAwaitable {
}

#[pymodule]
pub fn awaitable(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<IterAwaitable>()?;
m.add_class::<FutureAwaitable>()?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/buf_and_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn return_memoryview(py: Python<'_>) -> PyResult<Bound<'_, PyMemoryView>> {
}

#[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_function(wrap_pyfunction!(return_memoryview, m)?)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl OrderedDefaultNe {
}

#[pymodule]
pub fn comparisons(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Eq>()?;
m.add_class::<EqDefaultNe>()?;
m.add_class::<Ordered>()?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl TzClass {
}

#[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!(get_date_tuple, m)?)?;
m.add_function(wrap_pyfunction!(date_from_timestamp, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/dict_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use pyo3::prelude::*;
use pyo3::types::PyDict;

#[pymodule]
pub fn dict_iter(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<DictSize>()?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions pytests/src/enums.rs
Original file line number Diff line number Diff line change
@@ -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]
pub fn enums(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn enums(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SimpleEnum>()?;
m.add_class::<ComplexEnum>()?;
m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny>
}

#[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!(get_type_full_name, m)?)?;
m.add_function(wrap_pyfunction!(accepts_bool, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/objstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ impl ObjStore {
}

#[pymodule]
pub fn objstore(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ObjStore>()
}
2 changes: 1 addition & 1 deletion pytests/src/othermod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn double(x: i32) -> i32 {
}

#[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_class::<ModClass>()?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn take_pathbuf(path: PathBuf) -> PathBuf {
}

#[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!(take_pathbuf, m)?)?;

Expand Down
2 changes: 1 addition & 1 deletion pytests/src/pyclasses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl AssertingBaseClassGilRef {
struct ClassWithoutConstructor;

#[pymodule]
pub fn pyclasses(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<EmptyClass>()?;
m.add_class::<PyClassIter>()?;
m.add_class::<AssertingBaseClass>()?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/pyfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn args_kwargs<'a>(
}

#[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!(simple, m)?)?;
m.add_function(wrap_pyfunction!(simple_args, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn vec_to_vec_pystring(vec: Vec<&PyString>) -> Vec<&PyString> {
}

#[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!(array_to_array_i32, m)?)?;
m.add_function(wrap_pyfunction!(vec_to_vec_pystring, m)?)?;
Expand Down
2 changes: 1 addition & 1 deletion pytests/src/subclassing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Subclassable {
}

#[pymodule]
pub fn subclassing(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Subclassable>()?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
//! }
//!
//! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(calculate_statistics, m)?)?;
//! Ok(())
//! }
Expand Down
2 changes: 1 addition & 1 deletion src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//! }
//!
//! #[pymodule]
//! fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
//! fn my_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
//! m.add_function(wrap_pyfunction!(add_one, m)?)?;
//! Ok(())
//! }
Expand Down
Loading