@@ -2,6 +2,7 @@ use crate::class::basic::CompareOp;
22use crate :: conversion:: { AsPyPointer , FromPyObject , IntoPy , PyTryFrom , ToPyObject } ;
33use crate :: err:: { PyDowncastError , PyErr , PyResult } ;
44use crate :: exceptions:: { PyAttributeError , PyTypeError } ;
5+ use crate :: ffi_ptr_ext:: FfiPtrExt ;
56use crate :: instance:: Py2 ;
67use crate :: type_object:: PyTypeInfo ;
78#[ cfg( not( PyPy ) ) ]
@@ -12,6 +13,8 @@ use std::cell::UnsafeCell;
1213use std:: cmp:: Ordering ;
1314use std:: os:: raw:: c_int;
1415
16+ use crate :: py_result_ext:: PyResultExt ;
17+
1518/// Represents any Python object.
1619///
1720/// It currently only appears as a *reference*, `&PyAny`,
@@ -1708,10 +1711,8 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
17081711 attr_name : Py2 < ' _ , PyString > ,
17091712 ) -> PyResult < Py2 < ' py , PyAny > > {
17101713 unsafe {
1711- Py2 :: from_owned_ptr_or_err (
1712- any. py ( ) ,
1713- ffi:: PyObject_GetAttr ( any. as_ptr ( ) , attr_name. as_ptr ( ) ) ,
1714- )
1714+ ffi:: PyObject_GetAttr ( any. as_ptr ( ) , attr_name. as_ptr ( ) )
1715+ . assume_owned_or_ffi_error ( any. py ( ) )
17151716 }
17161717 }
17171718
@@ -1761,12 +1762,12 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
17611762 O : ToPyObject ,
17621763 {
17631764 fn inner ( any : & Py2 < ' _ , PyAny > , other : Py2 < ' _ , PyAny > ) -> PyResult < Ordering > {
1764- let py = any. py ( ) ;
17651765 let other = other. as_ptr ( ) ;
17661766 // Almost the same as ffi::PyObject_RichCompareBool, but this one doesn't try self == other.
17671767 // See https://github.com/PyO3/pyo3/issues/985 for more.
17681768 let do_compare = |other, op| unsafe {
1769- Py2 :: from_owned_ptr_or_err ( py, ffi:: PyObject_RichCompare ( any. as_ptr ( ) , other, op) )
1769+ ffi:: PyObject_RichCompare ( any. as_ptr ( ) , other, op)
1770+ . assume_owned_or_ffi_error ( any. py ( ) )
17701771 . and_then ( |obj| obj. is_true ( ) )
17711772 } ;
17721773 if do_compare ( other, ffi:: Py_EQ ) ? {
@@ -1796,10 +1797,8 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
17961797 compare_op : CompareOp ,
17971798 ) -> PyResult < Py2 < ' py , PyAny > > {
17981799 unsafe {
1799- Py2 :: from_owned_ptr_or_err (
1800- any. py ( ) ,
1801- ffi:: PyObject_RichCompare ( any. as_ptr ( ) , other. as_ptr ( ) , compare_op as c_int ) ,
1802- )
1800+ ffi:: PyObject_RichCompare ( any. as_ptr ( ) , other. as_ptr ( ) , compare_op as c_int )
1801+ . assume_owned_or_ffi_error ( any. py ( ) )
18031802 }
18041803 }
18051804
@@ -1870,14 +1869,12 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
18701869 kwargs : Option < & PyDict > ,
18711870 ) -> PyResult < Py2 < ' py , PyAny > > {
18721871 unsafe {
1873- Py2 :: from_owned_ptr_or_err (
1874- any. py ( ) ,
1875- ffi:: PyObject_Call (
1876- any. as_ptr ( ) ,
1877- args. as_ptr ( ) ,
1878- kwargs. map_or ( std:: ptr:: null_mut ( ) , |dict| dict. as_ptr ( ) ) ,
1879- ) ,
1872+ ffi:: PyObject_Call (
1873+ any. as_ptr ( ) ,
1874+ args. as_ptr ( ) ,
1875+ kwargs. map_or ( std:: ptr:: null_mut ( ) , |dict| dict. as_ptr ( ) ) ,
18801876 )
1877+ . assume_owned_or_ffi_error ( any. py ( ) )
18811878 }
18821879 }
18831880
@@ -1893,7 +1890,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
18931890 ) ) ] {
18941891 // Optimized path on python 3.9+
18951892 unsafe {
1896- Py2 :: from_owned_ptr_or_err ( self . py ( ) , ffi :: PyObject_CallNoArgs ( self . as_ptr ( ) ) )
1893+ ffi :: PyObject_CallNoArgs ( self . as_ptr ( ) ) . assume_owned_or_ffi_error ( self . py ( ) )
18971894 }
18981895 } else {
18991896 self . call( ( ) , None )
@@ -1930,7 +1927,7 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
19301927 // Optimized path on python 3.9+
19311928 unsafe {
19321929 let name = name. into_py( py) . attach_into( py) ;
1933- Py2 :: from_owned_ptr_or_err ( py , ffi:: PyObject_CallMethodNoArgs ( self . as_ptr( ) , name. as_ptr( ) ) )
1930+ ffi:: PyObject_CallMethodNoArgs ( self . as_ptr( ) , name. as_ptr( ) ) . assume_owned_or_ffi_error ( py )
19341931 }
19351932 } else {
19361933 self . call_method( name, ( ) , None )
@@ -1971,10 +1968,8 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
19711968 {
19721969 fn inner < ' py > ( any : & Py2 < ' py , PyAny > , key : Py2 < ' _ , PyAny > ) -> PyResult < Py2 < ' py , PyAny > > {
19731970 unsafe {
1974- Py2 :: from_owned_ptr_or_err (
1975- any. py ( ) ,
1976- ffi:: PyObject_GetItem ( any. as_ptr ( ) , key. as_ptr ( ) ) ,
1977- )
1971+ ffi:: PyObject_GetItem ( any. as_ptr ( ) , key. as_ptr ( ) )
1972+ . assume_owned_or_ffi_error ( any. py ( ) )
19781973 }
19791974 }
19801975
@@ -2103,15 +2098,17 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
21032098
21042099 fn repr ( & self ) -> PyResult < Py2 < ' py , PyString > > {
21052100 unsafe {
2106- Py2 :: from_owned_ptr_or_err ( self . py ( ) , ffi:: PyObject_Repr ( self . as_ptr ( ) ) )
2107- . map ( |any| any. downcast_into_unchecked ( ) )
2101+ ffi:: PyObject_Repr ( self . as_ptr ( ) )
2102+ . assume_owned_or_ffi_error ( self . py ( ) )
2103+ . downcast_into_unchecked ( )
21082104 }
21092105 }
21102106
21112107 fn str ( & self ) -> PyResult < Py2 < ' py , PyString > > {
21122108 unsafe {
2113- Py2 :: from_owned_ptr_or_err ( self . py ( ) , ffi:: PyObject_Str ( self . as_ptr ( ) ) )
2114- . map ( |any| any. downcast_into_unchecked ( ) )
2109+ ffi:: PyObject_Str ( self . as_ptr ( ) )
2110+ . assume_owned_or_ffi_error ( self . py ( ) )
2111+ . downcast_into_unchecked ( )
21152112 }
21162113 }
21172114
@@ -2129,7 +2126,8 @@ impl<'py> PyAnyMethods<'py> for Py2<'py, PyAny> {
21292126
21302127 fn dir ( & self ) -> Py2 < ' py , PyList > {
21312128 unsafe {
2132- Py2 :: from_owned_ptr ( self . py ( ) , ffi:: PyObject_Dir ( self . as_ptr ( ) ) )
2129+ ffi:: PyObject_Dir ( self . as_ptr ( ) )
2130+ . assume_owned_and_infallible ( self . py ( ) )
21332131 . downcast_into_unchecked ( )
21342132 }
21352133 }
0 commit comments