Skip to content

Commit

Permalink
More disambiguation
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 2, 2025
1 parent 0c5f6df commit 4076c62
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 95 deletions.
2 changes: 1 addition & 1 deletion examples/decorator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ impl PyCounter {

#[pymodule]
pub fn decorator(module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_class::<PyCounter>()?;
PyModule::add_class::<PyCounter>(module)?;
Ok(())
}
6 changes: 3 additions & 3 deletions examples/getitem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ExampleContainer {
} else if let Ok(slice) = key.downcast::<PySlice>() {
// 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
Expand Down Expand Up @@ -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
Expand All @@ -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::<ExampleContainer>()?;
PyModule::add_class::<ExampleContainer>(m)?;
Ok(())
}
4 changes: 2 additions & 2 deletions examples/maturin-starter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ExampleClass>()?;
m.add_wrapped(wrap_pymodule!(submodule::submodule))?;
PyModule::add_class::<ExampleClass>(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
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 @@ -17,6 +17,6 @@ impl SubmoduleClass {

#[pymodule]
pub fn submodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<SubmoduleClass>()?;
PyModule::add_class::<SubmoduleClass>(m)?;
Ok(())
}
6 changes: 3 additions & 3 deletions examples/word-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
4 changes: 2 additions & 2 deletions pytests/src/awaitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl FutureAwaitable {

#[pymodule(gil_used = false)]
pub fn awaitable(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<IterAwaitable>()?;
m.add_class::<FutureAwaitable>()?;
PyModule::add_class::<IterAwaitable>(m)?;
PyModule::add_class::<FutureAwaitable>(m)?;
Ok(())
}
6 changes: 3 additions & 3 deletions pytests/src/buf_and_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -49,7 +49,7 @@ fn return_memoryview(py: Python<'_>) -> PyResult<Bound<'_, PyMemoryView>> {

#[pymodule(gil_used = false)]
pub fn buf_and_str(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<BytesExtractor>()?;
m.add_function(wrap_pyfunction!(return_memoryview, m)?)?;
PyModule::add_class::<BytesExtractor>(m)?;
PyModule::add_function(m, wrap_pyfunction!(return_memoryview, m)?)?;
Ok(())
}
10 changes: 5 additions & 5 deletions pytests/src/comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ impl OrderedDefaultNe {

#[pymodule(gil_used = false)]
pub fn comparisons(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Eq>()?;
m.add_class::<EqDefaultNe>()?;
m.add_class::<EqDerived>()?;
m.add_class::<Ordered>()?;
m.add_class::<OrderedDefaultNe>()?;
PyModule::add_class::<Eq>(m)?;
PyModule::add_class::<EqDefaultNe>(m)?;
PyModule::add_class::<EqDerived>(m)?;
PyModule::add_class::<Ordered>(m)?;
PyModule::add_class::<OrderedDefaultNe>(m)?;
Ok(())
}
36 changes: 18 additions & 18 deletions pytests/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<TzClass>()?;
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::<TzClass>(m)?;

Ok(())
}
2 changes: 1 addition & 1 deletion pytests/src/dict_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::types::PyDict;

#[pymodule]
pub fn dict_iter(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<DictSize>()?;
PyModule::add_class::<DictSize>(m)?;
Ok(())
}

Expand Down
18 changes: 9 additions & 9 deletions pytests/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<SimpleEnum>()?;
m.add_class::<ComplexEnum>()?;
m.add_class::<SimpleTupleEnum>()?;
m.add_class::<TupleEnum>()?;
m.add_class::<MixedComplexEnum>()?;
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::<SimpleEnum>(m)?;
PyModule::add_class::<ComplexEnum>(m)?;
PyModule::add_class::<SimpleTupleEnum>(m)?;
PyModule::add_class::<TupleEnum>(m)?;
PyModule::add_class::<MixedComplexEnum>(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(())
}

Expand Down
28 changes: 14 additions & 14 deletions pytests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions pytests/src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pyo3::{
prelude::*,
types::{PyDict, PyString},
types::{PyDict, PyString, PyType},
};

#[pyfunction]
Expand All @@ -11,7 +11,7 @@ fn issue_219() {

#[pyfunction]
fn get_type_fully_qualified_name<'py>(obj: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyString>> {
obj.get_type().fully_qualified_name()
PyType::fully_qualified_name(&obj.get_type())
}

#[pyfunction]
Expand All @@ -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);
Expand All @@ -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(())
}
2 changes: 1 addition & 1 deletion pytests/src/objstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ impl ObjStore {

#[pymodule(gil_used = false)]
pub fn objstore(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<ObjStore>()
PyModule::add_class::<ObjStore>(m)
}
8 changes: 4 additions & 4 deletions pytests/src/othermod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<ModClass>()?;
PyModule::add_class::<ModClass>(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(())
}
4 changes: 2 additions & 2 deletions pytests/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
12 changes: 6 additions & 6 deletions pytests/src/pyclasses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ impl ClassWithDict {

#[pymodule(gil_used = false)]
pub fn pyclasses(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<EmptyClass>()?;
m.add_class::<PyClassIter>()?;
m.add_class::<PyClassThreadIter>()?;
m.add_class::<AssertingBaseClass>()?;
m.add_class::<ClassWithoutConstructor>()?;
PyModule::add_class::<EmptyClass>(m)?;
PyModule::add_class::<PyClassIter>(m)?;
PyModule::add_class::<PyClassThreadIter>(m)?;
PyModule::add_class::<AssertingBaseClass>(m)?;
PyModule::add_class::<ClassWithoutConstructor>(m)?;
#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
m.add_class::<ClassWithDict>()?;
PyModule::add_class::<ClassWithDict>(m)?;

Ok(())
}
12 changes: 6 additions & 6 deletions pytests/src/pyfunctions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
6 changes: 3 additions & 3 deletions pytests/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ fn vec_to_vec_pystring(vec: Vec<Bound<'_, PyString>>) -> Vec<Bound<'_, PyString>

#[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(())
}
2 changes: 1 addition & 1 deletion pytests/src/subclassing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ impl Subclassable {

#[pymodule(gil_used = false)]
pub fn subclassing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Subclassable>()?;
PyModule::add_class::<Subclassable>(m)?;
Ok(())
}
2 changes: 1 addition & 1 deletion tests/test_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl ClassMethod {
fn method_owned(cls: Py<PyType>, py: Python<'_>) -> PyResult<String> {
Ok(format!(
"{}.method_owned()!",
PyType::qualname(&cls.bind(py))?
PyType::qualname(cls.bind(py))?
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ fn pyfunction_with_module_owned(
module: Py<PyModule>,
py: Python<'_>,
) -> PyResult<Bound<'_, PyString>> {
PyModule::name(&module.bind(py))
PyModule::name(module.bind(py))
}

#[pyfunction]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_pyself.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Iter {
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult<Option<PyObject>> {
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;
Expand Down

0 comments on commit 4076c62

Please sign in to comment.