Skip to content

Commit 444be3b

Browse files
authored
feature gate deprecated APIs for Python (#4173)
1 parent 1e8e09d commit 444be3b

File tree

7 files changed

+89
-134
lines changed

7 files changed

+89
-134
lines changed

guide/src/memory.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ at the end of each loop iteration, before the `with_gil()` closure ends.
154154

155155
When doing this, you must be very careful to ensure that once the `GILPool` is
156156
dropped you do not retain access to any owned references created after the
157-
`GILPool` was created. Read the
158-
[documentation for `Python::new_pool()`]({{#PYO3_DOCS_URL}}/pyo3/marker/struct.Python.html#method.new_pool)
157+
`GILPool` was created. Read the documentation for `Python::new_pool()`
159158
for more information on safety.
160159

161160
This memory management can also be applicable when writing extension modules.

src/conversion.rs

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
//! Defines conversions between Rust and Python types.
2-
use crate::err::{self, PyDowncastError, PyResult};
2+
use crate::err::PyResult;
33
#[cfg(feature = "experimental-inspect")]
44
use crate::inspect::types::TypeInfo;
55
use crate::pyclass::boolean_struct::False;
66
use crate::types::any::PyAnyMethods;
77
use crate::types::PyTuple;
88
use crate::{
9-
ffi, gil, Borrowed, Bound, Py, PyAny, PyClass, PyNativeType, PyObject, PyRef, PyRefMut, Python,
9+
ffi, Borrowed, Bound, Py, PyAny, PyClass, PyNativeType, PyObject, PyRef, PyRefMut, Python,
10+
};
11+
#[cfg(feature = "gil-refs")]
12+
use {
13+
crate::{
14+
err::{self, PyDowncastError},
15+
gil,
16+
},
17+
std::ptr::NonNull,
1018
};
11-
use std::ptr::NonNull;
1219

1320
/// Returns a borrowed pointer to a Python object.
1421
///
@@ -385,6 +392,7 @@ where
385392
/// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
386393
///
387394
/// This trait is similar to `std::convert::TryFrom`
395+
#[cfg(feature = "gil-refs")]
388396
#[deprecated(since = "0.21.0")]
389397
pub trait PyTryFrom<'v>: Sized + PyNativeType {
390398
/// Cast from a concrete Python object type to PyObject.
@@ -416,6 +424,7 @@ pub trait PyTryFrom<'v>: Sized + PyNativeType {
416424

417425
/// Trait implemented by Python object types that allow a checked downcast.
418426
/// This trait is similar to `std::convert::TryInto`
427+
#[cfg(feature = "gil-refs")]
419428
#[deprecated(since = "0.21.0")]
420429
pub trait PyTryInto<T>: Sized {
421430
/// Cast from PyObject to a concrete Python object type.
@@ -506,6 +515,7 @@ impl IntoPy<Py<PyTuple>> for () {
506515
/// # Safety
507516
///
508517
/// See safety notes on individual functions.
518+
#[cfg(feature = "gil-refs")]
509519
#[deprecated(since = "0.21.0")]
510520
pub unsafe trait FromPyPointer<'p>: Sized {
511521
/// Convert from an arbitrary `PyObject`.
@@ -515,25 +525,19 @@ pub unsafe trait FromPyPointer<'p>: Sized {
515525
/// Implementations must ensure the object does not get freed during `'p`
516526
/// and ensure that `ptr` is of the correct type.
517527
/// Note that it must be safe to decrement the reference count of `ptr`.
518-
#[cfg_attr(
519-
not(feature = "gil-refs"),
520-
deprecated(
521-
since = "0.21.0",
522-
note = "use `Py::from_owned_ptr_or_opt(py, ptr)` or `Bound::from_owned_ptr_or_opt(py, ptr)` instead"
523-
)
528+
#[deprecated(
529+
since = "0.21.0",
530+
note = "use `Py::from_owned_ptr_or_opt(py, ptr)` or `Bound::from_owned_ptr_or_opt(py, ptr)` instead"
524531
)]
525532
unsafe fn from_owned_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject) -> Option<&'p Self>;
526533
/// Convert from an arbitrary `PyObject` or panic.
527534
///
528535
/// # Safety
529536
///
530537
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
531-
#[cfg_attr(
532-
not(feature = "gil-refs"),
533-
deprecated(
534-
since = "0.21.0",
535-
note = "use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
536-
)
538+
#[deprecated(
539+
since = "0.21.0",
540+
note = "use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
537541
)]
538542
unsafe fn from_owned_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
539543
#[allow(deprecated)]
@@ -544,12 +548,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
544548
/// # Safety
545549
///
546550
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
547-
#[cfg_attr(
548-
not(feature = "gil-refs"),
549-
deprecated(
550-
since = "0.21.0",
551-
note = "use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
552-
)
551+
#[deprecated(
552+
since = "0.21.0",
553+
note = "use `Py::from_owned_ptr(py, ptr)` or `Bound::from_owned_ptr(py, ptr)` instead"
553554
)]
554555
unsafe fn from_owned_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
555556
#[allow(deprecated)]
@@ -560,12 +561,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
560561
/// # Safety
561562
///
562563
/// Relies on [`from_owned_ptr_or_opt`](#method.from_owned_ptr_or_opt).
563-
#[cfg_attr(
564-
not(feature = "gil-refs"),
565-
deprecated(
566-
since = "0.21.0",
567-
note = "use `Py::from_owned_ptr_or_err(py, ptr)` or `Bound::from_owned_ptr_or_err(py, ptr)` instead"
568-
)
564+
#[deprecated(
565+
since = "0.21.0",
566+
note = "use `Py::from_owned_ptr_or_err(py, ptr)` or `Bound::from_owned_ptr_or_err(py, ptr)` instead"
569567
)]
570568
unsafe fn from_owned_ptr_or_err(py: Python<'p>, ptr: *mut ffi::PyObject) -> PyResult<&'p Self> {
571569
#[allow(deprecated)]
@@ -576,12 +574,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
576574
/// # Safety
577575
///
578576
/// Implementations must ensure the object does not get freed during `'p` and avoid type confusion.
579-
#[cfg_attr(
580-
not(feature = "gil-refs"),
581-
deprecated(
582-
since = "0.21.0",
583-
note = "use `Py::from_borrowed_ptr_or_opt(py, ptr)` or `Bound::from_borrowed_ptr_or_opt(py, ptr)` instead"
584-
)
577+
#[deprecated(
578+
since = "0.21.0",
579+
note = "use `Py::from_borrowed_ptr_or_opt(py, ptr)` or `Bound::from_borrowed_ptr_or_opt(py, ptr)` instead"
585580
)]
586581
unsafe fn from_borrowed_ptr_or_opt(py: Python<'p>, ptr: *mut ffi::PyObject)
587582
-> Option<&'p Self>;
@@ -590,12 +585,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
590585
/// # Safety
591586
///
592587
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
593-
#[cfg_attr(
594-
not(feature = "gil-refs"),
595-
deprecated(
596-
since = "0.21.0",
597-
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
598-
)
588+
#[deprecated(
589+
since = "0.21.0",
590+
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
599591
)]
600592
unsafe fn from_borrowed_ptr_or_panic(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
601593
#[allow(deprecated)]
@@ -606,12 +598,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
606598
/// # Safety
607599
///
608600
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
609-
#[cfg_attr(
610-
not(feature = "gil-refs"),
611-
deprecated(
612-
since = "0.21.0",
613-
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
614-
)
601+
#[deprecated(
602+
since = "0.21.0",
603+
note = "use `Py::from_borrowed_ptr(py, ptr)` or `Bound::from_borrowed_ptr(py, ptr)` instead"
615604
)]
616605
unsafe fn from_borrowed_ptr(py: Python<'p>, ptr: *mut ffi::PyObject) -> &'p Self {
617606
#[allow(deprecated)]
@@ -622,12 +611,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
622611
/// # Safety
623612
///
624613
/// Relies on unsafe fn [`from_borrowed_ptr_or_opt`](#method.from_borrowed_ptr_or_opt).
625-
#[cfg_attr(
626-
not(feature = "gil-refs"),
627-
deprecated(
628-
since = "0.21.0",
629-
note = "use `Py::from_borrowed_ptr_or_err(py, ptr)` or `Bound::from_borrowed_ptr_or_err(py, ptr)` instead"
630-
)
614+
#[deprecated(
615+
since = "0.21.0",
616+
note = "use `Py::from_borrowed_ptr_or_err(py, ptr)` or `Bound::from_borrowed_ptr_or_err(py, ptr)` instead"
631617
)]
632618
unsafe fn from_borrowed_ptr_or_err(
633619
py: Python<'p>,
@@ -638,6 +624,7 @@ pub unsafe trait FromPyPointer<'p>: Sized {
638624
}
639625
}
640626

627+
#[cfg(feature = "gil-refs")]
641628
#[allow(deprecated)]
642629
unsafe impl<'p, T> FromPyPointer<'p> for T
643630
where

src/gil.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ pub struct GILPool {
366366
impl GILPool {
367367
/// Creates a new [`GILPool`]. This function should only ever be called with the GIL held.
368368
///
369-
/// It is recommended not to use this API directly, but instead to use [`Python::new_pool`], as
369+
/// It is recommended not to use this API directly, but instead to use `Python::new_pool`, as
370370
/// that guarantees the GIL is held.
371371
///
372372
/// # Safety
373373
///
374-
/// As well as requiring the GIL, see the safety notes on [`Python::new_pool`].
374+
/// As well as requiring the GIL, see the safety notes on `Python::new_pool`.
375375
#[inline]
376376
pub unsafe fn new() -> GILPool {
377377
increment_gil_count();
@@ -462,6 +462,7 @@ pub unsafe fn register_decref(obj: NonNull<ffi::PyObject>) {
462462
///
463463
/// # Safety
464464
/// The object must be an owned Python reference.
465+
#[cfg(feature = "gil-refs")]
465466
pub unsafe fn register_owned(_py: Python<'_>, obj: NonNull<ffi::PyObject>) {
466467
debug_assert!(gil_is_acquired());
467468
// Ignores the error in case this function called from `atexit`.
@@ -507,9 +508,12 @@ fn decrement_gil_count() {
507508
mod tests {
508509
#[allow(deprecated)]
509510
use super::GILPool;
510-
use super::{gil_is_acquired, GIL_COUNT, OWNED_OBJECTS, POOL};
511+
use super::{gil_is_acquired, GIL_COUNT, POOL};
511512
use crate::types::any::PyAnyMethods;
512-
use crate::{ffi, gil, PyObject, Python};
513+
use crate::{ffi, PyObject, Python};
514+
#[cfg(feature = "gil-refs")]
515+
use {super::OWNED_OBJECTS, crate::gil};
516+
513517
use std::ptr::NonNull;
514518
#[cfg(not(target_arch = "wasm32"))]
515519
use std::sync;
@@ -518,6 +522,7 @@ mod tests {
518522
py.eval_bound("object()", None, None).unwrap().unbind()
519523
}
520524

525+
#[cfg(feature = "gil-refs")]
521526
fn owned_object_count() -> usize {
522527
#[cfg(debug_assertions)]
523528
let len = OWNED_OBJECTS.with(|owned_objects| owned_objects.borrow().len());
@@ -554,6 +559,7 @@ mod tests {
554559
}
555560

556561
#[test]
562+
#[cfg(feature = "gil-refs")]
557563
#[allow(deprecated)]
558564
fn test_owned() {
559565
Python::with_gil(|py| {
@@ -580,6 +586,7 @@ mod tests {
580586
}
581587

582588
#[test]
589+
#[cfg(feature = "gil-refs")]
583590
#[allow(deprecated)]
584591
fn test_owned_nested() {
585592
Python::with_gil(|py| {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@
317317
//! [`Ungil`]: crate::marker::Ungil
318318
pub use crate::class::*;
319319
pub use crate::conversion::{AsPyPointer, FromPyObject, IntoPy, ToPyObject};
320+
#[cfg(feature = "gil-refs")]
320321
#[allow(deprecated)]
321322
pub use crate::conversion::{FromPyPointer, PyTryFrom, PyTryInto};
322323
pub use crate::err::{

0 commit comments

Comments
 (0)