@@ -3,8 +3,8 @@ use std::str::from_utf8;
3
3
use pyo3:: exceptions:: { PyAttributeError , PyTypeError } ;
4
4
use pyo3:: prelude:: * ;
5
5
use pyo3:: types:: {
6
- PyBool , PyBytes , PyDate , PyDateTime , PyDict , PyFrozenSet , PyInt , PyList , PyMapping , PySequence , PySet , PyString ,
7
- PyTime , PyTuple , PyType ,
6
+ PyBool , PyByteArray , PyBytes , PyDate , PyDateTime , PyDict , PyFrozenSet , PyInt , PyList , PyMapping , PySequence , PySet ,
7
+ PyString , PyTime , PyTuple , PyType ,
8
8
} ;
9
9
use pyo3:: { intern, AsPyPointer } ;
10
10
@@ -60,6 +60,12 @@ impl<'a> Input<'a> for PyAny {
60
60
Err ( _) => return Err ( ValError :: new ( ErrorKind :: StrUnicode , self ) ) ,
61
61
} ;
62
62
Ok ( str. into ( ) )
63
+ } else if let Ok ( py_byte_array) = self . cast_as :: < PyByteArray > ( ) {
64
+ let str = match from_utf8 ( unsafe { py_byte_array. as_bytes ( ) } ) {
65
+ Ok ( s) => s,
66
+ Err ( _) => return Err ( ValError :: new ( ErrorKind :: StrUnicode , self ) ) ,
67
+ } ;
68
+ Ok ( str. into ( ) )
63
69
} else if self . cast_as :: < PyBool > ( ) . is_ok ( ) {
64
70
// do this before int and float parsing as `False` is cast to `0` and we don't want False to
65
71
// be returned as a string
@@ -270,6 +276,8 @@ impl<'a> Input<'a> for PyAny {
270
276
} else if let Ok ( py_str) = self . cast_as :: < PyString > ( ) {
271
277
let string = py_str. to_string_lossy ( ) . to_string ( ) ;
272
278
Ok ( string. into_bytes ( ) . into ( ) )
279
+ } else if let Ok ( py_byte_array) = self . cast_as :: < PyByteArray > ( ) {
280
+ Ok ( py_byte_array. to_vec ( ) . into ( ) )
273
281
} else {
274
282
Err ( ValError :: new ( ErrorKind :: BytesType , self ) )
275
283
}
0 commit comments