1
- use crate :: err:: { self , PyDowncastError , PyErr , PyResult } ;
1
+ use crate :: err:: { self , PyErr , PyResult } ;
2
2
use crate :: impl_:: pycell:: PyClassObject ;
3
3
use crate :: pycell:: { PyBorrowError , PyBorrowMutError } ;
4
4
use crate :: pyclass:: boolean_struct:: { False , True } ;
@@ -721,12 +721,12 @@ impl<T> IntoPy<PyObject> for Borrowed<'_, '_, T> {
721
721
///
722
722
/// This type does not auto-dereference to the inner object because you must prove you hold the GIL to access it.
723
723
/// Instead, call one of its methods to access the inner object:
724
- /// - [`Py::as_ref `], to borrow a GIL-bound reference to the contained object.
724
+ /// - [`Py::bind`] or [`Py::into_bound `], to borrow a GIL-bound reference to the contained object.
725
725
/// - [`Py::borrow`], [`Py::try_borrow`], [`Py::borrow_mut`], or [`Py::try_borrow_mut`],
726
726
/// to get a (mutable) reference to a contained pyclass, using a scheme similar to std's [`RefCell`].
727
- /// See the [`PyCell` guide entry](https://pyo3.rs/latest/class.html#pycell -and-interior-mutability)
727
+ /// See the [guide entry](https://pyo3.rs/latest/class.html#bound -and-interior-mutability)
728
728
/// for more information.
729
- /// - You can call methods directly on `Py` with [`Py::call `], [`Py::call_method `] and friends.
729
+ /// - You can call methods directly on `Py` with [`Py::call_bound `], [`Py::call_method_bound `] and friends.
730
730
/// These require passing in the [`Python<'py>`](crate::Python) token but are otherwise similar to the corresponding
731
731
/// methods on [`PyAny`].
732
732
///
@@ -991,12 +991,10 @@ where
991
991
/// assert!(my_class_cell.try_borrow().is_ok());
992
992
/// });
993
993
/// ```
994
- #[ cfg_attr(
995
- not( feature = "gil-refs" ) ,
996
- deprecated(
997
- since = "0.21.0" ,
998
- note = "use `obj.bind(py)` instead of `obj.as_ref(py)`"
999
- )
994
+ #[ cfg( feature = "gil-refs" ) ]
995
+ #[ deprecated(
996
+ since = "0.21.0" ,
997
+ note = "use `obj.bind(py)` instead of `obj.as_ref(py)`"
1000
998
) ]
1001
999
pub fn as_ref < ' py > ( & ' py self , _py : Python < ' py > ) -> & ' py T :: AsRefTarget {
1002
1000
let any = self . as_ptr ( ) as * const PyAny ;
@@ -1046,12 +1044,10 @@ where
1046
1044
/// obj.into_ref(py)
1047
1045
/// }
1048
1046
/// ```
1049
- #[ cfg_attr(
1050
- not( feature = "gil-refs" ) ,
1051
- deprecated(
1052
- since = "0.21.0" ,
1053
- note = "use `obj.into_bound(py)` instead of `obj.into_ref(py)`"
1054
- )
1047
+ #[ cfg( feature = "gil-refs" ) ]
1048
+ #[ deprecated(
1049
+ since = "0.21.0" ,
1050
+ note = "use `obj.into_bound(py)` instead of `obj.into_ref(py)`"
1055
1051
) ]
1056
1052
pub fn into_ref ( self , py : Python < ' _ > ) -> & T :: AsRefTarget {
1057
1053
#[ allow( deprecated) ]
@@ -1464,12 +1460,10 @@ impl<T> Py<T> {
1464
1460
}
1465
1461
1466
1462
/// Deprecated form of [`call_bound`][Py::call_bound].
1467
- #[ cfg_attr(
1468
- not( feature = "gil-refs" ) ,
1469
- deprecated(
1470
- since = "0.21.0" ,
1471
- note = "`call` will be replaced by `call_bound` in a future PyO3 version"
1472
- )
1463
+ #[ cfg( feature = "gil-refs" ) ]
1464
+ #[ deprecated(
1465
+ since = "0.21.0" ,
1466
+ note = "`call` will be replaced by `call_bound` in a future PyO3 version"
1473
1467
) ]
1474
1468
#[ inline]
1475
1469
pub fn call < A > ( & self , py : Python < ' _ > , args : A , kwargs : Option < & PyDict > ) -> PyResult < PyObject >
@@ -1506,12 +1500,10 @@ impl<T> Py<T> {
1506
1500
}
1507
1501
1508
1502
/// Deprecated form of [`call_method_bound`][Py::call_method_bound].
1509
- #[ cfg_attr(
1510
- not( feature = "gil-refs" ) ,
1511
- deprecated(
1512
- since = "0.21.0" ,
1513
- note = "`call_method` will be replaced by `call_method_bound` in a future PyO3 version"
1514
- )
1503
+ #[ cfg( feature = "gil-refs" ) ]
1504
+ #[ deprecated(
1505
+ since = "0.21.0" ,
1506
+ note = "`call_method` will be replaced by `call_method_bound` in a future PyO3 version"
1515
1507
) ]
1516
1508
#[ inline]
1517
1509
pub fn call_method < N , A > (
@@ -1779,6 +1771,7 @@ impl<T> std::convert::From<Bound<'_, T>> for Py<T> {
1779
1771
}
1780
1772
1781
1773
// `&PyCell<T>` can be converted to `Py<T>`
1774
+ #[ cfg( feature = "gil-refs" ) ]
1782
1775
#[ allow( deprecated) ]
1783
1776
impl < T > std:: convert:: From < & crate :: PyCell < T > > for Py < T >
1784
1777
where
@@ -1844,10 +1837,7 @@ where
1844
1837
{
1845
1838
/// Extracts `Self` from the source `PyObject`.
1846
1839
fn extract_bound ( ob : & Bound < ' py , PyAny > ) -> PyResult < Self > {
1847
- // TODO update MSRV past 1.59 and use .cloned() to make
1848
- // clippy happy
1849
- #[ allow( clippy:: map_clone) ]
1850
- ob. downcast ( ) . map ( Clone :: clone) . map_err ( Into :: into)
1840
+ ob. downcast ( ) . cloned ( ) . map_err ( Into :: into)
1851
1841
}
1852
1842
}
1853
1843
@@ -1888,21 +1878,22 @@ pub type PyObject = Py<PyAny>;
1888
1878
1889
1879
impl PyObject {
1890
1880
/// Deprecated form of [`PyObject::downcast_bound`]
1891
- #[ cfg_attr(
1892
- not( feature = "gil-refs" ) ,
1893
- deprecated(
1894
- since = "0.21.0" ,
1895
- note = "`PyObject::downcast` will be replaced by `PyObject::downcast_bound` in a future PyO3 version"
1896
- )
1881
+ #[ cfg( feature = "gil-refs" ) ]
1882
+ #[ deprecated(
1883
+ since = "0.21.0" ,
1884
+ note = "`PyObject::downcast` will be replaced by `PyObject::downcast_bound` in a future PyO3 version"
1897
1885
) ]
1898
1886
#[ inline]
1899
- pub fn downcast < ' py , T > ( & ' py self , py : Python < ' py > ) -> Result < & ' py T , PyDowncastError < ' py > >
1887
+ pub fn downcast < ' py , T > (
1888
+ & ' py self ,
1889
+ py : Python < ' py > ,
1890
+ ) -> Result < & ' py T , crate :: err:: PyDowncastError < ' py > >
1900
1891
where
1901
1892
T : PyTypeCheck < AsRefTarget = T > ,
1902
1893
{
1903
1894
self . downcast_bound :: < T > ( py)
1904
1895
. map ( Bound :: as_gil_ref)
1905
- . map_err ( PyDowncastError :: from_downcast_err)
1896
+ . map_err ( crate :: err :: PyDowncastError :: from_downcast_err)
1906
1897
}
1907
1898
/// Downcast this `PyObject` to a concrete Python type or pyclass.
1908
1899
///
@@ -1970,12 +1961,10 @@ impl PyObject {
1970
1961
/// # Safety
1971
1962
///
1972
1963
/// Callers must ensure that the type is valid or risk type confusion.
1973
- #[ cfg_attr(
1974
- not( feature = "gil-refs" ) ,
1975
- deprecated(
1976
- since = "0.21.0" ,
1977
- note = "`PyObject::downcast_unchecked` will be replaced by `PyObject::downcast_bound_unchecked` in a future PyO3 version"
1978
- )
1964
+ #[ cfg( feature = "gil-refs" ) ]
1965
+ #[ deprecated(
1966
+ since = "0.21.0" ,
1967
+ note = "`PyObject::downcast_unchecked` will be replaced by `PyObject::downcast_bound_unchecked` in a future PyO3 version"
1979
1968
) ]
1980
1969
#[ inline]
1981
1970
pub unsafe fn downcast_unchecked < ' py , T > ( & ' py self , py : Python < ' py > ) -> & T
@@ -1997,35 +1986,31 @@ impl PyObject {
1997
1986
}
1998
1987
1999
1988
#[ cfg( test) ]
2000
- #[ cfg_attr( not( feature = "gil-refs" ) , allow( deprecated) ) ]
2001
1989
mod tests {
2002
1990
use super :: { Bound , Py , PyObject } ;
2003
1991
use crate :: types:: any:: PyAnyMethods ;
2004
1992
use crate :: types:: { dict:: IntoPyDict , PyDict , PyString } ;
2005
1993
use crate :: types:: { PyCapsule , PyStringMethods } ;
2006
- use crate :: { ffi, Borrowed , PyAny , PyNativeType , PyResult , Python , ToPyObject } ;
1994
+ use crate :: { ffi, Borrowed , PyAny , PyResult , Python , ToPyObject } ;
2007
1995
2008
1996
#[ test]
2009
1997
fn test_call ( ) {
2010
1998
Python :: with_gil ( |py| {
2011
- let obj = py. get_type :: < PyDict > ( ) . to_object ( py) ;
1999
+ let obj = py. get_type_bound :: < PyDict > ( ) . to_object ( py) ;
2012
2000
2013
- let assert_repr = |obj : & PyAny , expected : & str | {
2014
- assert_eq ! ( obj. repr( ) . unwrap( ) . to_str ( ) . unwrap( ) , expected) ;
2001
+ let assert_repr = |obj : & Bound < ' _ , PyAny > , expected : & str | {
2002
+ assert_eq ! ( obj. repr( ) . unwrap( ) . to_cow ( ) . unwrap( ) , expected) ;
2015
2003
} ;
2016
2004
2017
- assert_repr ( obj. call0 ( py) . unwrap ( ) . as_ref ( py) , "{}" ) ;
2018
- assert_repr ( obj. call1 ( py, ( ) ) . unwrap ( ) . as_ref ( py) , "{}" ) ;
2019
- assert_repr ( obj. call ( py, ( ) , None ) . unwrap ( ) . as_ref ( py) , "{}" ) ;
2005
+ assert_repr ( obj. call0 ( py) . unwrap ( ) . bind ( py) , "{}" ) ;
2006
+ assert_repr ( obj. call1 ( py, ( ) ) . unwrap ( ) . bind ( py) , "{}" ) ;
2007
+ assert_repr ( obj. call_bound ( py, ( ) , None ) . unwrap ( ) . bind ( py) , "{}" ) ;
2020
2008
2021
- assert_repr (
2022
- obj. call1 ( py, ( ( ( 'x' , 1 ) , ) , ) ) . unwrap ( ) . as_ref ( py) ,
2023
- "{'x': 1}" ,
2024
- ) ;
2009
+ assert_repr ( obj. call1 ( py, ( ( ( 'x' , 1 ) , ) , ) ) . unwrap ( ) . bind ( py) , "{'x': 1}" ) ;
2025
2010
assert_repr (
2026
2011
obj. call_bound ( py, ( ) , Some ( & [ ( 'x' , 1 ) ] . into_py_dict_bound ( py) ) )
2027
2012
. unwrap ( )
2028
- . as_ref ( py) ,
2013
+ . bind ( py) ,
2029
2014
"{'x': 1}" ,
2030
2015
) ;
2031
2016
} )
@@ -2037,7 +2022,7 @@ mod tests {
2037
2022
let obj: PyObject = PyDict :: new_bound ( py) . into ( ) ;
2038
2023
assert ! ( obj. call_method0( py, "asdf" ) . is_err( ) ) ;
2039
2024
assert ! ( obj
2040
- . call_method ( py, "nonexistent_method" , ( 1 , ) , None )
2025
+ . call_method_bound ( py, "nonexistent_method" , ( 1 , ) , None )
2041
2026
. is_err( ) ) ;
2042
2027
assert ! ( obj. call_method0( py, "nonexistent_method" ) . is_err( ) ) ;
2043
2028
assert ! ( obj. call_method1( py, "nonexistent_method" , ( 1 , ) ) . is_err( ) ) ;
@@ -2083,7 +2068,7 @@ a = A()
2083
2068
2084
2069
assert ! ( instance
2085
2070
. getattr( py, "foo" ) ?
2086
- . as_ref ( py)
2071
+ . bind ( py)
2087
2072
. eq( PyString :: new_bound( py, "bar" ) ) ?) ;
2088
2073
2089
2074
instance. getattr ( py, "foo" ) ?;
@@ -2109,15 +2094,15 @@ a = A()
2109
2094
2110
2095
instance. getattr ( py, foo) . unwrap_err ( ) ;
2111
2096
instance. setattr ( py, foo, bar) ?;
2112
- assert ! ( instance. getattr( py, foo) ?. as_ref ( py) . eq( bar) ?) ;
2097
+ assert ! ( instance. getattr( py, foo) ?. bind ( py) . eq( bar) ?) ;
2113
2098
Ok ( ( ) )
2114
2099
} )
2115
2100
}
2116
2101
2117
2102
#[ test]
2118
2103
fn invalid_attr ( ) -> PyResult < ( ) > {
2119
2104
Python :: with_gil ( |py| {
2120
- let instance: Py < PyAny > = py. eval ( "object()" , None , None ) ?. into ( ) ;
2105
+ let instance: Py < PyAny > = py. eval_bound ( "object()" , None , None ) ?. into ( ) ;
2121
2106
2122
2107
instance. getattr ( py, "foo" ) . unwrap_err ( ) ;
2123
2108
@@ -2130,7 +2115,7 @@ a = A()
2130
2115
#[ test]
2131
2116
fn test_py2_from_py_object ( ) {
2132
2117
Python :: with_gil ( |py| {
2133
- let instance: & PyAny = py. eval ( "object()" , None , None ) . unwrap ( ) ;
2118
+ let instance = py. eval_bound ( "object()" , None , None ) . unwrap ( ) ;
2134
2119
let ptr = instance. as_ptr ( ) ;
2135
2120
let instance: Bound < ' _ , PyAny > = instance. extract ( ) . unwrap ( ) ;
2136
2121
assert_eq ! ( instance. as_ptr( ) , ptr) ;
@@ -2141,7 +2126,7 @@ a = A()
2141
2126
fn test_py2_into_py_object ( ) {
2142
2127
Python :: with_gil ( |py| {
2143
2128
let instance = py
2144
- . eval ( "object()" , None , None )
2129
+ . eval_bound ( "object()" , None , None )
2145
2130
. unwrap ( )
2146
2131
. as_borrowed ( )
2147
2132
. to_owned ( ) ;
0 commit comments