diff --git a/examples/decorator/src/lib.rs b/examples/decorator/src/lib.rs index 4c5471c9945..2257fab63d5 100644 --- a/examples/decorator/src/lib.rs +++ b/examples/decorator/src/lib.rs @@ -59,6 +59,6 @@ impl PyCounter { #[pymodule] pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> { - module.add_class::()?; + PyModule::add_class::(module)?; Ok(()) } diff --git a/examples/getitem/src/lib.rs b/examples/getitem/src/lib.rs index ba850a06b8d..9ee1be53e0e 100644 --- a/examples/getitem/src/lib.rs +++ b/examples/getitem/src/lib.rs @@ -28,7 +28,7 @@ impl ExampleContainer { } else if let Ok(slice) = key.downcast::() { // METHOD 1 - the use PySliceIndices to help with bounds checking and for cases when only start or end are provided // in this case the start/stop/step all filled in to give valid values based on the max_length given - let index = slice.indices(self.max_length as isize).unwrap(); + let index = PySlice::indices(&slice, self.max_length as isize).unwrap(); let _delta = index.stop - index.start; // METHOD 2 - Do the getattr manually really only needed if you have some special cases for stop/_step not being present @@ -61,7 +61,7 @@ impl ExampleContainer { fn __setitem__(&self, idx: IntOrSlice, value: u32) -> PyResult<()> { match idx { IntOrSlice::Slice(slice) => { - let index = slice.indices(self.max_length as isize).unwrap(); + let index = PySlice::indices(&slice, self.max_length as isize).unwrap(); println!( "Got a slice! {}-{}, step: {}, value: {}", index.start, index.stop, index.step, value @@ -78,6 +78,6 @@ impl ExampleContainer { #[pymodule(name = "getitem")] fn example(m: &Bound<'_, PyModule>) -> PyResult<()> { // ? -https://github.com/PyO3/maturin/issues/475 - m.add_class::()?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/examples/maturin-starter/src/lib.rs b/examples/maturin-starter/src/lib.rs index 4c2a30d3a5d..2076cac746b 100644 --- a/examples/maturin-starter/src/lib.rs +++ b/examples/maturin-starter/src/lib.rs @@ -21,8 +21,8 @@ impl ExampleClass { /// An example module implemented in Rust using PyO3. #[pymodule] fn maturin_starter(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_wrapped(wrap_pymodule!(submodule::submodule))?; + PyModule::add_class::(m)?; + PyModule::add_wrapped(m, wrap_pymodule!(submodule::submodule))?; // Inserting to sys.modules allows importing submodules nicely from Python // e.g. from maturin_starter.submodule import SubmoduleClass diff --git a/examples/maturin-starter/src/submodule.rs b/examples/maturin-starter/src/submodule.rs index f3eb174100b..ce425974037 100644 --- a/examples/maturin-starter/src/submodule.rs +++ b/examples/maturin-starter/src/submodule.rs @@ -17,6 +17,6 @@ impl SubmoduleClass { #[pymodule] pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/examples/word-count/src/lib.rs b/examples/word-count/src/lib.rs index 5bc73df97a4..5bfce2b8bdf 100644 --- a/examples/word-count/src/lib.rs +++ b/examples/word-count/src/lib.rs @@ -34,9 +34,9 @@ fn count_line(line: &str, needle: &str) -> usize { #[pymodule] 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)?)?; + PyModule::add_function(m, wrap_pyfunction!(search, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(search_sequential, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(search_sequential_allow_threads, m)?)?; Ok(()) } diff --git a/pytests/src/awaitable.rs b/pytests/src/awaitable.rs index fb04c33ed05..6c6b3d4fb1e 100644 --- a/pytests/src/awaitable.rs +++ b/pytests/src/awaitable.rs @@ -80,7 +80,7 @@ impl FutureAwaitable { #[pymodule(gil_used = false)] pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/pytests/src/buf_and_str.rs b/pytests/src/buf_and_str.rs index b42182f2e4e..13e4453af7c 100644 --- a/pytests/src/buf_and_str.rs +++ b/pytests/src/buf_and_str.rs @@ -30,7 +30,7 @@ impl BytesExtractor { #[staticmethod] pub fn from_str_lossy(string: &Bound<'_, PyString>) -> usize { - let rust_string_lossy: String = string.to_string_lossy().to_string(); + let rust_string_lossy: String = PyString::to_string_lossy(string).to_string(); rust_string_lossy.len() } @@ -49,7 +49,7 @@ fn return_memoryview(py: Python<'_>) -> PyResult> { #[pymodule(gil_used = false)] pub fn buf_and_str(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_function(wrap_pyfunction!(return_memoryview, m)?)?; + PyModule::add_class::(m)?; + PyModule::add_function(m, wrap_pyfunction!(return_memoryview, m)?)?; Ok(()) } diff --git a/pytests/src/comparisons.rs b/pytests/src/comparisons.rs index 4ed79e42790..56b5229b7fc 100644 --- a/pytests/src/comparisons.rs +++ b/pytests/src/comparisons.rs @@ -114,10 +114,10 @@ impl OrderedDefaultNe { #[pymodule(gil_used = false)] pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/pytests/src/datetime.rs b/pytests/src/datetime.rs index 5162b3508a5..946438b455c 100644 --- a/pytests/src/datetime.rs +++ b/pytests/src/datetime.rs @@ -205,24 +205,24 @@ impl TzClass { #[pymodule(gil_used = false)] 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)?)?; - m.add_function(wrap_pyfunction!(make_time, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tuple, m)?)?; - m.add_function(wrap_pyfunction!(make_delta, m)?)?; - m.add_function(wrap_pyfunction!(get_delta_tuple, m)?)?; - m.add_function(wrap_pyfunction!(make_datetime, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tuple, m)?)?; - m.add_function(wrap_pyfunction!(datetime_from_timestamp, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tzinfo, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tzinfo, m)?)?; - - m.add_function(wrap_pyfunction!(time_with_fold, m)?)?; - m.add_function(wrap_pyfunction!(get_time_tuple_fold, m)?)?; - m.add_function(wrap_pyfunction!(get_datetime_tuple_fold, m)?)?; - - m.add_class::()?; + PyModule::add_function(m, wrap_pyfunction!(make_date, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_date_tuple, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(date_from_timestamp, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(make_time, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_time_tuple, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(make_delta, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_delta_tuple, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(make_datetime, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_datetime_tuple, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(datetime_from_timestamp, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_datetime_tzinfo, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_time_tzinfo, m)?)?; + + PyModule::add_function(m, wrap_pyfunction!(time_with_fold, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_time_tuple_fold, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_datetime_tuple_fold, m)?)?; + + PyModule::add_class::(m)?; Ok(()) } diff --git a/pytests/src/dict_iter.rs b/pytests/src/dict_iter.rs index c312fbb5f83..f90e2bb29eb 100644 --- a/pytests/src/dict_iter.rs +++ b/pytests/src/dict_iter.rs @@ -4,7 +4,7 @@ use pyo3::types::PyDict; #[pymodule] pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/pytests/src/enums.rs b/pytests/src/enums.rs index cf7f2641e1e..2f01e6071b2 100644 --- a/pytests/src/enums.rs +++ b/pytests/src/enums.rs @@ -2,15 +2,15 @@ use pyo3::{pyclass, pyfunction, pymodule, types::PyModule, wrap_pyfunction, Boun #[pymodule(gil_used = false)] pub fn enums(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_wrapped(wrap_pyfunction!(do_simple_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_complex_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_tuple_stuff))?; - m.add_wrapped(wrap_pyfunction!(do_mixed_complex_stuff))?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_wrapped(m, wrap_pyfunction!(do_simple_stuff))?; + PyModule::add_wrapped(m, wrap_pyfunction!(do_complex_stuff))?; + PyModule::add_wrapped(m, wrap_pyfunction!(do_tuple_stuff))?; + PyModule::add_wrapped(m, wrap_pyfunction!(do_mixed_complex_stuff))?; Ok(()) } diff --git a/pytests/src/lib.rs b/pytests/src/lib.rs index b6c32230dac..4de7f4f4058 100644 --- a/pytests/src/lib.rs +++ b/pytests/src/lib.rs @@ -19,22 +19,22 @@ pub mod subclassing; #[pymodule(gil_used = false)] fn pyo3_pytests(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_wrapped(wrap_pymodule!(awaitable::awaitable))?; + PyModule::add_wrapped(m, wrap_pymodule!(awaitable::awaitable))?; #[cfg(not(Py_LIMITED_API))] - m.add_wrapped(wrap_pymodule!(buf_and_str::buf_and_str))?; - m.add_wrapped(wrap_pymodule!(comparisons::comparisons))?; + PyModule::add_wrapped(m, wrap_pymodule!(buf_and_str::buf_and_str))?; + PyModule::add_wrapped(m, wrap_pymodule!(comparisons::comparisons))?; #[cfg(not(Py_LIMITED_API))] - m.add_wrapped(wrap_pymodule!(datetime::datetime))?; - m.add_wrapped(wrap_pymodule!(dict_iter::dict_iter))?; - m.add_wrapped(wrap_pymodule!(enums::enums))?; - m.add_wrapped(wrap_pymodule!(misc::misc))?; - m.add_wrapped(wrap_pymodule!(objstore::objstore))?; - m.add_wrapped(wrap_pymodule!(othermod::othermod))?; - m.add_wrapped(wrap_pymodule!(path::path))?; - m.add_wrapped(wrap_pymodule!(pyclasses::pyclasses))?; - m.add_wrapped(wrap_pymodule!(pyfunctions::pyfunctions))?; - m.add_wrapped(wrap_pymodule!(sequence::sequence))?; - m.add_wrapped(wrap_pymodule!(subclassing::subclassing))?; + PyModule::add_wrapped(m, wrap_pymodule!(datetime::datetime))?; + PyModule::add_wrapped(m, wrap_pymodule!(dict_iter::dict_iter))?; + PyModule::add_wrapped(m, wrap_pymodule!(enums::enums))?; + PyModule::add_wrapped(m, wrap_pymodule!(misc::misc))?; + PyModule::add_wrapped(m, wrap_pymodule!(objstore::objstore))?; + PyModule::add_wrapped(m, wrap_pymodule!(othermod::othermod))?; + PyModule::add_wrapped(m, wrap_pymodule!(path::path))?; + PyModule::add_wrapped(m, wrap_pymodule!(pyclasses::pyclasses))?; + PyModule::add_wrapped(m, wrap_pymodule!(pyfunctions::pyfunctions))?; + PyModule::add_wrapped(m, wrap_pymodule!(sequence::sequence))?; + PyModule::add_wrapped(m, wrap_pymodule!(subclassing::subclassing))?; // Inserting to sys.modules allows importing submodules nicely from Python // e.g. import pyo3_pytests.buf_and_str as bas diff --git a/pytests/src/misc.rs b/pytests/src/misc.rs index e44d1aa0ecf..36690133af3 100644 --- a/pytests/src/misc.rs +++ b/pytests/src/misc.rs @@ -1,6 +1,6 @@ use pyo3::{ prelude::*, - types::{PyDict, PyString}, + types::{PyDict, PyString, PyType}, }; #[pyfunction] @@ -11,7 +11,7 @@ fn issue_219() { #[pyfunction] fn get_type_fully_qualified_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult> { - obj.get_type().fully_qualified_name() + PyType::fully_qualified_name(&obj.get_type()) } #[pyfunction] @@ -25,7 +25,7 @@ fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny> // gevent can instigate a context switch. This had problematic interactions // with PyO3's removed "GIL Pool". // For context, see https://github.com/PyO3/pyo3/issues/3668 - let item = dict.get_item("key")?.expect("key not found in dict"); + let item = PyDict::get_item(&dict, "key")?.expect("key not found in dict"); let string = item.to_string(); callback.call0()?; assert_eq!(item.to_string(), string); @@ -34,9 +34,9 @@ fn get_item_and_run_callback(dict: Bound<'_, PyDict>, callback: Bound<'_, PyAny> #[pymodule(gil_used = false)] pub fn misc(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(issue_219, m)?)?; - m.add_function(wrap_pyfunction!(get_type_fully_qualified_name, m)?)?; - m.add_function(wrap_pyfunction!(accepts_bool, m)?)?; - m.add_function(wrap_pyfunction!(get_item_and_run_callback, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(issue_219, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_type_fully_qualified_name, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(accepts_bool, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(get_item_and_run_callback, m)?)?; Ok(()) } diff --git a/pytests/src/objstore.rs b/pytests/src/objstore.rs index 8e729052992..abceb3090cf 100644 --- a/pytests/src/objstore.rs +++ b/pytests/src/objstore.rs @@ -20,5 +20,5 @@ impl ObjStore { #[pymodule(gil_used = false)] pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::() + PyModule::add_class::(m) } diff --git a/pytests/src/othermod.rs b/pytests/src/othermod.rs index 0de912d7d04..a9374960708 100644 --- a/pytests/src/othermod.rs +++ b/pytests/src/othermod.rs @@ -30,12 +30,12 @@ fn double(x: i32) -> i32 { #[pymodule(gil_used = false)] pub fn othermod(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(double, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(double, m)?)?; - m.add_class::()?; + PyModule::add_class::(m)?; - m.add("USIZE_MIN", usize::MIN)?; - m.add("USIZE_MAX", usize::MAX)?; + PyModule::add(m, "USIZE_MIN", usize::MIN)?; + PyModule::add(m, "USIZE_MAX", usize::MAX)?; Ok(()) } diff --git a/pytests/src/path.rs b/pytests/src/path.rs index b52c038ed34..73ca890483f 100644 --- a/pytests/src/path.rs +++ b/pytests/src/path.rs @@ -13,8 +13,8 @@ fn take_pathbuf(path: PathBuf) -> PathBuf { #[pymodule(gil_used = false)] pub fn path(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_function(wrap_pyfunction!(make_path, m)?)?; - m.add_function(wrap_pyfunction!(take_pathbuf, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(make_path, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(take_pathbuf, m)?)?; Ok(()) } diff --git a/pytests/src/pyclasses.rs b/pytests/src/pyclasses.rs index 3af08c053cc..cfddd9adc3c 100644 --- a/pytests/src/pyclasses.rs +++ b/pytests/src/pyclasses.rs @@ -106,13 +106,13 @@ impl ClassWithDict { #[pymodule(gil_used = false)] pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; + PyModule::add_class::(m)?; #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] - m.add_class::()?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/pytests/src/pyfunctions.rs b/pytests/src/pyfunctions.rs index 024641d3d2e..cc5680f233e 100644 --- a/pytests/src/pyfunctions.rs +++ b/pytests/src/pyfunctions.rs @@ -69,11 +69,11 @@ fn args_kwargs<'py>( #[pymodule(gil_used = false)] 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)?)?; - m.add_function(wrap_pyfunction!(simple_kwargs, m)?)?; - m.add_function(wrap_pyfunction!(simple_args_kwargs, m)?)?; - m.add_function(wrap_pyfunction!(args_kwargs, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(none, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(simple, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(simple_args, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(simple_kwargs, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(simple_args_kwargs, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(args_kwargs, m)?)?; Ok(()) } diff --git a/pytests/src/sequence.rs b/pytests/src/sequence.rs index 175f5fba8aa..69169bfeb37 100644 --- a/pytests/src/sequence.rs +++ b/pytests/src/sequence.rs @@ -18,8 +18,8 @@ fn vec_to_vec_pystring(vec: Vec>) -> Vec #[pymodule(gil_used = false)] 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)?)?; + PyModule::add_function(m, wrap_pyfunction!(vec_to_vec_i32, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(array_to_array_i32, m)?)?; + PyModule::add_function(m, wrap_pyfunction!(vec_to_vec_pystring, m)?)?; Ok(()) } diff --git a/pytests/src/subclassing.rs b/pytests/src/subclassing.rs index 0f00e74c19d..35426fd1f85 100644 --- a/pytests/src/subclassing.rs +++ b/pytests/src/subclassing.rs @@ -19,6 +19,6 @@ impl Subclassable { #[pymodule(gil_used = false)] pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; + PyModule::add_class::(m)?; Ok(()) } diff --git a/tests/test_methods.rs b/tests/test_methods.rs index 986b4c02430..2c3a6b0b3a7 100644 --- a/tests/test_methods.rs +++ b/tests/test_methods.rs @@ -82,7 +82,7 @@ impl ClassMethod { fn method_owned(cls: Py, py: Python<'_>) -> PyResult { Ok(format!( "{}.method_owned()!", - PyType::qualname(&cls.bind(py))? + PyType::qualname(cls.bind(py))? )) } } diff --git a/tests/test_module.rs b/tests/test_module.rs index 5b02b82f3da..bac5e93b415 100644 --- a/tests/test_module.rs +++ b/tests/test_module.rs @@ -388,7 +388,7 @@ fn pyfunction_with_module_owned( module: Py, py: Python<'_>, ) -> PyResult> { - PyModule::name(&module.bind(py)) + PyModule::name(module.bind(py)) } #[pyfunction] diff --git a/tests/test_pyself.rs b/tests/test_pyself.rs index bcdd9cd37dd..52c38ffff96 100644 --- a/tests/test_pyself.rs +++ b/tests/test_pyself.rs @@ -65,7 +65,7 @@ impl Iter { } fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult> { - let bytes = PyBytes::as_bytes(&slf.keys.bind(slf.py())); + let bytes = PyBytes::as_bytes(slf.keys.bind(slf.py())); match bytes.get(slf.idx) { Some(&b) => { slf.idx += 1;