@@ -53,6 +53,12 @@ impl PyStringIORef {
53
53
fn getvalue ( self , _vm : & VirtualMachine ) -> String {
54
54
self . data . borrow ( ) . clone ( )
55
55
}
56
+
57
+ fn read ( self , _vm : & VirtualMachine ) -> String {
58
+ let data = self . data . borrow ( ) . clone ( ) ;
59
+ self . data . borrow_mut ( ) . clear ( ) ;
60
+ data
61
+ }
56
62
}
57
63
58
64
fn string_io_new ( cls : PyClassRef , vm : & VirtualMachine ) -> PyResult < PyStringIORef > {
@@ -62,15 +68,41 @@ fn string_io_new(cls: PyClassRef, vm: &VirtualMachine) -> PyResult<PyStringIORef
62
68
. into_ref_with_type ( vm, cls)
63
69
}
64
70
65
- fn bytes_io_init ( vm : & VirtualMachine , _args : PyFuncArgs ) -> PyResult {
66
- // TODO
67
- Ok ( vm . get_none ( ) )
71
+ # [ derive ( Debug , Default , Clone ) ]
72
+ struct PyBytesIO {
73
+ data : RefCell < Vec < u8 > > ,
68
74
}
69
75
70
- fn bytes_io_getvalue ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
71
- arg_check ! ( vm, args) ;
72
- // TODO
73
- Ok ( vm. get_none ( ) )
76
+ type PyBytesIORef = PyRef < PyBytesIO > ;
77
+
78
+ impl PyValue for PyBytesIO {
79
+ fn class ( vm : & VirtualMachine ) -> PyClassRef {
80
+ vm. class ( "io" , "BytesIO" )
81
+ }
82
+ }
83
+
84
+ impl PyBytesIORef {
85
+ fn write ( self , data : objbytes:: PyBytesRef , _vm : & VirtualMachine ) {
86
+ let data = data. get_value ( ) ;
87
+ self . data . borrow_mut ( ) . extend ( data) ;
88
+ }
89
+
90
+ fn getvalue ( self , vm : & VirtualMachine ) -> PyResult {
91
+ Ok ( vm. ctx . new_bytes ( self . data . borrow ( ) . clone ( ) ) )
92
+ }
93
+
94
+ fn read ( self , vm : & VirtualMachine ) -> PyResult {
95
+ let data = self . data . borrow ( ) . clone ( ) ;
96
+ self . data . borrow_mut ( ) . clear ( ) ;
97
+ Ok ( vm. ctx . new_bytes ( data) )
98
+ }
99
+ }
100
+
101
+ fn bytes_io_new ( cls : PyClassRef , vm : & VirtualMachine ) -> PyResult < PyBytesIORef > {
102
+ PyBytesIO {
103
+ data : RefCell :: new ( Vec :: new ( ) ) ,
104
+ }
105
+ . into_ref_with_type ( vm, cls)
74
106
}
75
107
76
108
fn io_base_cm_enter ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
@@ -420,9 +452,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
420
452
// IOBase Subclasses
421
453
let raw_io_base = py_class ! ( ctx, "RawIOBase" , io_base. clone( ) , { } ) ;
422
454
423
- let buffered_io_base = py_class ! ( ctx, "BufferedIOBase" , io_base. clone( ) , {
424
- "__init__" => ctx. new_rustfunc( buffered_io_base_init)
425
- } ) ;
455
+ let buffered_io_base = py_class ! ( ctx, "BufferedIOBase" , io_base. clone( ) , { } ) ;
426
456
427
457
//TextIO Base has no public constructor
428
458
let text_io_base = py_class ! ( ctx, "TextIOBase" , io_base. clone( ) , {
@@ -441,10 +471,18 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
441
471
442
472
// BufferedIOBase Subclasses
443
473
let buffered_reader = py_class ! ( ctx, "BufferedReader" , buffered_io_base. clone( ) , {
474
+ //workaround till the buffered classes can be fixed up to be more
475
+ //consistent with the python model
476
+ //For more info see: https://github.com/RustPython/RustPython/issues/547
477
+ "__init__" => ctx. new_rustfunc( buffered_io_base_init) ,
444
478
"read" => ctx. new_rustfunc( buffered_reader_read)
445
479
} ) ;
446
480
447
481
let buffered_writer = py_class ! ( ctx, "BufferedWriter" , buffered_io_base. clone( ) , {
482
+ //workaround till the buffered classes can be fixed up to be more
483
+ //consistent with the python model
484
+ //For more info see: https://github.com/RustPython/RustPython/issues/547
485
+ "__init__" => ctx. new_rustfunc( buffered_io_base_init) ,
448
486
"write" => ctx. new_rustfunc( buffered_writer_write)
449
487
} ) ;
450
488
@@ -456,14 +494,17 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
456
494
//StringIO: in-memory text
457
495
let string_io = py_class ! ( ctx, "StringIO" , text_io_base. clone( ) , {
458
496
"__new__" => ctx. new_rustfunc( string_io_new) ,
497
+ "read" => ctx. new_rustfunc( PyStringIORef :: read) ,
459
498
"write" => ctx. new_rustfunc( PyStringIORef :: write) ,
460
499
"getvalue" => ctx. new_rustfunc( PyStringIORef :: getvalue)
461
500
} ) ;
462
501
463
502
//BytesIO: in-memory bytes
464
503
let bytes_io = py_class ! ( ctx, "BytesIO" , buffered_io_base. clone( ) , {
465
- "__init__" => ctx. new_rustfunc( bytes_io_init) ,
466
- "getvalue" => ctx. new_rustfunc( bytes_io_getvalue)
504
+ "__new__" => ctx. new_rustfunc( bytes_io_new) ,
505
+ "read" => ctx. new_rustfunc( PyBytesIORef :: read) ,
506
+ "write" => ctx. new_rustfunc( PyBytesIORef :: write) ,
507
+ "getvalue" => ctx. new_rustfunc( PyBytesIORef :: getvalue)
467
508
} ) ;
468
509
469
510
py_module ! ( vm, "io" , {
0 commit comments