Skip to content

Commit c8e5e85

Browse files
committed
migrate PyModule trait bounds
1 parent 9594994 commit c8e5e85

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/marker.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
//! [`SendWrapper`]: https://docs.rs/send_wrapper/latest/send_wrapper/struct.SendWrapper.html
117117
//! [`Rc`]: std::rc::Rc
118118
//! [`Py`]: crate::Py
119+
use crate::conversion::IntoPyObject;
119120
#[cfg(any(doc, not(Py_3_10)))]
120121
use crate::err::PyErr;
121122
use crate::err::{self, PyResult};
@@ -707,7 +708,7 @@ impl<'py> Python<'py> {
707708
/// Imports the Python module with the specified name.
708709
pub fn import<N>(self, name: N) -> PyResult<Bound<'py, PyModule>>
709710
where
710-
N: IntoPy<Py<PyString>>,
711+
N: IntoPyObject<'py, Target = PyString>,
711712
{
712713
PyModule::import(self, name)
713714
}
@@ -720,7 +721,7 @@ impl<'py> Python<'py> {
720721
where
721722
N: IntoPy<Py<PyString>>,
722723
{
723-
self.import(name)
724+
self.import(name.into_py(self))
724725
}
725726

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

src/types/module.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::callback::IntoPyCallbackOutput;
2+
use crate::conversion::IntoPyObject;
23
use crate::err::{PyErr, PyResult};
34
use crate::ffi_ptr_ext::FfiPtrExt;
45
use crate::py_result_ext::PyResultExt;
56
use crate::pyclass::PyClass;
67
use crate::types::{
78
any::PyAnyMethods, list::PyListMethods, PyAny, PyCFunction, PyDict, PyList, PyString,
89
};
9-
use crate::{exceptions, ffi, Bound, IntoPy, Py, PyObject, Python};
10+
use crate::{exceptions, ffi, Borrowed, Bound, BoundObject, Py, PyObject, Python};
1011
use std::ffi::{CStr, CString};
1112
use std::str;
1213

@@ -82,11 +83,11 @@ impl PyModule {
8283
///
8384
/// If you want to import a class, you can store a reference to it with
8485
/// [`GILOnceCell::import`][crate::sync::GILOnceCell#method.import].
85-
pub fn import<N>(py: Python<'_>, name: N) -> PyResult<Bound<'_, PyModule>>
86+
pub fn import<'py, N>(py: Python<'py>, name: N) -> PyResult<Bound<'py, PyModule>>
8687
where
87-
N: IntoPy<Py<PyString>>,
88+
N: IntoPyObject<'py, Target = PyString>,
8889
{
89-
let name: Py<PyString> = name.into_py(py);
90+
let name = name.into_pyobject(py).map_err(Into::into)?;
9091
unsafe {
9192
ffi::PyImport_Import(name.as_ptr())
9293
.assume_owned_or_err(py)
@@ -99,9 +100,9 @@ impl PyModule {
99100
#[inline]
100101
pub fn import_bound<N>(py: Python<'_>, name: N) -> PyResult<Bound<'_, PyModule>>
101102
where
102-
N: IntoPy<Py<PyString>>,
103+
N: crate::IntoPy<Py<PyString>>,
103104
{
104-
Self::import(py, name)
105+
Self::import(py, name.into_py(py))
105106
}
106107

107108
/// Creates and loads a module named `module_name`,
@@ -253,8 +254,8 @@ pub trait PyModuleMethods<'py>: crate::sealed::Sealed {
253254
/// ```
254255
fn add<N, V>(&self, name: N, value: V) -> PyResult<()>
255256
where
256-
N: IntoPy<Py<PyString>>,
257-
V: IntoPy<PyObject>;
257+
N: IntoPyObject<'py, Target = PyString>,
258+
V: IntoPyObject<'py>;
258259

259260
/// Adds a new class to the module.
260261
///
@@ -452,26 +453,30 @@ impl<'py> PyModuleMethods<'py> for Bound<'py, PyModule> {
452453

453454
fn add<N, V>(&self, name: N, value: V) -> PyResult<()>
454455
where
455-
N: IntoPy<Py<PyString>>,
456-
V: IntoPy<PyObject>,
456+
N: IntoPyObject<'py, Target = PyString>,
457+
V: IntoPyObject<'py>,
457458
{
458459
fn inner(
459460
module: &Bound<'_, PyModule>,
460-
name: Bound<'_, PyString>,
461-
value: Bound<'_, PyAny>,
461+
name: Borrowed<'_, '_, PyString>,
462+
value: Borrowed<'_, '_, PyAny>,
462463
) -> PyResult<()> {
463464
module
464465
.index()?
465466
.append(&name)
466467
.expect("could not append __name__ to __all__");
467-
module.setattr(name, value.into_py(module.py()))
468+
module.setattr(name, value)
468469
}
469470

470471
let py = self.py();
471472
inner(
472473
self,
473-
name.into_py(py).into_bound(py),
474-
value.into_py(py).into_bound(py),
474+
name.into_pyobject(py).map_err(Into::into)?.as_borrowed(),
475+
value
476+
.into_pyobject(py)
477+
.map_err(Into::into)?
478+
.into_any()
479+
.as_borrowed(),
475480
)
476481
}
477482

0 commit comments

Comments
 (0)