11use super :: super :: pyobject:: {
2- AttributeProtocol , PyContext , PyFuncArgs , PyObjectKind , PyObjectRef , PyResult , TypeProtocol ,
2+ AttributeProtocol , PyContext , PyFuncArgs , PyObject , PyObjectKind , PyObjectRef , PyResult ,
3+ TypeProtocol ,
34} ;
45use super :: super :: vm:: VirtualMachine ;
56use super :: objint;
6- use super :: objlist;
77use super :: objtype;
88use num_traits:: ToPrimitive ;
9+ use std:: cell:: Ref ;
10+ use std:: ops:: Deref ;
911// Binary data support
1012
1113// Fill bytes class methods:
1214pub fn init ( context : & PyContext ) {
1315let ref bytes_type = context. bytes_type ;
1416 bytes_type. set_attr ( "__eq__" , context. new_rustfunc ( bytes_eq) ) ;
15- bytes_type. set_attr ( "__init__ " , context. new_rustfunc ( bytes_init ) ) ;
17+ bytes_type. set_attr ( "__new__ " , context. new_rustfunc ( bytes_new ) ) ;
1618 bytes_type. set_attr ( "__repr__" , context. new_rustfunc ( bytes_repr) ) ;
1719}
1820
19- // __init__ (store value into objectkind)
20- fn bytes_init ( vm : & mut VirtualMachine , args : PyFuncArgs ) ->PyResult {
21+ fn bytes_new ( vm : & mut VirtualMachine , args : PyFuncArgs ) ->PyResult {
2122arg_check ! (
2223 vm,
2324 args,
24- required =[ ( zelf, Some ( vm. ctx. bytes_type( ) ) ) , ( arg, None ) ]
25+ required =[ ( cls, None ) ] ,
26+ optional =[ ( val_option, None ) ]
2527) ;
26- let val =if objtype:: isinstance ( arg, & vm. ctx . list_type ( ) ) {
28+ if !objtype:: issubclass ( cls, & vm. ctx . bytes_type ( ) ) {
29+ return Err ( vm. new_type_error ( format ! ( "{:?} is not a subtype of bytes" , cls) ) ) ;
30+ }
31+
32+ // Create bytes data:
33+ let value =if let Some ( ival) = val_option{
34+ let elements = vm. extract_elements ( ival) ?;
2735let mut data_bytes =vec ! [ ] ;
28- for elemin objlist :: get_elements ( arg ) . iter ( ) {
36+ for elemin elements . iter ( ) {
2937let v = objint:: to_int ( vm, elem, 10 ) ?;
3038 data_bytes. push ( v. to_u8 ( ) . unwrap ( ) ) ;
3139}
3240 data_bytes
41+ // return Err(vm.new_type_error("Cannot construct bytes".to_string()));
3342} else {
34- return Err ( vm . new_type_error ( "Cannot construct bytes" . to_string ( ) ) ) ;
43+ vec ! [ ]
3544} ;
36- set_value ( zelf, val) ;
37- Ok ( vm. get_none ( ) )
45+
46+ Ok ( PyObject :: new (
47+ PyObjectKind :: Bytes { value : value} ,
48+ cls. clone ( ) ,
49+ ) )
3850}
3951
4052fn bytes_eq ( vm : & mut VirtualMachine , args : PyFuncArgs ) ->PyResult {
@@ -45,29 +57,27 @@ fn bytes_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
4557) ;
4658
4759let result =if objtype:: isinstance ( b, & vm. ctx . bytes_type ( ) ) {
48- get_value ( a) ==get_value ( b)
60+ get_value ( a) . to_vec ( ) ==get_value ( b) . to_vec ( )
4961} else {
5062false
5163} ;
5264Ok ( vm. ctx . new_bool ( result) )
5365}
5466
55- pub fn get_value ( obj : & PyObjectRef ) ->Vec < u8 > {
56- if let PyObjectKind :: Bytes { value} =& obj. borrow ( ) . kind {
57- value. clone ( )
58- } else {
59- panic ! ( "Inner error getting int {:?}" , obj) ;
60- }
61- }
62-
63- fn set_value ( obj : & PyObjectRef , value : Vec < u8 > ) {
64- obj. borrow_mut ( ) . kind =PyObjectKind :: Bytes { value} ;
67+ pub fn get_value < ' a > ( obj : & ' a PyObjectRef ) ->impl Deref < Target =Vec < u8 > > +' a {
68+ Ref :: map ( obj. borrow ( ) , |py_obj|{
69+ if let PyObjectKind :: Bytes { ref value} = py_obj. kind {
70+ value
71+ } else {
72+ panic ! ( "Inner error getting int {:?}" , obj) ;
73+ }
74+ } )
6575}
6676
6777fn bytes_repr ( vm : & mut VirtualMachine , args : PyFuncArgs ) ->PyResult {
6878arg_check ! ( vm, args, required =[ ( obj, Some ( vm. ctx. bytes_type( ) ) ) ] ) ;
6979let data =get_value ( obj) ;
70- let data: Vec < String > = data. into_iter ( ) . map ( |b|format ! ( "\\ x{:02x}" , b) ) . collect ( ) ;
80+ let data: Vec < String > = data. iter ( ) . map ( |b|format ! ( "\\ x{:02x}" , b) ) . collect ( ) ;
7181let data = data. join ( "" ) ;
7282Ok ( vm. new_str ( format ! ( "b'{}'" , data) ) )
7383}