1
1
//! Defines conversions between Rust and Python types.
2
- use crate :: err:: { self , PyDowncastError , PyResult } ;
2
+ use crate :: err:: PyResult ;
3
3
#[ cfg( feature = "experimental-inspect" ) ]
4
4
use crate :: inspect:: types:: TypeInfo ;
5
5
use crate :: pyclass:: boolean_struct:: False ;
6
6
use crate :: types:: any:: PyAnyMethods ;
7
7
use crate :: types:: PyTuple ;
8
8
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 ,
10
18
} ;
11
- use std:: ptr:: NonNull ;
12
19
13
20
/// Returns a borrowed pointer to a Python object.
14
21
///
@@ -385,6 +392,7 @@ where
385
392
/// If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
386
393
///
387
394
/// This trait is similar to `std::convert::TryFrom`
395
+ #[ cfg( feature = "gil-refs" ) ]
388
396
#[ deprecated( since = "0.21.0" ) ]
389
397
pub trait PyTryFrom < ' v > : Sized + PyNativeType {
390
398
/// Cast from a concrete Python object type to PyObject.
@@ -416,6 +424,7 @@ pub trait PyTryFrom<'v>: Sized + PyNativeType {
416
424
417
425
/// Trait implemented by Python object types that allow a checked downcast.
418
426
/// This trait is similar to `std::convert::TryInto`
427
+ #[ cfg( feature = "gil-refs" ) ]
419
428
#[ deprecated( since = "0.21.0" ) ]
420
429
pub trait PyTryInto < T > : Sized {
421
430
/// Cast from PyObject to a concrete Python object type.
@@ -506,6 +515,7 @@ impl IntoPy<Py<PyTuple>> for () {
506
515
/// # Safety
507
516
///
508
517
/// See safety notes on individual functions.
518
+ #[ cfg( feature = "gil-refs" ) ]
509
519
#[ deprecated( since = "0.21.0" ) ]
510
520
pub unsafe trait FromPyPointer < ' p > : Sized {
511
521
/// Convert from an arbitrary `PyObject`.
@@ -515,25 +525,19 @@ pub unsafe trait FromPyPointer<'p>: Sized {
515
525
/// Implementations must ensure the object does not get freed during `'p`
516
526
/// and ensure that `ptr` is of the correct type.
517
527
/// 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"
524
531
) ]
525
532
unsafe fn from_owned_ptr_or_opt ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> Option < & ' p Self > ;
526
533
/// Convert from an arbitrary `PyObject` or panic.
527
534
///
528
535
/// # Safety
529
536
///
530
537
/// 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"
537
541
) ]
538
542
unsafe fn from_owned_ptr_or_panic ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> & ' p Self {
539
543
#[ allow( deprecated) ]
@@ -544,12 +548,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
544
548
/// # Safety
545
549
///
546
550
/// 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"
553
554
) ]
554
555
unsafe fn from_owned_ptr ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> & ' p Self {
555
556
#[ allow( deprecated) ]
@@ -560,12 +561,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
560
561
/// # Safety
561
562
///
562
563
/// 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"
569
567
) ]
570
568
unsafe fn from_owned_ptr_or_err ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> PyResult < & ' p Self > {
571
569
#[ allow( deprecated) ]
@@ -576,12 +574,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
576
574
/// # Safety
577
575
///
578
576
/// 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"
585
580
) ]
586
581
unsafe fn from_borrowed_ptr_or_opt ( py : Python < ' p > , ptr : * mut ffi:: PyObject )
587
582
-> Option < & ' p Self > ;
@@ -590,12 +585,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
590
585
/// # Safety
591
586
///
592
587
/// 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"
599
591
) ]
600
592
unsafe fn from_borrowed_ptr_or_panic ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> & ' p Self {
601
593
#[ allow( deprecated) ]
@@ -606,12 +598,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
606
598
/// # Safety
607
599
///
608
600
/// 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"
615
604
) ]
616
605
unsafe fn from_borrowed_ptr ( py : Python < ' p > , ptr : * mut ffi:: PyObject ) -> & ' p Self {
617
606
#[ allow( deprecated) ]
@@ -622,12 +611,9 @@ pub unsafe trait FromPyPointer<'p>: Sized {
622
611
/// # Safety
623
612
///
624
613
/// 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"
631
617
) ]
632
618
unsafe fn from_borrowed_ptr_or_err (
633
619
py : Python < ' p > ,
@@ -638,6 +624,7 @@ pub unsafe trait FromPyPointer<'p>: Sized {
638
624
}
639
625
}
640
626
627
+ #[ cfg( feature = "gil-refs" ) ]
641
628
#[ allow( deprecated) ]
642
629
unsafe impl < ' p , T > FromPyPointer < ' p > for T
643
630
where
0 commit comments