Skip to content

Commit 104328c

Browse files
authored
feature gate deprecated more APIs for Py (#4169)
1 parent f3c7b90 commit 104328c

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

guide/src/python-from-rust/calling-existing-code.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ fn main() -> PyResult<()> {
2424
}
2525
```
2626

27-
## Want to run just an expression? Then use `eval`.
27+
## Want to run just an expression? Then use `eval_bound`.
2828

29-
[`Python::eval`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.eval) is
29+
[`Python::eval_bound`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.eval_bound) is
3030
a method to execute a [Python expression](https://docs.python.org/3.7/reference/expressions.html)
3131
and return the evaluated value as a `Bound<'py, PyAny>` object.
3232

@@ -47,14 +47,14 @@ Python::with_gil(|py| {
4747
# }
4848
```
4949

50-
## Want to run statements? Then use `run`.
50+
## Want to run statements? Then use `run_bound`.
5151

52-
[`Python::run`] is a method to execute one or more
52+
[`Python::run_bound`] is a method to execute one or more
5353
[Python statements](https://docs.python.org/3.7/reference/simple_stmts.html).
5454
This method returns nothing (like any Python statement), but you can get
5555
access to manipulated objects via the `locals` dict.
5656

57-
You can also use the [`py_run!`] macro, which is a shorthand for [`Python::run`].
57+
You can also use the [`py_run!`] macro, which is a shorthand for [`Python::run_bound`].
5858
Since [`py_run!`] panics on exceptions, we recommend you use this macro only for
5959
quickly testing your Python extensions.
6060

src/err/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl PyErr {
724724
///
725725
/// The `category` should be one of the `Warning` classes available in
726726
/// [`pyo3::exceptions`](crate::exceptions), or a subclass. The Python
727-
/// object can be retrieved using [`Python::get_type()`].
727+
/// object can be retrieved using [`Python::get_type_bound()`].
728728
///
729729
/// Example:
730730
/// ```rust

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
///
33
/// # Panics
44
///
5-
/// This macro internally calls [`Python::run`](crate::Python::run) and panics
5+
/// This macro internally calls [`Python::run_bound`](crate::Python::run_bound) and panics
66
/// if it returns `Err`, after printing the error to stdout.
77
///
8-
/// If you need to handle failures, please use [`Python::run`](crate::marker::Python::run) instead.
8+
/// If you need to handle failures, please use [`Python::run_bound`](crate::marker::Python::run_bound) instead.
99
///
1010
/// # Examples
1111
/// ```

src/marker.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ use crate::types::{
126126
PyAny, PyDict, PyEllipsis, PyModule, PyNone, PyNotImplemented, PyString, PyType,
127127
};
128128
use crate::version::PythonVersionInfo;
129-
use crate::{ffi, Bound, IntoPy, Py, PyNativeType, PyObject, PyTypeInfo};
129+
#[cfg(feature = "gil-refs")]
130+
use crate::PyNativeType;
131+
use crate::{ffi, Bound, IntoPy, Py, PyObject, PyTypeInfo};
130132
#[allow(deprecated)]
131133
use crate::{gil::GILPool, FromPyPointer};
132134
use std::ffi::{CStr, CString};
@@ -305,7 +307,7 @@ pub use nightly::Ungil;
305307
/// A marker token that represents holding the GIL.
306308
///
307309
/// It serves three main purposes:
308-
/// - It provides a global API for the Python interpreter, such as [`Python::eval`].
310+
/// - It provides a global API for the Python interpreter, such as [`Python::eval_bound`].
309311
/// - It can be passed to functions that require a proof of holding the GIL, such as
310312
/// [`Py::clone_ref`].
311313
/// - Its lifetime represents the scope of holding the GIL which can be used to create Rust
@@ -321,7 +323,7 @@ pub use nightly::Ungil;
321323
/// - In a function or method annotated with [`#[pyfunction]`](crate::pyfunction) or [`#[pymethods]`](crate::pymethods) you can declare it
322324
/// as a parameter, and PyO3 will pass in the token when Python code calls it.
323325
/// - If you already have something with a lifetime bound to the GIL, such as `&`[`PyAny`], you can
324-
/// use its [`.py()`][PyAny::py] method to get a token.
326+
/// use its `.py()` method to get a token.
325327
/// - When you need to acquire the GIL yourself, such as when calling Python code from Rust, you
326328
/// should call [`Python::with_gil`] to do that and pass your code as a closure to it.
327329
///
@@ -352,7 +354,7 @@ pub use nightly::Ungil;
352354
/// # Releasing and freeing memory
353355
///
354356
/// The [`Python`] type can be used to create references to variables owned by the Python
355-
/// interpreter, using functions such as [`Python::eval`] and `PyModule::import`. These
357+
/// interpreter, using functions such as `Python::eval` and `PyModule::import`. These
356358
/// references are tied to a [`GILPool`] whose references are not cleared until it is dropped.
357359
/// This can cause apparent "memory leaks" if it is kept around for a long time.
358360
///
@@ -552,12 +554,10 @@ impl<'py> Python<'py> {
552554
}
553555

554556
/// Deprecated version of [`Python::eval_bound`]
555-
#[cfg_attr(
556-
not(feature = "gil-refs"),
557-
deprecated(
558-
since = "0.21.0",
559-
note = "`Python::eval` will be replaced by `Python::eval_bound` in a future PyO3 version"
560-
)
557+
#[cfg(feature = "gil-refs")]
558+
#[deprecated(
559+
since = "0.21.0",
560+
note = "`Python::eval` will be replaced by `Python::eval_bound` in a future PyO3 version"
561561
)]
562562
pub fn eval(
563563
self,
@@ -601,12 +601,10 @@ impl<'py> Python<'py> {
601601
}
602602

603603
/// Deprecated version of [`Python::run_bound`]
604-
#[cfg_attr(
605-
not(feature = "gil-refs"),
606-
deprecated(
607-
since = "0.21.0",
608-
note = "`Python::run` will be replaced by `Python::run_bound` in a future PyO3 version"
609-
)
604+
#[cfg(feature = "gil-refs")]
605+
#[deprecated(
606+
since = "0.21.0",
607+
note = "`Python::run` will be replaced by `Python::run_bound` in a future PyO3 version"
610608
)]
611609
pub fn run(
612610
self,
@@ -728,12 +726,10 @@ impl<'py> Python<'py> {
728726
}
729727

730728
/// Gets the Python type object for type `T`.
731-
#[cfg_attr(
732-
not(feature = "gil-refs"),
733-
deprecated(
734-
since = "0.21.0",
735-
note = "`Python::get_type` will be replaced by `Python::get_type_bound` in a future PyO3 version"
736-
)
729+
#[cfg(feature = "gil-refs")]
730+
#[deprecated(
731+
since = "0.21.0",
732+
note = "`Python::get_type` will be replaced by `Python::get_type_bound` in a future PyO3 version"
737733
)]
738734
#[inline]
739735
pub fn get_type<T>(self) -> &'py PyType
@@ -753,12 +749,10 @@ impl<'py> Python<'py> {
753749
}
754750

755751
/// Deprecated form of [`Python::import_bound`]
756-
#[cfg_attr(
757-
not(feature = "gil-refs"),
758-
deprecated(
759-
since = "0.21.0",
760-
note = "`Python::import` will be replaced by `Python::import_bound` in a future PyO3 version"
761-
)
752+
#[cfg(feature = "gil-refs")]
753+
#[deprecated(
754+
since = "0.21.0",
755+
note = "`Python::import` will be replaced by `Python::import_bound` in a future PyO3 version"
762756
)]
763757
pub fn import<N>(self, name: N) -> PyResult<&'py PyModule>
764758
where

0 commit comments

Comments
 (0)