Skip to content

Commit

Permalink
migrate PyModule trait bounds (#4594)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu authored Oct 4, 2024
1 parent e9a1b9b commit d4ad8a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
//! [`SendWrapper`]: https://docs.rs/send_wrapper/latest/send_wrapper/struct.SendWrapper.html
//! [`Rc`]: std::rc::Rc
//! [`Py`]: crate::Py
use crate::conversion::IntoPyObject;
#[cfg(any(doc, not(Py_3_10)))]
use crate::err::PyErr;
use crate::err::{self, PyResult};
Expand Down Expand Up @@ -707,7 +708,7 @@ impl<'py> Python<'py> {
/// Imports the Python module with the specified name.
pub fn import<N>(self, name: N) -> PyResult<Bound<'py, PyModule>>
where
N: IntoPy<Py<PyString>>,
N: IntoPyObject<'py, Target = PyString>,
{
PyModule::import(self, name)
}
Expand All @@ -720,7 +721,7 @@ impl<'py> Python<'py> {
where
N: IntoPy<Py<PyString>>,
{
self.import(name)
self.import(name.into_py(self))
}

/// Gets the Python builtin value `None`.
Expand Down
37 changes: 21 additions & 16 deletions src/types/module.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::callback::IntoPyCallbackOutput;
use crate::conversion::IntoPyObject;
use crate::err::{PyErr, PyResult};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::py_result_ext::PyResultExt;
use crate::pyclass::PyClass;
use crate::types::{
any::PyAnyMethods, list::PyListMethods, PyAny, PyCFunction, PyDict, PyList, PyString,
};
use crate::{exceptions, ffi, Bound, IntoPy, Py, PyObject, Python};
use crate::{exceptions, ffi, Borrowed, Bound, BoundObject, Py, PyObject, Python};
use std::ffi::{CStr, CString};
use std::str;

Expand Down Expand Up @@ -82,11 +83,11 @@ impl PyModule {
///
/// If you want to import a class, you can store a reference to it with
/// [`GILOnceCell::import`][crate::sync::GILOnceCell#method.import].
pub fn import<N>(py: Python<'_>, name: N) -> PyResult<Bound<'_, PyModule>>
pub fn import<'py, N>(py: Python<'py>, name: N) -> PyResult<Bound<'py, PyModule>>
where
N: IntoPy<Py<PyString>>,
N: IntoPyObject<'py, Target = PyString>,
{
let name: Py<PyString> = name.into_py(py);
let name = name.into_pyobject(py).map_err(Into::into)?;
unsafe {
ffi::PyImport_Import(name.as_ptr())
.assume_owned_or_err(py)
Expand All @@ -99,9 +100,9 @@ impl PyModule {
#[inline]
pub fn import_bound<N>(py: Python<'_>, name: N) -> PyResult<Bound<'_, PyModule>>
where
N: IntoPy<Py<PyString>>,
N: crate::IntoPy<Py<PyString>>,
{
Self::import(py, name)
Self::import(py, name.into_py(py))
}

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

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

fn add<N, V>(&self, name: N, value: V) -> PyResult<()>
where
N: IntoPy<Py<PyString>>,
V: IntoPy<PyObject>,
N: IntoPyObject<'py, Target = PyString>,
V: IntoPyObject<'py>,
{
fn inner(
module: &Bound<'_, PyModule>,
name: Bound<'_, PyString>,
value: Bound<'_, PyAny>,
name: Borrowed<'_, '_, PyString>,
value: Borrowed<'_, '_, PyAny>,
) -> PyResult<()> {
module
.index()?
.append(&name)
.append(name)
.expect("could not append __name__ to __all__");
module.setattr(name, value.into_py(module.py()))
module.setattr(name, value)
}

let py = self.py();
inner(
self,
name.into_py(py).into_bound(py),
value.into_py(py).into_bound(py),
name.into_pyobject(py).map_err(Into::into)?.as_borrowed(),
value
.into_pyobject(py)
.map_err(Into::into)?
.into_any()
.as_borrowed(),
)
}

Expand Down

0 comments on commit d4ad8a2

Please sign in to comment.