@@ -7,25 +7,24 @@ use ::libipld::cid::{Cid, Error as CidError, Result as CidResult, Version};
7
7
use anyhow:: { anyhow, Result } ;
8
8
use byteorder:: { BigEndian , ByteOrder } ;
9
9
use multihash:: Multihash ;
10
- use pyo3:: { ffi, prelude:: * , types:: * , PyObject , Python } ;
11
- use pyo3:: conversion:: ToPyObject ;
10
+ use pyo3:: { ffi, prelude:: * , types:: * , BoundObject , PyObject , Python } ;
12
11
use pyo3:: pybacked:: PyBackedStr ;
13
12
14
13
fn cid_hash_to_pydict < ' py > ( py : Python < ' py > , cid : & Cid ) -> Bound < ' py , PyDict > {
15
14
let hash = cid. hash ( ) ;
16
- let dict_obj = PyDict :: new_bound ( py) ;
15
+ let dict_obj = PyDict :: new ( py) ;
17
16
18
17
dict_obj. set_item ( "code" , hash. code ( ) ) . unwrap ( ) ;
19
18
dict_obj. set_item ( "size" , hash. size ( ) ) . unwrap ( ) ;
20
19
dict_obj
21
- . set_item ( "digest" , PyBytes :: new_bound ( py, & hash. digest ( ) ) )
20
+ . set_item ( "digest" , PyBytes :: new ( py, & hash. digest ( ) ) )
22
21
. unwrap ( ) ;
23
22
24
23
dict_obj
25
24
}
26
25
27
26
fn 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) ;
29
28
30
29
dict_obj. set_item ( "version" , cid. version ( ) as u64 ) . unwrap ( ) ;
31
30
dict_obj. set_item ( "codec" , cid. codec ( ) ) . unwrap ( ) ;
@@ -122,15 +121,15 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
122
121
123
122
let major = decode:: read_major ( r) ?;
124
123
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 ( ) ,
127
126
MajorKind :: ByteString => {
128
127
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 ( )
130
129
}
131
130
MajorKind :: TextString => {
132
131
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 ( )
134
133
}
135
134
MajorKind :: Array => {
136
135
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>(
143
142
}
144
143
145
144
let list: Bound < ' _ , PyList > = Bound :: from_owned_ptr ( py, ptr) . downcast_into_unchecked ( ) ;
146
- list. to_object ( py)
145
+ list. into_pyobject ( py) ? . into ( )
147
146
}
148
147
}
149
148
MajorKind :: Map => {
150
149
let len = decode_len ( decode:: read_uint ( r, major) ?) ?;
151
- let dict = PyDict :: new_bound ( py) ;
150
+ let dict = PyDict :: new ( py) ;
152
151
153
152
let mut prev_key: Option < Vec < u8 > > = None ;
154
153
for _ in 0 ..len {
@@ -168,14 +167,14 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
168
167
}
169
168
}
170
169
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) ? ;
172
171
prev_key = Some ( key) ;
173
172
174
173
let value_py = decode_dag_cbor_to_pyobject ( py, r, depth + 1 ) ?;
175
174
dict. set_item ( key_py, value_py) ?;
176
175
}
177
176
178
- dict. to_object ( py)
177
+ dict. into_pyobject ( py) ? . into ( )
179
178
}
180
179
MajorKind :: Tag => {
181
180
let value = decode:: read_uint ( r, major) ?;
@@ -185,14 +184,15 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
185
184
186
185
// FIXME(MarshalX): to_bytes allocates
187
186
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 ( )
189
188
}
190
189
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 ( ) ,
193
193
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 ( ) ,
196
196
_ => return Err ( anyhow ! ( "Unsupported major type" . to_string( ) ) ) ,
197
197
} ,
198
198
} )
@@ -311,7 +311,7 @@ fn encode_dag_cbor_from_pyobject<'py, W: Write>(
311
311
#[ pyfunction]
312
312
fn decode_dag_cbor_multi < ' py > ( py : Python < ' py > , data : & [ u8 ] ) -> PyResult < Bound < ' py , PyList > > {
313
313
let mut reader = BufReader :: new ( Cursor :: new ( data) ) ;
314
- let decoded_parts = PyList :: empty_bound ( py) ;
314
+ let decoded_parts = PyList :: empty ( py) ;
315
315
316
316
loop {
317
317
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
420
420
421
421
// FIXME (MarshalX): we are not verifying if the roots are valid CIDs
422
422
423
- let parsed_blocks = PyDict :: new_bound ( py) ;
423
+ let parsed_blocks = PyDict :: new ( py) ;
424
424
425
425
loop {
426
426
if let Err ( _) = read_u64_leb128 ( buf) {
@@ -452,7 +452,7 @@ pub fn decode_car<'py>(py: Python<'py>, data: &[u8]) -> PyResult<(PyObject, Boun
452
452
} ;
453
453
454
454
// 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) ? ;
456
456
parsed_blocks. set_item ( key, block) ?;
457
457
}
458
458
@@ -494,7 +494,7 @@ pub fn encode_dag_cbor<'py>(
494
494
if let Err ( e) = buf. flush ( ) {
495
495
return Err ( get_err ( "Failed to flush buffer" , e. to_string ( ) ) ) ;
496
496
}
497
- Ok ( PyBytes :: new_bound ( py, & buf. get_ref ( ) ) )
497
+ Ok ( PyBytes :: new ( py, & buf. get_ref ( ) ) )
498
498
}
499
499
500
500
fn 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,
522
522
523
523
#[ pyfunction]
524
524
fn 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 ( ) ) )
526
526
}
527
527
528
528
#[ pyfunction]
529
529
fn decode_multibase < ' py > ( py : Python < ' py > , data : & str ) -> PyResult < ( char , Bound < ' py , PyBytes > ) > {
530
530
let base = multibase:: decode ( data) ;
531
531
if let Ok ( ( base, data) ) = base {
532
- Ok ( ( base. code ( ) , PyBytes :: new_bound ( py, & data) ) )
532
+ Ok ( ( base. code ( ) , PyBytes :: new ( py, & data) ) )
533
533
} else {
534
534
Err ( get_err (
535
535
"Failed to decode multibase" ,
@@ -558,17 +558,17 @@ fn get_err(msg: &str, err: String) -> PyErr {
558
558
559
559
#[ pymodule]
560
560
fn 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) ?) ?;
563
563
564
- m. add_function ( wrap_pyfunction_bound ! ( decode_car, m) ?) ?;
564
+ m. add_function ( wrap_pyfunction ! ( decode_car, m) ?) ?;
565
565
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) ?) ?;
569
569
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) ?) ?;
572
572
573
573
Ok ( ( ) )
574
574
}
0 commit comments