@@ -2,10 +2,11 @@ use crate::cross::clrepr::CLReprObject;
22use crate :: cross:: { CLRepr , CLReprObjectKind , StringType } ;
33use pyo3:: exceptions:: { PyNotImplementedError , PyTypeError } ;
44use pyo3:: types:: {
5- PyBool , PyComplex , PyDate , PyDict , PyFloat , PyFrame , PyFunction , PyInt , PyList , PySequence ,
6- PySet , PyString , PyTraceback , PyTuple ,
5+ PyAnyMethods , PyBool , PyBoolMethods , PyComplex , PyDate , PyDict , PyDictMethods , PyFloat ,
6+ PyFloatMethods , PyFrame , PyFunction , PyInt , PyList , PyListMethods , PySequence , PySet ,
7+ PySetMethods , PyString , PyTraceback , PyTuple , PyTupleMethods , PyTypeMethods ,
78} ;
8- use pyo3:: { Py , PyAny , PyErr , PyObject , Python , ToPyObject } ;
9+ use pyo3:: { Bound , Py , PyAny , PyErr , PyObject , Python , ToPyObject } ;
910
1011#[ derive( Debug , Clone ) ]
1112pub enum PythonRef {
@@ -18,7 +19,7 @@ pub enum PythonRef {
1819
1920impl CLRepr {
2021 /// Convert python value to CLRepr
21- pub fn from_python_ref ( v : & PyAny ) -> Result < Self , PyErr > {
22+ pub fn from_python_ref ( v : & Bound < ' _ , PyAny > ) -> Result < Self , PyErr > {
2223 if v. is_none ( ) {
2324 return Ok ( Self :: Null ) ;
2425 }
@@ -30,7 +31,7 @@ impl CLRepr {
3031 StringType :: Normal
3132 } ;
3233
33- Self :: String ( v. to_string ( ) , string_type)
34+ Self :: String ( v. str ( ) ? . to_string ( ) , string_type)
3435 } else if v. get_type ( ) . is_subclass_of :: < PyBool > ( ) ? {
3536 Self :: Bool ( v. downcast :: < PyBool > ( ) ?. is_true ( ) )
3637 } else if v. get_type ( ) . is_subclass_of :: < PyFloat > ( ) ? {
@@ -47,7 +48,7 @@ impl CLRepr {
4748 if k. get_type ( ) . is_subclass_of :: < PyString > ( ) ? {
4849 let key_str = k. downcast :: < PyString > ( ) ?;
4950
50- obj. insert ( key_str. to_string ( ) , Self :: from_python_ref ( v) ?) ;
51+ obj. insert ( key_str. to_string ( ) , Self :: from_python_ref ( & v) ?) ;
5152 }
5253 }
5354
@@ -57,7 +58,7 @@ impl CLRepr {
5758 let mut r = Vec :: with_capacity ( l. len ( ) ) ;
5859
5960 for v in l. iter ( ) {
60- r. push ( Self :: from_python_ref ( v) ?) ;
61+ r. push ( Self :: from_python_ref ( & v) ?) ;
6162 }
6263
6364 Self :: Array ( r)
@@ -66,7 +67,7 @@ impl CLRepr {
6667 let mut r = Vec :: with_capacity ( l. len ( ) ) ;
6768
6869 for v in l. iter ( ) {
69- r. push ( Self :: from_python_ref ( v) ?) ;
70+ r. push ( Self :: from_python_ref ( & v) ?) ;
7071 }
7172
7273 Self :: Array ( r)
@@ -75,12 +76,12 @@ impl CLRepr {
7576 let mut r = Vec :: with_capacity ( l. len ( ) ) ;
7677
7778 for v in l. iter ( ) {
78- r. push ( Self :: from_python_ref ( v) ?) ;
79+ r. push ( Self :: from_python_ref ( & v) ?) ;
7980 }
8081
8182 Self :: Tuple ( r)
8283 } else if v. get_type ( ) . is_subclass_of :: < PyFunction > ( ) ? {
83- let fun: Py < PyFunction > = v. downcast :: < PyFunction > ( ) ?. into ( ) ;
84+ let fun: Py < PyFunction > = v. downcast :: < PyFunction > ( ) ?. clone ( ) . unbind ( ) ;
8485
8586 Self :: PythonRef ( PythonRef :: PyFunction ( fun) )
8687 } else if v. get_type ( ) . is_subclass_of :: < PyComplex > ( ) ? {
@@ -116,12 +117,12 @@ impl CLRepr {
116117 ) ) ) ;
117118 }
118119
119- Self :: PythonRef ( PythonRef :: PyObject ( v. into ( ) ) )
120+ Self :: PythonRef ( PythonRef :: PyObject ( v. clone ( ) . unbind ( ) ) )
120121 } )
121122 }
122123
123- fn into_py_dict_impl ( obj : CLReprObject , py : Python ) -> Result < & PyDict , PyErr > {
124- let r = PyDict :: new ( py) ;
124+ fn into_py_dict_impl ( obj : CLReprObject , py : Python ) -> Result < Bound < ' _ , PyDict > , PyErr > {
125+ let r = PyDict :: new_bound ( py) ;
125126
126127 for ( k, v) in obj. into_iter ( ) {
127128 r. set_item ( k, Self :: into_py_impl ( v, py) ?) ?;
@@ -132,11 +133,11 @@ impl CLRepr {
132133
133134 fn into_py_impl ( from : CLRepr , py : Python ) -> Result < PyObject , PyErr > {
134135 Ok ( match from {
135- CLRepr :: String ( v, _) => PyString :: new ( py, & v) . to_object ( py) ,
136- CLRepr :: Bool ( v) => PyBool :: new ( py, v) . to_object ( py) ,
137- CLRepr :: Float ( v) => PyFloat :: new ( py, v) . to_object ( py) ,
136+ CLRepr :: String ( v, _) => PyString :: new_bound ( py, & v) . to_object ( py) ,
137+ CLRepr :: Bool ( v) => PyBool :: new_bound ( py, v) . to_object ( py) ,
138+ CLRepr :: Float ( v) => PyFloat :: new_bound ( py, v) . to_object ( py) ,
138139 CLRepr :: Int ( v) => {
139- let py_int: & PyInt = unsafe { py . from_owned_ptr ( pyo3:: ffi:: PyLong_FromLong ( v) ) } ;
140+ let py_int = unsafe { Bound :: from_owned_ptr ( py , pyo3:: ffi:: PyLong_FromLong ( v) ) } ;
140141
141142 py_int. to_object ( py)
142143 }
@@ -147,7 +148,7 @@ impl CLRepr {
147148 elements. push ( Self :: into_py_impl ( el, py) ?) ;
148149 }
149150
150- PyList :: new ( py, elements) . to_object ( py)
151+ PyList :: new_bound ( py, elements) . to_object ( py)
151152 }
152153 CLRepr :: Tuple ( arr) => {
153154 let mut elements = Vec :: with_capacity ( arr. len ( ) ) ;
@@ -156,7 +157,7 @@ impl CLRepr {
156157 elements. push ( Self :: into_py_impl ( el, py) ?) ;
157158 }
158159
159- PyTuple :: new ( py, elements) . to_object ( py)
160+ PyTuple :: new_bound ( py, elements) . to_object ( py)
160161 }
161162 CLRepr :: Object ( obj) => {
162163 let r = Self :: into_py_dict_impl ( obj, py) ?;
@@ -189,7 +190,7 @@ impl CLRepr {
189190 } )
190191 }
191192
192- pub fn into_py_dict ( self , py : Python ) -> Result < & PyDict , PyErr > {
193+ pub fn into_py_dict ( self , py : Python ) -> Result < Bound < ' _ , PyDict > , PyErr > {
193194 Ok ( match self {
194195 CLRepr :: Object ( obj) => Self :: into_py_dict_impl ( obj, py) ?,
195196 other => {
0 commit comments