@@ -10,15 +10,15 @@ use super::objbyteinner::{
10
10
use super :: objint:: PyIntRef ;
11
11
use super :: objiter;
12
12
use super :: objslice:: PySliceRef ;
13
- use super :: objstr:: PyStringRef ;
13
+ use super :: objstr:: { PyString , PyStringRef } ;
14
14
use super :: objtuple:: PyTupleRef ;
15
15
use super :: objtype:: PyClassRef ;
16
16
use crate :: cformat:: CFormatString ;
17
17
use crate :: function:: OptionalArg ;
18
18
use crate :: obj:: objstr:: do_cformat_string;
19
19
use crate :: pyobject:: {
20
20
Either , PyClassImpl , PyComparisonValue , PyContext , PyIterable , PyObjectRef , PyRef , PyResult ,
21
- PyValue , TryFromObject ,
21
+ PyValue , TryFromObject , TypeProtocol ,
22
22
} ;
23
23
use crate :: vm:: VirtualMachine ;
24
24
use std:: mem:: size_of;
@@ -590,6 +590,26 @@ impl PyByteArray {
590
590
self . inner . borrow_mut ( ) . elements . reverse ( ) ;
591
591
Ok ( ( ) )
592
592
}
593
+
594
+ #[ pymethod]
595
+ fn decode (
596
+ zelf : PyRef < Self > ,
597
+ encoding : OptionalArg < PyStringRef > ,
598
+ errors : OptionalArg < PyStringRef > ,
599
+ vm : & VirtualMachine ,
600
+ ) -> PyResult < PyStringRef > {
601
+ let encoding = encoding. into_option ( ) ;
602
+ vm. decode ( zelf. into_object ( ) , encoding. clone ( ) , errors. into_option ( ) ) ?
603
+ . downcast :: < PyString > ( )
604
+ . map_err ( |obj| {
605
+ vm. new_type_error ( format ! (
606
+ "'{}' decoder returned '{}' instead of 'str'; use codecs.encode() to \
607
+ encode arbitrary types",
608
+ encoding. as_ref( ) . map_or( "utf-8" , |s| s. as_str( ) ) ,
609
+ obj. class( ) . name,
610
+ ) )
611
+ } )
612
+ }
593
613
}
594
614
595
615
// fn set_value(obj: &PyObjectRef, value: Vec<u8>) {
0 commit comments