@@ -11,12 +11,12 @@ use num_bigint::ToBigInt;
11
11
use num_traits:: ToPrimitive ;
12
12
13
13
use super :: os;
14
- use crate :: function:: PyFuncArgs ;
14
+ use crate :: function:: { OptionalArg , PyFuncArgs } ;
15
15
use crate :: import;
16
16
use crate :: obj:: objbytearray:: PyByteArray ;
17
17
use crate :: obj:: objbytes;
18
18
use crate :: obj:: objint;
19
- use crate :: obj:: objstr;
19
+ use crate :: obj:: objstr:: { get_value , PyStringRef } ;
20
20
use crate :: obj:: objtype:: PyClassRef ;
21
21
use crate :: pyobject:: { BufferProtocol , PyObjectRef , PyRef , PyResult , PyValue } ;
22
22
use crate :: vm:: VirtualMachine ;
@@ -45,7 +45,7 @@ impl PyValue for PyStringIO {
45
45
}
46
46
47
47
impl PyStringIORef {
48
- fn write ( self , data : objstr :: PyStringRef , _vm : & VirtualMachine ) {
48
+ fn write ( self , data : PyStringRef , _vm : & VirtualMachine ) {
49
49
let data = data. value . clone ( ) ;
50
50
self . data . borrow_mut ( ) . push_str ( & data) ;
51
51
}
@@ -61,9 +61,18 @@ impl PyStringIORef {
61
61
}
62
62
}
63
63
64
- fn string_io_new ( cls : PyClassRef , vm : & VirtualMachine ) -> PyResult < PyStringIORef > {
64
+ fn string_io_new (
65
+ cls : PyClassRef ,
66
+ object : OptionalArg < PyObjectRef > ,
67
+ vm : & VirtualMachine ,
68
+ ) -> PyResult < PyStringIORef > {
69
+ let raw_string = match object {
70
+ OptionalArg :: Present ( ref input) => get_value ( input) ,
71
+ OptionalArg :: Missing => String :: new ( ) ,
72
+ } ;
73
+
65
74
PyStringIO {
66
- data : RefCell :: new ( String :: default ( ) ) ,
75
+ data : RefCell :: new ( raw_string ) ,
67
76
}
68
77
. into_ref_with_type ( vm, cls)
69
78
}
@@ -98,9 +107,18 @@ impl PyBytesIORef {
98
107
}
99
108
}
100
109
101
- fn bytes_io_new ( cls : PyClassRef , vm : & VirtualMachine ) -> PyResult < PyBytesIORef > {
110
+ fn bytes_io_new (
111
+ cls : PyClassRef ,
112
+ object : OptionalArg < PyObjectRef > ,
113
+ vm : & VirtualMachine ,
114
+ ) -> PyResult < PyBytesIORef > {
115
+ let raw_bytes = match object {
116
+ OptionalArg :: Present ( ref input) => objbytes:: get_value ( input) . to_vec ( ) ,
117
+ OptionalArg :: Missing => vec ! [ ] ,
118
+ } ;
119
+
102
120
PyBytesIO {
103
- data : RefCell :: new ( Vec :: new ( ) ) ,
121
+ data : RefCell :: new ( raw_bytes ) ,
104
122
}
105
123
. into_ref_with_type ( vm, cls)
106
124
}
@@ -172,7 +190,7 @@ fn file_io_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
172
190
optional = [ ( mode, Some ( vm. ctx. str_type( ) ) ) ]
173
191
) ;
174
192
175
- let rust_mode = mode. map_or ( "r" . to_string ( ) , |m| objstr :: get_value ( m) ) ;
193
+ let rust_mode = mode. map_or ( "r" . to_string ( ) , |m| get_value ( m) ) ;
176
194
177
195
match compute_c_flag ( & rust_mode) . to_bigint ( ) {
178
196
Some ( os_mode) => {
@@ -193,7 +211,7 @@ fn file_io_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
193
211
fn file_io_read ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
194
212
arg_check ! ( vm, args, required = [ ( file_io, None ) ] ) ;
195
213
let py_name = vm. get_attribute ( file_io. clone ( ) , "name" ) ?;
196
- let f = match File :: open ( objstr :: get_value ( & py_name) ) {
214
+ let f = match File :: open ( get_value ( & py_name) ) {
197
215
Ok ( v) => Ok ( v) ,
198
216
Err ( _) => Err ( vm. new_type_error ( "Error opening file" . to_string ( ) ) ) ,
199
217
} ;
@@ -389,7 +407,7 @@ pub fn io_open(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
389
407
optional = [ ( mode, Some ( vm. ctx. str_type( ) ) ) ]
390
408
) ;
391
409
// mode is optional: 'rt' is the default mode (open from reading text)
392
- let mode_string = mode. map_or ( "rt" . to_string ( ) , |m| objstr :: get_value ( m) ) ;
410
+ let mode_string = mode. map_or ( "rt" . to_string ( ) , |m| get_value ( m) ) ;
393
411
let ( mode, typ) = match split_mode_string ( mode_string) {
394
412
Ok ( ( mode, typ) ) => ( mode, typ) ,
395
413
Err ( error_message) => {
0 commit comments