Skip to content

Commit b58826e

Browse files
committed
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.
1 parent d33b319 commit b58826e

File tree

12 files changed

+139
-183
lines changed

12 files changed

+139
-183
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ pub use pyo3_macros::{pyclass, pyfunction, pymethods, pymodule, pyproto, FromPyO
360360
#[macro_use]
361361
mod macros;
362362

363+
/// Test macro hygiene - this is in the crate since we won't have
364+
/// `pyo3` available in the crate root.
365+
#[cfg(test)]
366+
mod test_hygiene;
367+
363368
/// Test readme and user guide
364369
#[cfg(doctest)]
365370
pub mod doc_test {

src/test_hygiene/misc.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![no_implicit_prelude]
2+
3+
#[derive(crate::FromPyObject)]
4+
#[pyo3(pyo3_path = "crate")]
5+
struct Derive1(i32); // newtype case
6+
7+
#[derive(crate::FromPyObject)]
8+
#[pyo3(pyo3_path = "crate")]
9+
#[allow(dead_code)]
10+
struct Derive2(i32, i32); // tuple case
11+
12+
#[derive(crate::FromPyObject)]
13+
#[pyo3(pyo3_path = "crate")]
14+
#[allow(dead_code)]
15+
struct Derive3 {
16+
f: i32,
17+
g: i32,
18+
} // struct case
19+
20+
#[derive(crate::FromPyObject)]
21+
#[pyo3(pyo3_path = "crate")]
22+
#[allow(dead_code)]
23+
enum Derive4 {
24+
A(i32),
25+
B { f: i32 },
26+
} // enum case
27+
28+
crate::create_exception!(mymodule, CustomError, crate::exceptions::PyException);
29+
crate::import_exception!(socket, gaierror);

src/test_hygiene/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod misc;
2+
mod pyclass;
3+
mod pyfunction;
4+
mod pymethods;
5+
mod pymodule;
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
#![no_implicit_prelude]
22
#![allow(unused_variables)]
33

4-
#[::pyo3::pyclass]
4+
#[crate::pyclass]
5+
#[pyo3(pyo3_path = "crate")]
56
#[derive(::std::clone::Clone)]
67
pub struct Foo;
78

8-
#[::pyo3::pyclass]
9+
#[crate::pyclass]
10+
#[pyo3(pyo3_path = "crate")]
911
pub struct Foo2;
1012

11-
#[::pyo3::pyclass(
13+
#[crate::pyclass(
1214
name = "ActuallyBar",
1315
freelist = 8,
1416
weakref,
1517
unsendable,
1618
subclass,
17-
extends = ::pyo3::types::PyAny,
19+
extends = crate::types::PyAny,
1820
module = "Spam"
1921
)]
22+
#[pyo3(pyo3_path = "crate")]
2023
pub struct Bar {
2124
#[pyo3(get, set)]
2225
a: u8,
2326
#[pyo3(get, set)]
2427
b: Foo,
2528
#[pyo3(get, set)]
26-
c: ::std::option::Option<::pyo3::Py<Foo2>>,
29+
c: ::std::option::Option<crate::Py<Foo2>>,
2730
}

src/test_hygiene/pyfunction.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![no_implicit_prelude]
2+
#![allow(unused_variables)]
3+
4+
#[crate::pyfunction]
5+
#[pyo3(pyo3_path = "crate")]
6+
fn do_something(x: i32) -> crate::PyResult<i32> {
7+
::std::result::Result::Ok(x)
8+
}
9+
10+
#[test]
11+
fn invoke_wrap_pyfunction() {
12+
crate::Python::with_gil(|py| {
13+
let func = crate::wrap_pyfunction!(do_something)(py).unwrap();
14+
crate::py_run!(py, func, r#"func(5)"#);
15+
});
16+
}

tests/hygiene/pymethods.rs renamed to src/test_hygiene/pymethods.rs

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#![no_implicit_prelude]
22
#![allow(unused_variables)]
33

4-
#[::pyo3::pyclass]
4+
#[crate::pyclass]
5+
#[pyo3(pyo3_path = "crate")]
56
pub struct Dummy;
67

7-
#[::pyo3::pyclass]
8+
#[crate::pyclass]
9+
#[pyo3(pyo3_path = "crate")]
810
pub struct DummyIter;
911

10-
#[::pyo3::pymethods]
12+
#[crate::pymethods]
13+
#[pyo3(pyo3_path = "crate")]
1114
impl Dummy {
1215
//////////////////////
1316
// Basic customization
@@ -20,8 +23,8 @@ impl Dummy {
2023
"Dummy"
2124
}
2225

23-
fn __bytes__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyBytes {
24-
::pyo3::types::PyBytes::new(py, &[0])
26+
fn __bytes__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyBytes {
27+
crate::types::PyBytes::new(py, &[0])
2528
}
2629

2730
fn __format__(&self, format_spec: ::std::string::String) -> ::std::string::String {
@@ -60,20 +63,20 @@ impl Dummy {
6063
// Customizing attribute access
6164
//////////////////////
6265

63-
fn __getattr__(&self, name: ::std::string::String) -> &::pyo3::PyAny {
66+
fn __getattr__(&self, name: ::std::string::String) -> &crate::PyAny {
6467
::std::panic!("unimplemented isn't hygienic before 1.50")
6568
}
6669

67-
fn __getattribute__(&self, name: ::std::string::String) -> &::pyo3::PyAny {
70+
fn __getattribute__(&self, name: ::std::string::String) -> &crate::PyAny {
6871
::std::panic!("unimplemented isn't hygienic before 1.50")
6972
}
7073

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

7376
fn __delattr__(&mut self, name: ::std::string::String) {}
7477

75-
fn __dir__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyList {
76-
::pyo3::types::PyList::new(py, ::std::vec![0_u8])
78+
fn __dir__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyList {
79+
crate::types::PyList::new(py, ::std::vec![0_u8])
7780
}
7881

7982
//////////////////////
@@ -82,17 +85,17 @@ impl Dummy {
8285

8386
fn __get__(
8487
&self,
85-
instance: &::pyo3::PyAny,
86-
owner: &::pyo3::PyAny,
87-
) -> ::pyo3::PyResult<&::pyo3::PyAny> {
88+
instance: &crate::PyAny,
89+
owner: &crate::PyAny,
90+
) -> crate::PyResult<&crate::PyAny> {
8891
::std::panic!("unimplemented isn't hygienic before 1.50")
8992
}
9093

91-
fn __set__(&self, instance: &::pyo3::PyAny, owner: &::pyo3::PyAny) {}
94+
fn __set__(&self, instance: &crate::PyAny, owner: &crate::PyAny) {}
9295

93-
fn __delete__(&self, instance: &::pyo3::PyAny) {}
96+
fn __delete__(&self, instance: &crate::PyAny) {}
9497

95-
fn __set_name__(&self, owner: &::pyo3::PyAny, name: &::pyo3::PyAny) {}
98+
fn __set_name__(&self, owner: &crate::PyAny, name: &crate::PyAny) {}
9699

97100
//////////////////////
98101
// Implementing Descriptors
@@ -102,24 +105,24 @@ impl Dummy {
102105
0
103106
}
104107

105-
fn __getitem__(&self, key: u32) -> ::pyo3::PyResult<u32> {
106-
::std::result::Result::Err(::pyo3::exceptions::PyKeyError::new_err("boo"))
108+
fn __getitem__(&self, key: u32) -> crate::PyResult<u32> {
109+
::std::result::Result::Err(crate::exceptions::PyKeyError::new_err("boo"))
107110
}
108111

109112
fn __setitem__(&self, key: u32, value: u32) {}
110113

111114
fn __delitem__(&self, key: u32) {}
112115

113-
fn __iter__(_: ::pyo3::pycell::PyRef<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
114-
::pyo3::Py::new(py, DummyIter {}).unwrap()
116+
fn __iter__(_: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
117+
crate::Py::new(py, DummyIter {}).unwrap()
115118
}
116119

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

121-
fn __reversed__(slf: ::pyo3::pycell::PyRef<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
122-
::pyo3::Py::new(py, DummyIter {}).unwrap()
124+
fn __reversed__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
125+
crate::Py::new(py, DummyIter {}).unwrap()
123126
}
124127

125128
fn __contains__(&self, item: u32) -> bool {
@@ -142,12 +145,12 @@ impl Dummy {
142145
Dummy {}
143146
}
144147

145-
fn __truediv__(&self, _other: &Self) -> ::pyo3::PyResult<()> {
146-
::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo"))
148+
fn __truediv__(&self, _other: &Self) -> crate::PyResult<()> {
149+
::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo"))
147150
}
148151

149-
fn __floordiv__(&self, _other: &Self) -> ::pyo3::PyResult<()> {
150-
::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo"))
152+
fn __floordiv__(&self, _other: &Self) -> crate::PyResult<()> {
153+
::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo"))
151154
}
152155

153156
fn __mod__(&self, _other: &Self) -> u32 {
@@ -194,12 +197,12 @@ impl Dummy {
194197
Dummy {}
195198
}
196199

197-
fn __rtruediv__(&self, _other: &Self) -> ::pyo3::PyResult<()> {
198-
::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo"))
200+
fn __rtruediv__(&self, _other: &Self) -> crate::PyResult<()> {
201+
::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo"))
199202
}
200203

201-
fn __rfloordiv__(&self, _other: &Self) -> ::pyo3::PyResult<()> {
202-
::std::result::Result::Err(::pyo3::exceptions::PyZeroDivisionError::new_err("boo"))
204+
fn __rfloordiv__(&self, _other: &Self) -> crate::PyResult<()> {
205+
::std::result::Result::Err(crate::exceptions::PyZeroDivisionError::new_err("boo"))
203206
}
204207

205208
fn __rmod__(&self, _other: &Self) -> u32 {
@@ -258,24 +261,24 @@ impl Dummy {
258261

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

261-
fn __neg__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
264+
fn __neg__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
262265
slf
263266
}
264267

265-
fn __pos__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
268+
fn __pos__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
266269
slf
267270
}
268271

269-
fn __abs__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
272+
fn __abs__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
270273
slf
271274
}
272275

273-
fn __invert__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
276+
fn __invert__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
274277
slf
275278
}
276279

277-
fn __complex__<'py>(&self, py: ::pyo3::Python<'py>) -> &'py ::pyo3::types::PyComplex {
278-
::pyo3::types::PyComplex::from_doubles(py, 0.0, 0.0)
280+
fn __complex__<'py>(&self, py: crate::Python<'py>) -> &'py crate::types::PyComplex {
281+
crate::types::PyComplex::from_doubles(py, 0.0, 0.0)
279282
}
280283

281284
fn __int__(&self) -> u32 {
@@ -314,17 +317,17 @@ impl Dummy {
314317

315318
fn __exit__(
316319
&mut self,
317-
exc_type: &::pyo3::PyAny,
318-
exc_value: &::pyo3::PyAny,
319-
traceback: &::pyo3::PyAny,
320+
exc_type: &crate::PyAny,
321+
exc_value: &crate::PyAny,
322+
traceback: &crate::PyAny,
320323
) {
321324
}
322325

323326
//////////////////////
324327
// Awaitable Objects
325328
//////////////////////
326329

327-
fn __await__(slf: ::pyo3::pycell::PyRef<Self>) -> ::pyo3::pycell::PyRef<Self> {
330+
fn __await__(slf: crate::pycell::PyRef<Self>) -> crate::pycell::PyRef<Self> {
328331
slf
329332
}
330333

@@ -333,8 +336,8 @@ impl Dummy {
333336
// Asynchronous Iterators
334337
//////////////////////
335338

336-
fn __aiter__(slf: ::pyo3::pycell::PyRef<Self>, py: ::pyo3::Python) -> ::pyo3::Py<DummyIter> {
337-
::pyo3::Py::new(py, DummyIter {}).unwrap()
339+
fn __aiter__(slf: crate::pycell::PyRef<Self>, py: crate::Python) -> crate::Py<DummyIter> {
340+
crate::Py::new(py, DummyIter {}).unwrap()
338341
}
339342

340343
fn __anext__(&mut self) -> ::std::option::Option<()> {
@@ -349,9 +352,9 @@ impl Dummy {
349352

350353
fn __aexit__(
351354
&mut self,
352-
exc_type: &::pyo3::PyAny,
353-
exc_value: &::pyo3::PyAny,
354-
traceback: &::pyo3::PyAny,
355+
exc_type: &crate::PyAny,
356+
exc_value: &crate::PyAny,
357+
traceback: &crate::PyAny,
355358
) {
356359
}
357360

@@ -362,13 +365,13 @@ impl Dummy {
362365
#[staticmethod]
363366
fn staticmethod() {}
364367
#[classmethod]
365-
fn clsmethod(_: &::pyo3::types::PyType) {}
368+
fn clsmethod(_: &crate::types::PyType) {}
366369
#[args(args = "*", kwds = "**")]
367370
fn __call__(
368371
&self,
369-
_args: &::pyo3::types::PyTuple,
370-
_kwds: ::std::option::Option<&::pyo3::types::PyDict>,
371-
) -> ::pyo3::PyResult<i32> {
372+
_args: &crate::types::PyTuple,
373+
_kwds: ::std::option::Option<&crate::types::PyDict>,
374+
) -> crate::PyResult<i32> {
372375
::std::panic!("unimplemented isn't hygienic before 1.50")
373376
}
374377
#[new]
@@ -391,8 +394,8 @@ impl Dummy {
391394
fn __richcmp__(
392395
&self,
393396
other: &Self,
394-
op: ::pyo3::class::basic::CompareOp,
395-
) -> ::pyo3::PyResult<bool> {
397+
op: crate::class::basic::CompareOp,
398+
) -> crate::PyResult<bool> {
396399
::std::result::Result::Ok(false)
397400
}
398401
// PyGcProtocol

src/test_hygiene/pymodule.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![no_implicit_prelude]
2+
#![allow(unused_variables)]
3+
4+
#[crate::pyfunction]
5+
#[pyo3(pyo3_path = "crate")]
6+
fn do_something(x: i32) -> crate::PyResult<i32> {
7+
::std::result::Result::Ok(x)
8+
}
9+
10+
#[crate::pymodule]
11+
#[pyo3(pyo3_path = "crate")]
12+
fn foo(_py: crate::Python, _m: &crate::types::PyModule) -> crate::PyResult<()> {
13+
::std::result::Result::Ok(())
14+
}
15+
16+
#[crate::pymodule]
17+
#[pyo3(pyo3_path = "crate")]
18+
fn my_module(_py: crate::Python, m: &crate::types::PyModule) -> crate::PyResult<()> {
19+
m.add_function(crate::wrap_pyfunction!(do_something, m)?)?;
20+
m.add_wrapped(crate::wrap_pymodule!(foo))?;
21+
22+
::std::result::Result::Ok(())
23+
}

0 commit comments

Comments
 (0)