Skip to content

Commit 3eb45fb

Browse files
committed
add PyModule::new and PyModule::import_bound
1 parent 0c12d91 commit 3eb45fb

23 files changed

+144
-102
lines changed

guide/src/class.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ impl MyClass {
941941
# fn main() -> PyResult<()> {
942942
# Python::with_gil(|py| {
943943
# let inspect = PyModule::import(py, "inspect")?.getattr("signature")?;
944-
# let module = PyModule::new(py, "my_module")?;
944+
# let module = PyModule::new_bound(py, "my_module")?;
945945
# module.add_class::<MyClass>()?;
946946
# let class = module.getattr("MyClass")?;
947947
#

guide/src/module.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ fn parent_module(py: Python<'_>, m: &PyModule) -> PyResult<()> {
7878
}
7979

8080
fn register_child_module(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
81-
let child_module = PyModule::new(py, "child_module")?;
82-
child_module.add_function(wrap_pyfunction!(func, child_module)?)?;
83-
parent_module.add_submodule(child_module)?;
81+
let child_module = PyModule::new_bound(py, "child_module")?;
82+
child_module.add_function(wrap_pyfunction!(func, child_module.as_gil_ref())?)?;
83+
parent_module.add_submodule(child_module.as_gil_ref())?;
8484
Ok(())
8585
}
8686

guide/src/python_from_rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub fn add_one(x: i64) -> i64 {
310310
fn main() -> PyResult<()> {
311311
Python::with_gil(|py| {
312312
// Create new module
313-
let foo_module = PyModule::new(py, "foo")?;
313+
let foo_module = PyModule::new_bound(py, "foo")?;
314314
foo_module.add_function(wrap_pyfunction!(add_one, foo_module)?)?;
315315

316316
// Import and get sys.modules

pyo3-benches/benches/bench_call.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
use std::hint::black_box;
2+
13
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
24

35
use pyo3::prelude::*;
46

57
macro_rules! test_module {
68
($py:ident, $code:literal) => {
7-
PyModule::from_code($py, $code, file!(), "test_module").expect("module creation failed")
9+
PyModule::from_code_bound($py, $code, file!(), "test_module")
10+
.expect("module creation failed")
811
};
912
}
1013

1114
fn bench_call_0(b: &mut Bencher<'_>) {
1215
Python::with_gil(|py| {
1316
let module = test_module!(py, "def foo(): pass");
1417

15-
let foo_module = module.getattr("foo").unwrap();
18+
let foo_module = &module.getattr("foo").unwrap();
1619

1720
b.iter(|| {
1821
for _ in 0..1000 {
19-
foo_module.call0().unwrap();
22+
black_box(foo_module).call0().unwrap();
2023
}
2124
});
2225
})
@@ -33,11 +36,11 @@ class Foo:
3336
"
3437
);
3538

36-
let foo_module = module.getattr("Foo").unwrap().call0().unwrap();
39+
let foo_module = &module.getattr("Foo").unwrap().call0().unwrap();
3740

3841
b.iter(|| {
3942
for _ in 0..1000 {
40-
foo_module.call_method0("foo").unwrap();
43+
black_box(foo_module).call_method0("foo").unwrap();
4144
}
4245
});
4346
})

pyo3-benches/benches/bench_intern.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::hint::black_box;
2+
13
use codspeed_criterion_compat::{criterion_group, criterion_main, Bencher, Criterion};
24

35
use pyo3::prelude::*;
@@ -6,17 +8,17 @@ use pyo3::intern;
68

79
fn getattr_direct(b: &mut Bencher<'_>) {
810
Python::with_gil(|py| {
9-
let sys = py.import("sys").unwrap();
11+
let sys = &py.import_bound("sys").unwrap();
1012

11-
b.iter(|| sys.getattr("version").unwrap());
13+
b.iter(|| black_box(sys).getattr("version").unwrap());
1214
});
1315
}
1416

1517
fn getattr_intern(b: &mut Bencher<'_>) {
1618
Python::with_gil(|py| {
17-
let sys = py.import("sys").unwrap();
19+
let sys = &py.import_bound("sys").unwrap();
1820

19-
b.iter(|| sys.getattr(intern!(py, "version")).unwrap());
21+
b.iter(|| black_box(sys).getattr(intern!(py, "version")).unwrap());
2022
});
2123
}
2224

src/conversions/chrono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ fn timezone_utc_bound(py: Python<'_>) -> Bound<'_, PyAny> {
564564
#[cfg(test)]
565565
mod tests {
566566
use super::*;
567-
use crate::{types::PyTuple, Py};
567+
use crate::{types::PyTuple, Bound, Py};
568568
use std::{cmp::Ordering, panic};
569569

570570
#[test]

src/conversions/chrono_tz.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ impl FromPyObject<'_> for Tz {
6969

7070
#[cfg(all(test, not(windows)))] // Troubles loading timezones on Windows
7171
mod tests {
72+
use crate::{types::any::PyAnyMethods, Bound};
73+
7274
use super::*;
7375

7476
#[test]

src/conversions/num_bigint.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ mod tests {
268268
use self::{any::PyAnyMethods, dict::PyDictMethods};
269269

270270
use super::*;
271-
use crate::types::{PyDict, PyModule};
271+
use crate::{
272+
types::{PyDict, PyModule},
273+
Bound,
274+
};
272275
use indoc::indoc;
273276

274277
fn rust_fib<T>() -> impl Iterator<Item = T>
@@ -329,7 +332,7 @@ mod tests {
329332
});
330333
}
331334

332-
fn python_index_class(py: Python<'_>) -> &PyModule {
335+
fn python_index_class(py: Python<'_>) -> Bound<'_, PyModule> {
333336
let index_code = indoc!(
334337
r#"
335338
class C:
@@ -339,7 +342,7 @@ mod tests {
339342
return self.x
340343
"#
341344
);
342-
PyModule::from_code(py, index_code, "index.py", "index").unwrap()
345+
PyModule::from_code_bound(py, index_code, "index.py", "index").unwrap()
343346
}
344347

345348
#[test]

src/conversions/num_complex.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
//! #
5656
//! # fn main() -> PyResult<()> {
5757
//! # Python::with_gil(|py| -> PyResult<()> {
58-
//! # let module = PyModule::new(py, "my_module")?;
58+
//! # let module = PyModule::new_bound(py, "my_module")?;
5959
//! #
6060
//! # module.add_function(wrap_pyfunction!(get_eigenvalues, module)?)?;
6161
//! #
@@ -183,7 +183,7 @@ complex_conversion!(f64);
183183
#[cfg(test)]
184184
mod tests {
185185
use super::*;
186-
use crate::types::PyModule;
186+
use crate::types::{any::PyAnyMethods, PyModule};
187187

188188
#[test]
189189
fn from_complex() {
@@ -212,7 +212,7 @@ mod tests {
212212
#[test]
213213
fn from_python_magic() {
214214
Python::with_gil(|py| {
215-
let module = PyModule::from_code(
215+
let module = PyModule::from_code_bound(
216216
py,
217217
r#"
218218
class A:
@@ -250,7 +250,7 @@ class C:
250250
#[test]
251251
fn from_python_inherited_magic() {
252252
Python::with_gil(|py| {
253-
let module = PyModule::from_code(
253+
let module = PyModule::from_code_bound(
254254
py,
255255
r#"
256256
class First: pass
@@ -294,7 +294,7 @@ class C(First, IndexMixin): pass
294294
// `type(inst).attr(inst)` equivalent to `inst.attr()` for methods, but this isn't the only
295295
// way the descriptor protocol might be implemented.
296296
Python::with_gil(|py| {
297-
let module = PyModule::from_code(
297+
let module = PyModule::from_code_bound(
298298
py,
299299
r#"
300300
class A:
@@ -317,7 +317,7 @@ class A:
317317
fn from_python_nondescriptor_magic() {
318318
// Magic methods don't need to implement the descriptor protocol, if they're callable.
319319
Python::with_gil(|py| {
320-
let module = PyModule::from_code(
320+
let module = PyModule::from_code_bound(
321321
py,
322322
r#"
323323
class MyComplex:

src/instance.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ impl<T> IntoPy<PyObject> for Borrowed<'_, '_, T> {
642642
/// #
643643
/// # fn main() -> PyResult<()> {
644644
/// # Python::with_gil(|py| {
645-
/// # let m = pyo3::types::PyModule::new(py, "test")?;
645+
/// # let m = pyo3::types::PyModule::new_bound(py, "test")?;
646646
/// # m.add_class::<Foo>()?;
647647
/// #
648648
/// # let foo: &PyCell<Foo> = m.getattr("Foo")?.call0()?.downcast()?;
@@ -679,7 +679,7 @@ impl<T> IntoPy<PyObject> for Borrowed<'_, '_, T> {
679679
/// #
680680
/// # fn main() -> PyResult<()> {
681681
/// # Python::with_gil(|py| {
682-
/// # let m = pyo3::types::PyModule::new(py, "test")?;
682+
/// # let m = pyo3::types::PyModule::new_bound(py, "test")?;
683683
/// # m.add_class::<Foo>()?;
684684
/// #
685685
/// # let foo: &PyCell<Foo> = m.getattr("Foo")?.call0()?.downcast()?;
@@ -1233,7 +1233,7 @@ impl<T> Py<T> {
12331233
/// }
12341234
/// #
12351235
/// # Python::with_gil(|py| {
1236-
/// # let ob = PyModule::new(py, "empty").unwrap().into_py(py);
1236+
/// # let ob = PyModule::new_bound(py, "empty").unwrap().into_py(py);
12371237
/// # set_answer(ob, py).unwrap();
12381238
/// # });
12391239
/// ```

src/marker.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ impl<'py> Python<'py> {
748748
where
749749
N: IntoPy<Py<PyString>>,
750750
{
751+
#[allow(deprecated)]
751752
PyModule::import(self, name)
752753
}
753754

@@ -756,11 +757,7 @@ impl<'py> Python<'py> {
756757
where
757758
N: IntoPy<Py<PyString>>,
758759
{
759-
// FIXME: This should be replaced by `PyModule::import_bound` once thats
760-
// implemented.
761-
PyModule::import(self, name)
762-
.map(PyNativeType::as_borrowed)
763-
.map(crate::Borrowed::to_owned)
760+
PyModule::import_bound(self, name)
764761
}
765762

766763
/// Gets the Python builtin value `None`.

src/pyclass_init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<T: PyClass> PyClassInitializer<T> {
184184
///
185185
/// fn main() -> PyResult<()> {
186186
/// Python::with_gil(|py| {
187-
/// let m = PyModule::new(py, "example")?;
187+
/// let m = PyModule::new_bound(py, "example")?;
188188
/// m.add_class::<SubClass>()?;
189189
/// m.add_class::<BaseClass>()?;
190190
///

src/types/any.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ impl PyAny {
149149
/// # use pyo3::{intern, pyfunction, types::PyModule, PyAny, Python, PyResult};
150150
/// #
151151
/// #[pyfunction]
152-
/// fn set_answer(ob: &PyAny) -> PyResult<()> {
152+
/// fn set_answer(ob: &Bound<'_, PyAny>) -> PyResult<()> {
153153
/// ob.setattr(intern!(ob.py(), "answer"), 42)
154154
/// }
155155
/// #
156156
/// # Python::with_gil(|py| {
157-
/// # let ob = PyModule::new(py, "empty").unwrap();
158-
/// # set_answer(ob).unwrap();
157+
/// # let ob = PyModule::new_bound(py, "empty").unwrap();
158+
/// # set_answer(&ob).unwrap();
159159
/// # });
160160
/// ```
161161
pub fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>
@@ -1011,13 +1011,13 @@ pub trait PyAnyMethods<'py> {
10111011
/// # use pyo3::{intern, pyfunction, types::PyModule, PyAny, Python, PyResult};
10121012
/// #
10131013
/// #[pyfunction]
1014-
/// fn set_answer(ob: &PyAny) -> PyResult<()> {
1014+
/// fn set_answer(ob: &Bound<'_, PyAny>) -> PyResult<()> {
10151015
/// ob.setattr(intern!(ob.py(), "answer"), 42)
10161016
/// }
10171017
/// #
10181018
/// # Python::with_gil(|py| {
1019-
/// # let ob = PyModule::new(py, "empty").unwrap();
1020-
/// # set_answer(ob).unwrap();
1019+
/// # let ob = PyModule::new_bound(py, "empty").unwrap();
1020+
/// # set_answer(&ob).unwrap();
10211021
/// # });
10221022
/// ```
10231023
fn setattr<N, V>(&self, attr_name: N, value: V) -> PyResult<()>

src/types/capsule.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ fn name_ptr_ignore_error(slf: &Bound<'_, PyCapsule>) -> *const c_char {
441441
}
442442

443443
#[cfg(test)]
444+
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
444445
mod tests {
445446
use libc::c_void;
446447

0 commit comments

Comments
 (0)