@@ -7,25 +7,24 @@ use ::libipld::cid::{Cid, Error as CidError, Result as CidResult, Version};
77use anyhow:: { anyhow, Result } ;
88use byteorder:: { BigEndian , ByteOrder } ;
99use multihash:: Multihash ;
10- use pyo3:: { ffi, prelude:: * , types:: * , PyObject , Python } ;
11- use pyo3:: conversion:: ToPyObject ;
10+ use pyo3:: { ffi, prelude:: * , types:: * , BoundObject , PyObject , Python } ;
1211use pyo3:: pybacked:: PyBackedStr ;
1312
1413fn cid_hash_to_pydict < ' py > ( py : Python < ' py > , cid : & Cid ) -> Bound < ' py , PyDict > {
1514 let hash = cid. hash ( ) ;
16- let dict_obj = PyDict :: new_bound ( py) ;
15+ let dict_obj = PyDict :: new ( py) ;
1716
1817 dict_obj. set_item ( "code" , hash. code ( ) ) . unwrap ( ) ;
1918 dict_obj. set_item ( "size" , hash. size ( ) ) . unwrap ( ) ;
2019 dict_obj
21- . set_item ( "digest" , PyBytes :: new_bound ( py, & hash. digest ( ) ) )
20+ . set_item ( "digest" , PyBytes :: new ( py, & hash. digest ( ) ) )
2221 . unwrap ( ) ;
2322
2423 dict_obj
2524}
2625
2726fn cid_to_pydict < ' py > ( py : Python < ' py > , cid : & Cid ) -> Bound < ' py , PyDict > {
28- let dict_obj = PyDict :: new_bound ( py) ;
27+ let dict_obj = PyDict :: new ( py) ;
2928
3029 dict_obj. set_item ( "version" , cid. version ( ) as u64 ) . unwrap ( ) ;
3130 dict_obj. set_item ( "codec" , cid. codec ( ) ) . unwrap ( ) ;
@@ -122,15 +121,15 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
122121
123122 let major = decode:: read_major ( r) ?;
124123 Ok ( match major. kind ( ) {
125- MajorKind :: UnsignedInt => decode:: read_uint ( r, major) ?. to_object ( py) ,
126- MajorKind :: NegativeInt => ( -1 - decode:: read_uint ( r, major) ? as i64 ) . to_object ( py) ,
124+ MajorKind :: UnsignedInt => decode:: read_uint ( r, major) ?. into_pyobject ( py) ? . into ( ) ,
125+ MajorKind :: NegativeInt => ( -1 - decode:: read_uint ( r, major) ? as i64 ) . into_pyobject ( py) ? . into ( ) ,
127126 MajorKind :: ByteString => {
128127 let len = decode:: read_uint ( r, major) ?;
129- PyBytes :: new_bound ( py, & decode:: read_bytes ( r, len) ?) . to_object ( py)
128+ PyBytes :: new ( py, & decode:: read_bytes ( r, len) ?) . into_pyobject ( py) ? . into ( )
130129 }
131130 MajorKind :: TextString => {
132131 let len = decode:: read_uint ( r, major) ?;
133- string_new_bound ( py, & decode:: read_bytes ( r, len) ?) . to_object ( py)
132+ string_new_bound ( py, & decode:: read_bytes ( r, len) ?) . into_pyobject ( py) ? . into ( )
134133 }
135134 MajorKind :: Array => {
136135 let len: ffi:: Py_ssize_t = decode_len ( decode:: read_uint ( r, major) ?) ?. try_into ( ) ?;
@@ -143,12 +142,12 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
143142 }
144143
145144 let list: Bound < ' _ , PyList > = Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( ) ;
146- list. to_object ( py)
145+ list. into_pyobject ( py) ? . into ( )
147146 }
148147 }
149148 MajorKind :: Map => {
150149 let len = decode_len ( decode:: read_uint ( r, major) ?) ?;
151- let dict = PyDict :: new_bound ( py) ;
150+ let dict = PyDict :: new ( py) ;
152151
153152 let mut prev_key: Option < Vec < u8 > > = None ;
154153 for _ in 0 ..len {
@@ -168,14 +167,14 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
168167 }
169168 }
170169
171- let key_py = string_new_bound ( py, key. as_slice ( ) ) . to_object ( py) ;
170+ let key_py = string_new_bound ( py, key. as_slice ( ) ) . into_pyobject ( py) ? ;
172171 prev_key = Some ( key) ;
173172
174173 let value_py = decode_dag_cbor_to_pyobject ( py, r, depth + 1 ) ?;
175174 dict. set_item ( key_py, value_py) ?;
176175 }
177176
178- dict. to_object ( py)
177+ dict. into_pyobject ( py) ? . into ( )
179178 }
180179 MajorKind :: Tag => {
181180 let value = decode:: read_uint ( r, major) ?;
@@ -185,14 +184,15 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
185184
186185 // FIXME(MarshalX): to_bytes allocates
187186 let cid = decode:: read_link ( r) ?. to_bytes ( ) ;
188- PyBytes :: new_bound ( py, & cid) . to_object ( py)
187+ PyBytes :: new ( py, & cid) . into_pyobject ( py) ? . into ( )
189188 }
190189 MajorKind :: Other => match major {
191- cbor:: FALSE => false . to_object ( py) ,
192- cbor:: TRUE => true . to_object ( py) ,
190+ // FIXME(MarshalX): should be more clear for bool?
191+ cbor:: FALSE => false . into_pyobject ( py) ?. into_any ( ) . unbind ( ) ,
192+ cbor:: TRUE => true . into_pyobject ( py) ?. into_any ( ) . unbind ( ) ,
193193 cbor:: NULL => py. None ( ) ,
194- cbor:: F32 => decode:: read_f32 ( r) ?. to_object ( py) ,
195- cbor:: F64 => decode:: read_f64 ( r) ?. to_object ( py) ,
194+ cbor:: F32 => decode:: read_f32 ( r) ?. into_pyobject ( py) ? . into ( ) ,
195+ cbor:: F64 => decode:: read_f64 ( r) ?. into_pyobject ( py) ? . into ( ) ,
196196 _ => return Err ( anyhow ! ( "Unsupported major type" . to_string( ) ) ) ,
197197 } ,
198198 } )
@@ -311,7 +311,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(
311311#[ pyfunction]
312312fn decode_dag_cbor_multi < ' py > ( py : Python < ' py > , data : & [ u8 ] ) -> PyResult < Bound < ' py , PyList > > {
313313 let mut reader = BufReader :: new ( Cursor :: new ( data) ) ;
314- let decoded_parts = PyList :: empty_bound ( py) ;
314+ let decoded_parts = PyList :: empty ( py) ;
315315
316316 loop {
317317 let py_object = decode_dag_cbor_to_pyobject ( py, & mut reader, 0 ) ;
@@ -420,7 +420,7 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(PyObject, Boun
420420
421421 // FIXME (MarshalX): we are not verifying if the roots are valid CIDs
422422
423- let parsed_blocks = PyDict :: new_bound ( py) ;
423+ let parsed_blocks = PyDict :: new ( py) ;
424424
425425 loop {
426426 if let Err ( _) = read_u64_leb128 ( buf) {
@@ -452,7 +452,7 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(PyObject, Boun
452452 } ;
453453
454454 // FIXME(MarshalX): to_bytes allocates
455- let key = PyBytes :: new_bound ( py, & cid. to_bytes ( ) ) . to_object ( py) ;
455+ let key = PyBytes :: new ( py, & cid. to_bytes ( ) ) . into_pyobject ( py) ? ;
456456 parsed_blocks. set_item ( key, block) ?;
457457 }
458458
@@ -494,7 +494,7 @@ pub fn encode_dag_cbor<'py>(
494494 if let Err ( e) = buf. flush ( ) {
495495 return Err ( get_err ( "Failed to flush buffer" , e. to_string ( ) ) ) ;
496496 }
497- Ok ( PyBytes :: new_bound ( py, & buf. get_ref ( ) ) )
497+ Ok ( PyBytes :: new ( py, & buf. get_ref ( ) ) )
498498}
499499
500500fn get_cid_from_py_any < ' py > ( data : & Bound < PyAny > ) -> PyResult < Cid > {
@@ -522,14 +522,14 @@ fn decode_cid<'py>(py: Python<'py>, data: &Bound<PyAny>) -> PyResult<Bound<'py,
522522
523523#[ pyfunction]
524524fn encode_cid < ' py > ( py : Python < ' py > , data : & Bound < PyAny > ) -> PyResult < Bound < ' py , PyString > > {
525- Ok ( PyString :: new_bound ( py, get_cid_from_py_any ( data) ?. to_string ( ) . as_str ( ) ) )
525+ Ok ( PyString :: new ( py, get_cid_from_py_any ( data) ?. to_string ( ) . as_str ( ) ) )
526526}
527527
528528#[ pyfunction]
529529fn decode_multibase < ' py > ( py : Python < ' py > , data : & str ) -> PyResult < ( char , Bound < ' py , PyBytes > ) > {
530530 let base = multibase:: decode ( data) ;
531531 if let Ok ( ( base, data) ) = base {
532- Ok ( ( base. code ( ) , PyBytes :: new_bound ( py, & data) ) )
532+ Ok ( ( base. code ( ) , PyBytes :: new ( py, & data) ) )
533533 } else {
534534 Err ( get_err (
535535 "Failed to decode multibase" ,
@@ -558,17 +558,17 @@ fn get_err(msg: &str, err: String) -> PyErr {
558558
559559#[ pymodule]
560560fn libipld ( m : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
561- m. add_function ( wrap_pyfunction_bound ! ( decode_cid, m) ?) ?;
562- m. add_function ( wrap_pyfunction_bound ! ( encode_cid, m) ?) ?;
561+ m. add_function ( wrap_pyfunction ! ( decode_cid, m) ?) ?;
562+ m. add_function ( wrap_pyfunction ! ( encode_cid, m) ?) ?;
563563
564- m. add_function ( wrap_pyfunction_bound ! ( decode_car, m) ?) ?;
564+ m. add_function ( wrap_pyfunction ! ( decode_car, m) ?) ?;
565565
566- m. add_function ( wrap_pyfunction_bound ! ( decode_dag_cbor, m) ?) ?;
567- m. add_function ( wrap_pyfunction_bound ! ( decode_dag_cbor_multi, m) ?) ?;
568- m. add_function ( wrap_pyfunction_bound ! ( encode_dag_cbor, m) ?) ?;
566+ m. add_function ( wrap_pyfunction ! ( decode_dag_cbor, m) ?) ?;
567+ m. add_function ( wrap_pyfunction ! ( decode_dag_cbor_multi, m) ?) ?;
568+ m. add_function ( wrap_pyfunction ! ( encode_dag_cbor, m) ?) ?;
569569
570- m. add_function ( wrap_pyfunction_bound ! ( decode_multibase, m) ?) ?;
571- m. add_function ( wrap_pyfunction_bound ! ( encode_multibase, m) ?) ?;
570+ m. add_function ( wrap_pyfunction ! ( decode_multibase, m) ?) ?;
571+ m. add_function ( wrap_pyfunction ! ( encode_multibase, m) ?) ?;
572572
573573 Ok ( ( ) )
574574}
0 commit comments