Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature gate deprecated APIs for PyModule #4151

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions guide/src/python-from-rust/calling-existing-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

If you already have some existing Python code that you need to execute from Rust, the following FAQs can help you select the right PyO3 functionality for your situation:

## Want to access Python APIs? Then use `PyModule::import`.
## Want to access Python APIs? Then use `PyModule::import_bound`.

[`Pymodule::import`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.import) can
[`PyModule::import_bound`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.import_bound) can
be used to get handle to a Python module from Rust. You can use this to import and use any Python
module available in your environment.

Expand Down Expand Up @@ -95,9 +95,9 @@ assert userdata.as_tuple() == userdata_as_tuple
# }
```

## You have a Python file or code snippet? Then use `PyModule::from_code`.
## You have a Python file or code snippet? Then use `PyModule::from_code_bound`.

[`PyModule::from_code`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.from_code)
[`PyModule::from_code_bound`]({{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.from_code_bound)
can be used to generate a Python module which can then be used just as if it was imported with
`PyModule::import`.

Expand Down Expand Up @@ -171,7 +171,7 @@ fn main() -> PyResult<()> {
```

If `append_to_inittab` cannot be used due to constraints in the program,
an alternative is to create a module using [`PyModule::new`]
an alternative is to create a module using [`PyModule::new_bound`]
and insert it manually into `sys.modules`:

```rust
Expand Down Expand Up @@ -394,4 +394,4 @@ Python::with_gil(|py| -> PyResult<()> {
```


[`PyModule::new`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.new
[`PyModule::new_bound`]: {{#PYO3_DOCS_URL}}/pyo3/types/struct.PyModule.html#method.new_bound
2 changes: 1 addition & 1 deletion src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ pub use nightly::Ungil;
/// # Releasing and freeing memory
///
/// The [`Python`] type can be used to create references to variables owned by the Python
/// interpreter, using functions such as [`Python::eval`] and [`PyModule::import`]. These
/// interpreter, using functions such as [`Python::eval`] and `PyModule::import`. These
/// references are tied to a [`GILPool`] whose references are not cleared until it is dropped.
/// This can cause apparent "memory leaks" if it is kept around for a long time.
///
Expand Down
31 changes: 12 additions & 19 deletions src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ pyobject_native_type_core!(PyModule, pyobject_native_static_type_object!(ffi::Py
impl PyModule {
/// Deprecated form of [`PyModule::new_bound`].
#[inline]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyModule::new` will be replaced by `PyModule::new_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyModule::new` will be replaced by `PyModule::new_bound` in a future PyO3 version"
)]
pub fn new<'py>(py: Python<'py>, name: &str) -> PyResult<&'py PyModule> {
Self::new_bound(py, name).map(Bound::into_gil_ref)
Expand Down Expand Up @@ -66,12 +64,10 @@ impl PyModule {

/// Deprecated form of [`PyModule::import_bound`].
#[inline]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyModule::import` will be replaced by `PyModule::import_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyModule::import` will be replaced by `PyModule::import_bound` in a future PyO3 version"
)]
pub fn import<N>(py: Python<'_>, name: N) -> PyResult<&PyModule>
where
Expand Down Expand Up @@ -112,12 +108,10 @@ impl PyModule {

/// Deprecated form of [`PyModule::from_code_bound`].
#[inline]
#[cfg_attr(
not(feature = "gil-refs"),
deprecated(
since = "0.21.0",
note = "`PyModule::from_code` will be replaced by `PyModule::from_code_bound` in a future PyO3 version"
)
#[cfg(feature = "gil-refs")]
#[deprecated(
since = "0.21.0",
note = "`PyModule::from_code` will be replaced by `PyModule::from_code_bound` in a future PyO3 version"
)]
pub fn from_code<'py>(
py: Python<'py>,
Expand Down Expand Up @@ -722,7 +716,6 @@ fn __name__(py: Python<'_>) -> &Bound<'_, PyString> {
}

#[cfg(test)]
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
mod tests {
use crate::{
types::{module::PyModuleMethods, string::PyStringMethods, PyModule},
Expand Down
Loading