@@ -10,7 +10,7 @@ use crate::types::PyBytes;
10
10
use crate :: IntoPy ;
11
11
use crate :: { ffi, Bound , Py , PyAny , PyResult , Python } ;
12
12
use std:: borrow:: Cow ;
13
- use std:: ffi:: CString ;
13
+ use std:: ffi:: { CStr , CString } ;
14
14
use std:: str;
15
15
16
16
/// Deprecated alias for [`PyString`].
@@ -212,32 +212,48 @@ impl PyString {
212
212
/// Attempts to create a Python string from a Python [bytes-like object].
213
213
///
214
214
/// [bytes-like object]: (https://docs.python.org/3/glossary.html#term-bytes-like-object).
215
+ pub fn from_encoded_object < ' py > (
216
+ src : & Bound < ' py , PyAny > ,
217
+ encoding : & CStr ,
218
+ errors : & CStr ,
219
+ ) -> PyResult < Bound < ' py , PyString > > {
220
+ unsafe {
221
+ ffi:: PyUnicode_FromEncodedObject ( src. as_ptr ( ) , encoding. as_ptr ( ) , errors. as_ptr ( ) )
222
+ . assume_owned_or_err ( src. py ( ) )
223
+ . downcast_into_unchecked ( )
224
+ }
225
+ }
226
+
227
+ /// Deprecated form of `PyString::from_encoded_object`.
228
+ ///
229
+ /// This version took `&str` arguments for `encoding` and `errors`, which required a runtime
230
+ /// conversion to `CString` internally.
231
+ #[ deprecated(
232
+ since = "0.24.1" ,
233
+ note = "replaced with to `PyString::from_encoded_object`"
234
+ ) ]
215
235
pub fn from_object < ' py > (
216
236
src : & Bound < ' py , PyAny > ,
217
237
encoding : & str ,
218
238
errors : & str ,
219
239
) -> PyResult < Bound < ' py , PyString > > {
220
240
let encoding = CString :: new ( encoding) ?;
221
241
let errors = CString :: new ( errors) ?;
222
- unsafe {
223
- ffi:: PyUnicode_FromEncodedObject (
224
- src. as_ptr ( ) ,
225
- encoding. as_ptr ( ) . cast ( ) ,
226
- errors. as_ptr ( ) . cast ( ) ,
227
- )
228
- . assume_owned_or_err ( src. py ( ) )
229
- . downcast_into_unchecked ( )
230
- }
242
+ PyString :: from_encoded_object ( src, & encoding, & errors)
231
243
}
232
244
233
245
/// Deprecated name for [`PyString::from_object`].
234
- #[ deprecated( since = "0.23.0" , note = "renamed to `PyString::from_object`" ) ]
246
+ #[ deprecated(
247
+ since = "0.23.0" ,
248
+ note = "replcaed with `PyString::from_encoded_object`"
249
+ ) ]
235
250
#[ inline]
236
251
pub fn from_object_bound < ' py > (
237
252
src : & Bound < ' py , PyAny > ,
238
253
encoding : & str ,
239
254
errors : & str ,
240
255
) -> PyResult < Bound < ' py , PyString > > {
256
+ #[ allow( deprecated) ]
241
257
Self :: from_object ( src, encoding, errors)
242
258
}
243
259
}
@@ -586,6 +602,8 @@ impl PartialEq<Borrowed<'_, '_, PyString>> for &'_ str {
586
602
587
603
#[ cfg( test) ]
588
604
mod tests {
605
+ use pyo3_ffi:: c_str;
606
+
589
607
use super :: * ;
590
608
use crate :: { IntoPyObject , PyObject } ;
591
609
@@ -678,6 +696,14 @@ mod tests {
678
696
Python :: with_gil ( |py| {
679
697
let py_bytes = PyBytes :: new ( py, b"ab\xFF cd" ) ;
680
698
699
+ let py_string =
700
+ PyString :: from_encoded_object ( & py_bytes, c_str ! ( "utf-8" ) , c_str ! ( "ignore" ) )
701
+ . unwrap ( ) ;
702
+
703
+ let result = py_string. to_cow ( ) . unwrap ( ) ;
704
+ assert_eq ! ( result, "abcd" ) ;
705
+
706
+ #[ allow( deprecated) ]
681
707
let py_string = PyString :: from_object ( & py_bytes, "utf-8" , "ignore" ) . unwrap ( ) ;
682
708
683
709
let result = py_string. to_cow ( ) . unwrap ( ) ;
@@ -686,13 +712,18 @@ mod tests {
686
712
}
687
713
688
714
#[ test]
689
- fn test_string_from_obect_with_invalid_encoding_errors ( ) {
715
+ fn test_string_from_object_with_invalid_encoding_errors ( ) {
690
716
Python :: with_gil ( |py| {
691
717
let py_bytes = PyBytes :: new ( py, b"abcd" ) ;
692
718
719
+ let result = PyString :: from_encoded_object ( & py_bytes, c_str ! ( "wat" ) , c_str ! ( "ignore" ) ) ;
720
+ assert ! ( result. is_err( ) ) ;
721
+
722
+ #[ allow( deprecated) ]
693
723
let result = PyString :: from_object ( & py_bytes, "utf\0 -8" , "ignore" ) ;
694
724
assert ! ( result. is_err( ) ) ;
695
725
726
+ #[ allow( deprecated) ]
696
727
let result = PyString :: from_object ( & py_bytes, "utf-8" , "ign\0 ore" ) ;
697
728
assert ! ( result. is_err( ) ) ;
698
729
} ) ;
0 commit comments