@@ -49,20 +49,58 @@ fn bytes_io_getvalue(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
49
49
}
50
50
51
51
fn buffered_io_base_init ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
52
- arg_check ! ( vm, args) ;
53
-
54
- // TODO
52
+ arg_check ! (
53
+ vm,
54
+ args,
55
+ required = [ ( buffered, None ) , ( raw, None ) ]
56
+ ) ;
57
+ vm. ctx . set_attr ( & buffered, "raw" , raw. clone ( ) ) ;
55
58
Ok ( vm. get_none ( ) )
56
59
}
57
60
61
+ fn buffered_io_base_read ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
62
+ arg_check ! (
63
+ vm,
64
+ args,
65
+ required = [ ( buffered, None ) ]
66
+ ) ;
67
+ let buff_size = 8 ;
68
+ let mut buffer = vm. ctx . new_bytes ( vec ! [ 0 ; buff_size] ) ;
69
+
70
+ //buffer method
71
+ let mut result = vec ! [ ] ;
72
+ let mut length = buff_size;
73
+
74
+ let raw = vm. ctx . get_attr ( & buffered, "raw" ) . unwrap ( ) ;
75
+
76
+ while length == buff_size {
77
+ let raw_read = vm. get_method ( raw. clone ( ) , & "readinto" . to_string ( ) ) . unwrap ( ) ;
78
+ vm. invoke ( raw_read, PyFuncArgs :: new ( vec ! [ buffer. clone( ) ] , vec ! [ ] ) ) ;
79
+
80
+
81
+ match buffer. borrow_mut ( ) . kind {
82
+ PyObjectKind :: Bytes { ref mut value } => {
83
+ result. extend ( value. iter ( ) . cloned ( ) ) ;
84
+ } ,
85
+ _ => { }
86
+ } ;
87
+
88
+ let len = vm. get_method ( buffer. clone ( ) , & "__len__" . to_string ( ) ) ;
89
+ let py_len = vm. invoke ( len. unwrap ( ) , PyFuncArgs :: default ( ) ) ;
90
+ length = objint:: get_value ( & py_len. unwrap ( ) ) . to_usize ( ) . unwrap ( ) ;
91
+ }
92
+
93
+ Ok ( vm. ctx . new_bytes ( result) )
94
+ }
95
+
96
+
58
97
fn file_io_init ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
59
98
arg_check ! (
60
99
vm,
61
100
args,
62
101
required = [ ( file_io, None ) , ( name, Some ( vm. ctx. str_type( ) ) ) ] ,
63
102
optional = [ ( mode, Some ( vm. ctx. str_type( ) ) ) ]
64
- ) ; //mode is an optional string argument
65
- //if mode is NOT defined we redefine it as 'w'
103
+ ) ;
66
104
67
105
let mode = if let Some ( m) = mode {
68
106
objstr:: get_value ( m)
@@ -113,7 +151,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
113
151
arg_check ! (
114
152
vm,
115
153
args,
116
- required = [ ( file_io, None ) , ( obj, Some ( vm. ctx. bytearray_type ( ) ) ) ]
154
+ required = [ ( file_io, None ) , ( obj, Some ( vm. ctx. bytes_type ( ) ) ) ]
117
155
) ;
118
156
119
157
//extract length of buffer
@@ -131,11 +169,11 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
131
169
} ;
132
170
133
171
let mut f = handle. take ( length) ;
134
-
135
172
match obj. borrow_mut ( ) . kind {
136
173
PyObjectKind :: Bytes { ref mut value } => {
137
174
value. clear ( ) ;
138
175
f. read_to_end ( & mut * value) ;
176
+
139
177
} ,
140
178
_ => { }
141
179
} ;
@@ -164,7 +202,6 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
164
202
165
203
match obj. borrow_mut ( ) . kind {
166
204
PyObjectKind :: Bytes { ref mut value } => {
167
- println ! ( "Match!" ) ;
168
205
match handle. write ( & value[ ..] ) {
169
206
Ok ( k) => { println ! ( "{}" , k) ; } ,
170
207
Err ( _) => { }
@@ -176,6 +213,8 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
176
213
let len_method = vm. get_method ( obj. clone ( ) , & "__len__" . to_string ( ) ) ;
177
214
vm. invoke ( len_method. unwrap ( ) , PyFuncArgs :: default ( ) )
178
215
216
+ //TODO: reset fileno
217
+
179
218
}
180
219
181
220
fn buffered_reader_init ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -249,6 +288,7 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
249
288
250
289
let buffered_io_base = ctx. new_class ( "BufferedIOBase" , io_base. clone ( ) ) ;
251
290
ctx. set_attr ( & buffered_io_base, "__init__" , ctx. new_rustfunc ( buffered_io_base_init) ) ;
291
+ ctx. set_attr ( & buffered_io_base, "read" , ctx. new_rustfunc ( buffered_io_base_read) ) ;
252
292
ctx. set_attr ( & py_mod, "BufferedIOBase" , buffered_io_base. clone ( ) ) ;
253
293
254
294
let text_io_base = ctx. new_class ( "TextIOBase" , io_base. clone ( ) ) ;
@@ -267,7 +307,6 @@ pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
267
307
let buffered_reader = ctx. new_class ( "BufferedReader" , buffered_io_base. clone ( ) ) ;
268
308
ctx. set_attr ( & py_mod, "BufferedReader" , buffered_reader. clone ( ) ) ;
269
309
270
-
271
310
let buffered_reader = ctx. new_class ( "BufferedWriter" , buffered_io_base. clone ( ) ) ;
272
311
ctx. set_attr ( & py_mod, "BufferedWriter" , buffered_reader. clone ( ) ) ;
273
312
0 commit comments