Skip to content

Commit

Permalink
pyo3_path, part 3: move test_hygiene into lib crate
Browse files Browse the repository at this point in the history
This removes the crate-root `pyo3` item to ensure that our
selected pyo3_path needs to be taken into account.
  • Loading branch information
birkenfeld committed Dec 8, 2021
1 parent d33b319 commit b58826e
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 183 deletions.
5 changes: 5 additions & 0 deletions src/lib.rs
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions 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);
5 changes: 5 additions & 0 deletions src/test_hygiene/mod.rs
@@ -0,0 +1,5 @@
mod misc;
mod pyclass;
mod pyfunction;
mod pymethods;
mod pymodule;
13 changes: 8 additions & 5 deletions tests/hygiene/pyclass.rs → 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<Foo2>>,
c: ::std::option::Option<crate::Py<Foo2>>,
}
16 changes: 16 additions & 0 deletions 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<i32> {
::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)"#);
});
}
103 changes: 53 additions & 50 deletions tests/hygiene/pymethods.rs → 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
Expand All @@ -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 {
Expand Down Expand Up @@ -60,20 +63,20 @@ 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")
}

fn __setattr__(&mut self, name: ::std::string::String, value: ::std::string::String) {}

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])
}

//////////////////////
Expand All @@ -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
Expand All @@ -102,24 +105,24 @@ impl Dummy {
0
}

fn __getitem__(&self, key: u32) -> ::pyo3::PyResult<u32> {
::std::result::Result::Err(::pyo3::exceptions::PyKeyError::new_err("boo"))
fn __getitem__(&self, key: u32) -> crate::PyResult<u32> {
::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<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
::pyo3::Py::new(py, DummyIter {}).unwrap()
fn __iter__(_: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
crate::Py::new(py, DummyIter {}).unwrap()
}

fn __next__(&mut self) -> ::std::option::Option<()> {
::std::option::Option::None
}

fn __reversed__(slf: ::pyo3::pycell::PyRef<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
::pyo3::Py::new(py, DummyIter {}).unwrap()
fn __reversed__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
crate::Py::new(py, DummyIter {}).unwrap()
}

fn __contains__(&self, item: u32) -> bool {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -258,24 +261,24 @@ impl Dummy {

fn __ior__(&mut self, other: &Self) {}

fn __neg__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __neg__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
slf
}

fn __pos__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __pos__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
slf
}

fn __abs__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __abs__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
slf
}

fn __invert__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __invert__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
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 {
Expand Down Expand Up @@ -314,17 +317,17 @@ 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,
) {
}

//////////////////////
// Awaitable Objects
//////////////////////

fn __await__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __await__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
slf
}

Expand All @@ -333,8 +336,8 @@ impl Dummy {
// Asynchronous Iterators
//////////////////////

fn __aiter__(slf: ::pyo3::pycell::PyRef<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
::pyo3::Py::new(py, DummyIter {}).unwrap()
fn __aiter__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
crate::Py::new(py, DummyIter {}).unwrap()
}

fn __anext__(&mut self) -> ::std::option::Option<()> {
Expand All @@ -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,
) {
}

Expand All @@ -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<i32> {
_args: &crate::types::PyTuple,
_kwds: ::std::option::Option<&crate::types::PyDict>,
) -> crate::PyResult<i32> {
::std::panic!("unimplemented isn't hygienic before 1.50")
}
#[new]
Expand All @@ -391,8 +394,8 @@ impl Dummy {
fn __richcmp__(
&self,
other: &Self,
op: ::pyo3::class::basic::CompareOp,
) -> ::pyo3::PyResult<bool> {
op: crate::class::basic::CompareOp,
) -> crate::PyResult<bool> {
::std::result::Result::Ok(false)
}
// PyGcProtocol
Expand Down
23 changes: 23 additions & 0 deletions 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<i32> {
::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(())
}

0 comments on commit b58826e

Please sign in to comment.