@@ -102,11 +102,13 @@ fn get_bytes_from_py_any<'py>(obj: &'py Bound<'py, PyAny>) -> PyResult<&'py [u8]
102102 }
103103}
104104
105- fn string_new_bound < ' py > ( py : Python < ' py > , s : & [ u8 ] ) -> Bound < ' py , PyString > {
105+ fn string_new_bound < ' py > ( py : Python < ' py > , s : & [ u8 ] ) -> Result < Bound < ' py , PyString > > {
106+ std:: str:: from_utf8 ( s) . map_err ( |e| anyhow ! ( "Invalid UTF-8 string: {}" , e) ) ?;
107+
106108 let ptr = s. as_ptr ( ) as * const c_char ;
107109 let len = s. len ( ) as ffi:: Py_ssize_t ;
108110 unsafe {
109- Bound :: from_owned_ptr ( py, ffi:: PyUnicode_FromStringAndSize ( ptr, len) ) . downcast_into_unchecked ( )
111+ Ok ( Bound :: from_owned_ptr ( py, ffi:: PyUnicode_FromStringAndSize ( ptr, len) ) . downcast_into_unchecked ( ) )
110112 }
111113}
112114
@@ -135,7 +137,7 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
135137 }
136138 MajorKind :: TextString => {
137139 let len = decode:: read_uint ( r, major) ?;
138- string_new_bound ( py, & decode:: read_bytes ( r, len) ?) . into_pyobject ( py) ?. into ( )
140+ string_new_bound ( py, & decode:: read_bytes ( r, len) ?) ? . into_pyobject ( py) ?. into ( )
139141 }
140142 MajorKind :: Array => {
141143 let len: ffi:: Py_ssize_t = decode_len ( decode:: read_uint ( r, major) ?) ?. try_into ( ) ?;
@@ -173,7 +175,7 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
173175 }
174176 }
175177
176- let key_py = string_new_bound ( py, key. as_slice ( ) ) . into_pyobject ( py) ?;
178+ let key_py = string_new_bound ( py, key. as_slice ( ) ) ? . into_pyobject ( py) ?;
177179 prev_key = Some ( key) ;
178180
179181 let value_py = decode_dag_cbor_to_pyobject ( py, r, depth + 1 ) ?;
0 commit comments