From b58826e434fcc5205a2cf87958a61024e44f1b1f Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 8 Dec 2021 10:51:06 +0100 Subject: [PATCH] pyo3_path, part 3: move test_hygiene into lib crate This removes the crate-root `pyo3` item to ensure that our selected pyo3_path needs to be taken into account. --- src/lib.rs | 5 + src/test_hygiene/misc.rs | 29 +++++ src/test_hygiene/mod.rs | 5 + .../hygiene => src/test_hygiene}/pyclass.rs | 13 ++- src/test_hygiene/pyfunction.rs | 16 +++ .../hygiene => src/test_hygiene}/pymethods.rs | 103 +++++++++--------- src/test_hygiene/pymodule.rs | 23 ++++ tests/hygiene/misc.rs | 25 ----- tests/hygiene/pyfunction.rs | 15 --- tests/hygiene/pymodule.rs | 20 ---- tests/hygiene/pyproto.rs | 58 ---------- tests/test_hygiene.rs | 10 -- 12 files changed, 139 insertions(+), 183 deletions(-) create mode 100644 src/test_hygiene/misc.rs create mode 100644 src/test_hygiene/mod.rs rename {tests/hygiene => src/test_hygiene}/pyclass.rs (60%) create mode 100644 src/test_hygiene/pyfunction.rs rename {tests/hygiene => src/test_hygiene}/pymethods.rs (67%) create mode 100644 src/test_hygiene/pymodule.rs delete mode 100644 tests/hygiene/misc.rs delete mode 100644 tests/hygiene/pyfunction.rs delete mode 100644 tests/hygiene/pymodule.rs delete mode 100644 tests/hygiene/pyproto.rs delete mode 100644 tests/test_hygiene.rs diff --git a/src/lib.rs b/src/lib.rs index 7d7d44b7dcb..01d568c06b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -360,6 +360,11 @@ pub use pyo3_macros::{pyclass, pyfunction, pymethods, pymodule, pyproto, FromPyO #[macro_use] mod macros; +/// Test macro hygiene - this is in the crate since we won't have +/// `pyo3` available in the crate root. +#[cfg(test)] +mod test_hygiene; + /// Test readme and user guide #[cfg(doctest)] pub mod doc_test { diff --git a/src/test_hygiene/misc.rs b/src/test_hygiene/misc.rs new file mode 100644 index 00000000000..75fe8fb8f30 --- /dev/null +++ b/src/test_hygiene/misc.rs @@ -0,0 +1,29 @@ +#![no_implicit_prelude] + +#[derive(crate::FromPyObject)] +#[pyo3(pyo3_path = "crate")] +struct Derive1(i32); // newtype case + +#[derive(crate::FromPyObject)] +#[pyo3(pyo3_path = "crate")] +#[allow(dead_code)] +struct Derive2(i32, i32); // tuple case + +#[derive(crate::FromPyObject)] +#[pyo3(pyo3_path = "crate")] +#[allow(dead_code)] +struct Derive3 { + f: i32, + g: i32, +} // struct case + +#[derive(crate::FromPyObject)] +#[pyo3(pyo3_path = "crate")] +#[allow(dead_code)] +enum Derive4 { + A(i32), + B { f: i32 }, +} // enum case + +crate::create_exception!(mymodule, CustomError, crate::exceptions::PyException); +crate::import_exception!(socket, gaierror); diff --git a/src/test_hygiene/mod.rs b/src/test_hygiene/mod.rs new file mode 100644 index 00000000000..d907d2e88e1 --- /dev/null +++ b/src/test_hygiene/mod.rs @@ -0,0 +1,5 @@ +mod misc; +mod pyclass; +mod pyfunction; +mod pymethods; +mod pymodule; diff --git a/tests/hygiene/pyclass.rs b/src/test_hygiene/pyclass.rs similarity index 60% rename from tests/hygiene/pyclass.rs rename to src/test_hygiene/pyclass.rs index 985d05dc2da..9649c4b7471 100644 --- a/tests/hygiene/pyclass.rs +++ b/src/test_hygiene/pyclass.rs @@ -1,27 +1,30 @@ #![no_implicit_prelude] #![allow(unused_variables)] -#[::pyo3::pyclass] +#[crate::pyclass] +#[pyo3(pyo3_path = "crate")] #[derive(::std::clone::Clone)] pub struct Foo; -#[::pyo3::pyclass] +#[crate::pyclass] +#[pyo3(pyo3_path = "crate")] pub struct Foo2; -#[::pyo3::pyclass( +#[crate::pyclass( name = "ActuallyBar", freelist = 8, weakref, unsendable, subclass, - extends = ::pyo3::types::PyAny, + extends = crate::types::PyAny, module = "Spam" )] +#[pyo3(pyo3_path = "crate")] pub struct Bar { #[pyo3(get, set)] a: u8, #[pyo3(get, set)] b: Foo, #[pyo3(get, set)] - c: ::std::option::Option<::pyo3::Py>, + c: ::std::option::Option>, } diff --git a/src/test_hygiene/pyfunction.rs b/src/test_hygiene/pyfunction.rs new file mode 100644 index 00000000000..b95345891c1 --- /dev/null +++ b/src/test_hygiene/pyfunction.rs @@ -0,0 +1,16 @@ +#![no_implicit_prelude] +#![allow(unused_variables)] + +#[crate::pyfunction] +#[pyo3(pyo3_path = "crate")] +fn do_something(x: i32) -> crate::PyResult { + ::std::result::Result::Ok(x) +} + +#[test] +fn invoke_wrap_pyfunction() { + crate::Python::with_gil(|py| { + let func = crate::wrap_pyfunction!(do_something)(py).unwrap(); + crate::py_run!(py, func, r#"func(5)"#); + }); +} diff --git a/tests/hygiene/pymethods.rs b/src/test_hygiene/pymethods.rs similarity index 67% rename from tests/hygiene/pymethods.rs rename to src/test_hygiene/pymethods.rs index 37a916f9c6b..fe4f792eb9d 100644 --- a/tests/hygiene/pymethods.rs +++ b/src/test_hygiene/pymethods.rs @@ -1,13 +1,16 @@ #![no_implicit_prelude] #![allow(unused_variables)] -#[::pyo3::pyclass] +#[crate::pyclass] +#[pyo3(pyo3_path = "crate")] pub struct Dummy; -#[::pyo3::pyclass] +#[crate::pyclass] +#[pyo3(pyo3_path = "crate")] pub struct DummyIter; -#[::pyo3::pymethods] +#[crate::pymethods] +#[pyo3(pyo3_path = "crate")] impl Dummy { ////////////////////// // Basic customization @@ -20,8 +23,8 @@ impl Dummy { "Dummy" } - fn __bytes__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyBytes { - ::pyo3::types::PyBytes::new(py, &[0]) + fn __bytes__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyBytes { + crate::types::PyBytes::new(py, &[0]) } fn __format__(&self, format_spec: ::std::string::String) -> ::std::string::String { @@ -60,11 +63,11 @@ impl Dummy { // Customizing attribute access ////////////////////// - fn __getattr__(&self, name: ::std::string::String) -> &::pyo3::PyAny { + fn __getattr__(&self, name: ::std::string::String) -> &crate::PyAny { ::std::panic!("unimplemented isn't hygienic before 1.50") } - fn __getattribute__(&self, name: ::std::string::String) -> &::pyo3::PyAny { + fn __getattribute__(&self, name: ::std::string::String) -> &crate::PyAny { ::std::panic!("unimplemented isn't hygienic before 1.50") } @@ -72,8 +75,8 @@ impl Dummy { fn __delattr__(&mut self, name: ::std::string::String) {} - fn __dir__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyList { - ::pyo3::types::PyList::new(py, ::std::vec![0_u8]) + fn __dir__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyList { + crate::types::PyList::new(py, ::std::vec![0_u8]) } ////////////////////// @@ -82,17 +85,17 @@ impl Dummy { fn __get__( &self, - instance: &::pyo3::PyAny, - owner: &::pyo3::PyAny, - ) -> ::pyo3::PyResult<&::pyo3::PyAny> { + instance: &crate::PyAny, + owner: &crate::PyAny, + ) -> crate::PyResult<&crate::PyAny> { ::std::panic!("unimplemented isn't hygienic before 1.50") } - fn __set__(&self, instance: &::pyo3::PyAny, owner: &::pyo3::PyAny) {} + fn __set__(&self, instance: &crate::PyAny, owner: &crate::PyAny) {} - fn __delete__(&self, instance: &::pyo3::PyAny) {} + fn __delete__(&self, instance: &crate::PyAny) {} - fn __set_name__(&self, owner: &::pyo3::PyAny, name: &::pyo3::PyAny) {} + fn __set_name__(&self, owner: &crate::PyAny, name: &crate::PyAny) {} ////////////////////// // Implementing Descriptors @@ -102,24 +105,24 @@ impl Dummy { 0 } - fn __getitem__(&self, key: u32) -> ::pyo3::PyResult { - ::std::result::Result::Err(::pyo3::exceptions::PyKeyError::new_err("boo")) + fn __getitem__(&self, key: u32) -> crate::PyResult { + ::std::result::Result::Err(crate::exceptions::PyKeyError::new_err("boo")) } fn __setitem__(&self, key: u32, value: u32) {} fn __delitem__(&self, key: u32) {} - fn __iter__(_: ::pyo3::pycell::PyRef, py: ::pyo3::Python) -> ::pyo3::Py { - ::pyo3::Py::new(py, DummyIter {}).unwrap() + fn __iter__(_: crate::pycell::PyRef, py: crate::Python) -> crate::Py { + crate::Py::new(py, DummyIter {}).unwrap() } fn __next__(&mut self) -> ::std::option::Option<()> { ::std::option::Option::None } - fn __reversed__(slf: ::pyo3::pycell::PyRef, py: ::pyo3::Python) -> ::pyo3::Py { - ::pyo3::Py::new(py, DummyIter {}).unwrap() + fn __reversed__(slf: crate::pycell::PyRef, py: crate::Python) -> crate::Py { + crate::Py::new(py, DummyIter {}).unwrap() } fn __contains__(&self, item: u32) -> bool { @@ -142,12 +145,12 @@ impl Dummy { Dummy {} } - fn __truediv__(&self, _other: &Self) -> ::pyo3::PyResult<()> { - ::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo")) + fn __truediv__(&self, _other: &Self) -> crate::PyResult<()> { + ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) } - fn __floordiv__(&self, _other: &Self) -> ::pyo3::PyResult<()> { - ::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo")) + fn __floordiv__(&self, _other: &Self) -> crate::PyResult<()> { + ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) } fn __mod__(&self, _other: &Self) -> u32 { @@ -194,12 +197,12 @@ impl Dummy { Dummy {} } - fn __rtruediv__(&self, _other: &Self) -> ::pyo3::PyResult<()> { - ::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo")) + fn __rtruediv__(&self, _other: &Self) -> crate::PyResult<()> { + ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) } - fn __rfloordiv__(&self, _other: &Self) -> ::pyo3::PyResult<()> { - ::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo")) + fn __rfloordiv__(&self, _other: &Self) -> crate::PyResult<()> { + ::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo")) } fn __rmod__(&self, _other: &Self) -> u32 { @@ -258,24 +261,24 @@ impl Dummy { fn __ior__(&mut self, other: &Self) {} - fn __neg__(slf: ::pyo3::pycell::PyRef) -> ::pyo3::pycell::PyRef { + fn __neg__(slf: crate::pycell::PyRef) -> crate::pycell::PyRef { slf } - fn __pos__(slf: ::pyo3::pycell::PyRef) -> ::pyo3::pycell::PyRef { + fn __pos__(slf: crate::pycell::PyRef) -> crate::pycell::PyRef { slf } - fn __abs__(slf: ::pyo3::pycell::PyRef) -> ::pyo3::pycell::PyRef { + fn __abs__(slf: crate::pycell::PyRef) -> crate::pycell::PyRef { slf } - fn __invert__(slf: ::pyo3::pycell::PyRef) -> ::pyo3::pycell::PyRef { + fn __invert__(slf: crate::pycell::PyRef) -> crate::pycell::PyRef { slf } - fn __complex__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyComplex { - ::pyo3::types::PyComplex::from_doubles(py, 0.0, 0.0) + fn __complex__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyComplex { + crate::types::PyComplex::from_doubles(py, 0.0, 0.0) } fn __int__(&self) -> u32 { @@ -314,9 +317,9 @@ impl Dummy { fn __exit__( &mut self, - exc_type: &::pyo3::PyAny, - exc_value: &::pyo3::PyAny, - traceback: &::pyo3::PyAny, + exc_type: &crate::PyAny, + exc_value: &crate::PyAny, + traceback: &crate::PyAny, ) { } @@ -324,7 +327,7 @@ impl Dummy { // Awaitable Objects ////////////////////// - fn __await__(slf: ::pyo3::pycell::PyRef) -> ::pyo3::pycell::PyRef { + fn __await__(slf: crate::pycell::PyRef) -> crate::pycell::PyRef { slf } @@ -333,8 +336,8 @@ impl Dummy { // Asynchronous Iterators ////////////////////// - fn __aiter__(slf: ::pyo3::pycell::PyRef, py: ::pyo3::Python) -> ::pyo3::Py { - ::pyo3::Py::new(py, DummyIter {}).unwrap() + fn __aiter__(slf: crate::pycell::PyRef, py: crate::Python) -> crate::Py { + crate::Py::new(py, DummyIter {}).unwrap() } fn __anext__(&mut self) -> ::std::option::Option<()> { @@ -349,9 +352,9 @@ impl Dummy { fn __aexit__( &mut self, - exc_type: &::pyo3::PyAny, - exc_value: &::pyo3::PyAny, - traceback: &::pyo3::PyAny, + exc_type: &crate::PyAny, + exc_value: &crate::PyAny, + traceback: &crate::PyAny, ) { } @@ -362,13 +365,13 @@ impl Dummy { #[staticmethod] fn staticmethod() {} #[classmethod] - fn clsmethod(_: &::pyo3::types::PyType) {} + fn clsmethod(_: &crate::types::PyType) {} #[args(args = "*", kwds = "**")] fn __call__( &self, - _args: &::pyo3::types::PyTuple, - _kwds: ::std::option::Option<&::pyo3::types::PyDict>, - ) -> ::pyo3::PyResult { + _args: &crate::types::PyTuple, + _kwds: ::std::option::Option<&crate::types::PyDict>, + ) -> crate::PyResult { ::std::panic!("unimplemented isn't hygienic before 1.50") } #[new] @@ -391,8 +394,8 @@ impl Dummy { fn __richcmp__( &self, other: &Self, - op: ::pyo3::class::basic::CompareOp, - ) -> ::pyo3::PyResult { + op: crate::class::basic::CompareOp, + ) -> crate::PyResult { ::std::result::Result::Ok(false) } // PyGcProtocol diff --git a/src/test_hygiene/pymodule.rs b/src/test_hygiene/pymodule.rs new file mode 100644 index 00000000000..d6427512112 --- /dev/null +++ b/src/test_hygiene/pymodule.rs @@ -0,0 +1,23 @@ +#![no_implicit_prelude] +#![allow(unused_variables)] + +#[crate::pyfunction] +#[pyo3(pyo3_path = "crate")] +fn do_something(x: i32) -> crate::PyResult { + ::std::result::Result::Ok(x) +} + +#[crate::pymodule] +#[pyo3(pyo3_path = "crate")] +fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> { + ::std::result::Result::Ok(()) +} + +#[crate::pymodule] +#[pyo3(pyo3_path = "crate")] +fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> { + m.add_function(crate::wrap_pyfunction!(do_something, m)?)?; + m.add_wrapped(crate::wrap_pymodule!(foo))?; + + ::std::result::Result::Ok(()) +} diff --git a/tests/hygiene/misc.rs b/tests/hygiene/misc.rs deleted file mode 100644 index abd0cc6aee7..00000000000 --- a/tests/hygiene/misc.rs +++ /dev/null @@ -1,25 +0,0 @@ -#![no_implicit_prelude] - -#[derive(::pyo3::FromPyObject)] -struct Derive1(i32); // newtype case - -#[derive(::pyo3::FromPyObject)] -#[allow(dead_code)] -struct Derive2(i32, i32); // tuple case - -#[derive(::pyo3::FromPyObject)] -#[allow(dead_code)] -struct Derive3 { - f: i32, - g: i32, -} // struct case - -#[derive(::pyo3::FromPyObject)] -#[allow(dead_code)] -enum Derive4 { - A(i32), - B { f: i32 }, -} // enum case - -::pyo3::create_exception!(mymodule, CustomError, ::pyo3::exceptions::PyException); -::pyo3::import_exception!(socket, gaierror); diff --git a/tests/hygiene/pyfunction.rs b/tests/hygiene/pyfunction.rs deleted file mode 100644 index ba1402cdf28..00000000000 --- a/tests/hygiene/pyfunction.rs +++ /dev/null @@ -1,15 +0,0 @@ -#![no_implicit_prelude] -#![allow(unused_variables)] - -#[::pyo3::pyfunction] -fn do_something(x: i32) -> ::pyo3::PyResult { - ::std::result::Result::Ok(x) -} - -#[test] -fn invoke_wrap_pyfunction() { - ::pyo3::Python::with_gil(|py| { - let func = ::pyo3::wrap_pyfunction!(do_something)(py).unwrap(); - ::pyo3::py_run!(py, func, r#"func(5)"#); - }); -} diff --git a/tests/hygiene/pymodule.rs b/tests/hygiene/pymodule.rs deleted file mode 100644 index 8bb5e19ec58..00000000000 --- a/tests/hygiene/pymodule.rs +++ /dev/null @@ -1,20 +0,0 @@ -#![no_implicit_prelude] -#![allow(unused_variables)] - -#[::pyo3::pyfunction] -fn do_something(x: i32) -> ::pyo3::PyResult { - ::std::result::Result::Ok(x) -} - -#[::pyo3::pymodule] -fn foo(_py: ::pyo3::Python, _m: &::pyo3::types::PyModule) -> ::pyo3::PyResult<()> { - ::std::result::Result::Ok(()) -} - -#[::pyo3::pymodule] -fn my_module(_py: ::pyo3::Python, m: &::pyo3::types::PyModule) -> ::pyo3::PyResult<()> { - m.add_function(::pyo3::wrap_pyfunction!(do_something, m)?)?; - m.add_wrapped(::pyo3::wrap_pymodule!(foo))?; - - ::std::result::Result::Ok(()) -} diff --git a/tests/hygiene/pyproto.rs b/tests/hygiene/pyproto.rs deleted file mode 100644 index 9f6b0af8b8c..00000000000 --- a/tests/hygiene/pyproto.rs +++ /dev/null @@ -1,58 +0,0 @@ -#![no_implicit_prelude] -#![allow(unused_variables)] - -#[::pyo3::pyclass] -#[derive(::std::clone::Clone)] -pub struct Foo; - -#[::pyo3::pyclass] -pub struct Foo2; - -#[::pyo3::pyclass( - name = "ActuallyBar", - freelist = 8, - weakref, - unsendable, - gc, - subclass, - extends = ::pyo3::types::PyAny, - module = "Spam" -)] -pub struct Bar { - #[pyo3(get, set)] - a: u8, - #[pyo3(get, set)] - b: Foo, - #[pyo3(get, set)] - c: ::std::option::Option<::pyo3::Py>, -} - -#[::pyo3::pyproto] -impl ::pyo3::class::gc::PyGCProtocol for Bar { - fn __traverse__( - &self, - visit: ::pyo3::class::gc::PyVisit, - ) -> ::std::result::Result<(), ::pyo3::class::gc::PyTraverseError> { - if let ::std::option::Option::Some(obj) = &self.c { - visit.call(obj)? - } - ::std::result::Result::Ok(()) - } - - fn __clear__(&mut self) { - self.c = ::std::option::Option::None; - } -} - -#[cfg(not(Py_LIMITED_API))] -#[::pyo3::pyproto] -impl ::pyo3::class::PyBufferProtocol for Bar { - fn bf_getbuffer( - _s: ::pyo3::PyRefMut, - _v: *mut ::pyo3::ffi::Py_buffer, - _f: ::std::os::raw::c_int, - ) -> ::pyo3::PyResult<()> { - ::std::panic!("unimplemented isn't hygienic before 1.50") - } - fn bf_releasebuffer(_s: ::pyo3::PyRefMut, _v: *mut ::pyo3::ffi::Py_buffer) {} -} diff --git a/tests/test_hygiene.rs b/tests/test_hygiene.rs deleted file mode 100644 index 09289457085..00000000000 --- a/tests/test_hygiene.rs +++ /dev/null @@ -1,10 +0,0 @@ -#![cfg(feature = "macros")] - -mod hygiene { - mod misc; - mod pyclass; - mod pyfunction; - mod pymethods; - mod pymodule; - mod pyproto; -}