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.
This commit is contained in:
Georg Brandl 2021-12-08 10:51:06 +01:00
parent e4f608f605
commit 681217d8d9
12 changed files with 139 additions and 183 deletions

View File

@ -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(all(test, feature = "macros"))]
mod test_hygiene;
/// Test readme and user guide
#[cfg(doctest)]
pub mod doc_test {

29
src/test_hygiene/misc.rs Normal file
View File

@ -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
src/test_hygiene/mod.rs Normal file
View File

@ -0,0 +1,5 @@
mod misc;
mod pyclass;
mod pyfunction;
mod pymethods;
mod pymodule;

View File

@ -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>>,
}

View File

@ -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)"#);
});
}

View File

@ -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<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 {
@ -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<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 {
@ -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<Self>) -> ::pyo3::pycell::PyRef<Self> {
fn __await__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
slf
}
@ -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<()> {
@ -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<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]
@ -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

View File

@ -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(())
}

View File

@ -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);

View File

@ -1,15 +0,0 @@
#![no_implicit_prelude]
#![allow(unused_variables)]
#[::pyo3::pyfunction]
fn do_something(x: i32) -> ::pyo3::PyResult<i32> {
::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)"#);
});
}

View File

@ -1,20 +0,0 @@
#![no_implicit_prelude]
#![allow(unused_variables)]
#[::pyo3::pyfunction]
fn do_something(x: i32) -> ::pyo3::PyResult<i32> {
::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(())
}

View File

@ -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<Foo2>>,
}
#[::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<Self>,
_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<Self>, _v: *mut ::pyo3::ffi::Py_buffer) {}
}

View File

@ -1,10 +0,0 @@
#![cfg(feature = "macros")]
mod hygiene {
mod misc;
mod pyclass;
mod pyfunction;
mod pymethods;
mod pymodule;
mod pyproto;
}