@@ -237,11 +237,32 @@ impl VirtualMachine {
237
237
#[ cfg( not( target_arch = "wasm32" ) ) ]
238
238
import:: import_builtin ( self , "signal" ) ?;
239
239
240
- import:: init_importlib ( self , initialize_parameter) ?;
240
+ let io = {
241
+ let module_name = "_io" ;
242
+ let attrs = self . ctx . new_dict ( ) ;
243
+ let io_code = self . frozen . borrow ( ) . get ( module_name) . unwrap ( ) . code . clone ( ) ;
244
+
245
+ attrs. set_item ( "__name__" , self . new_str ( module_name. to_owned ( ) ) , self ) ?;
246
+
247
+ let module = self . new_module ( module_name, attrs. clone ( ) ) ;
248
+
249
+ // Store module in cache to prevent infinite loop with mutual importing libs:
250
+ let sys_modules = self . get_attribute ( self . sys_module . clone ( ) , "modules" ) ?;
251
+ sys_modules. set_item ( module_name, module. clone ( ) , self ) ?;
252
+
253
+ import:: init_importlib ( self , initialize_parameter) ?;
254
+
255
+ // Execute main code in module:
256
+ self . run_code_obj (
257
+ PyCode :: new ( io_code) . into_ref ( self ) ,
258
+ Scope :: with_builtins ( None , attrs, self ) ,
259
+ ) ?;
260
+
261
+ module
262
+ } ;
241
263
242
264
#[ cfg( not( target_arch = "wasm32" ) ) ]
243
265
{
244
- let io = self . import ( "io" , & [ ] , 0 ) ?;
245
266
let io_open = self . get_attribute ( io. clone ( ) , "open" ) ?;
246
267
let set_stdio = |name, fd, mode : & str | {
247
268
let stdio = self . invoke (
@@ -1082,6 +1103,12 @@ impl VirtualMachine {
1082
1103
self . call_codec_func ( "encode" , obj, encoding, errors)
1083
1104
}
1084
1105
1106
+ pub fn io_open ( & self , args : PyFuncArgs ) -> PyResult {
1107
+ let io = self . import ( "io" , & [ ] , 0 ) ?;
1108
+ let io_open = self . get_attribute ( io, "open" ) ?;
1109
+ self . invoke ( & io_open, args)
1110
+ }
1111
+
1085
1112
pub fn _sub ( & self , a : PyObjectRef , b : PyObjectRef ) -> PyResult {
1086
1113
self . call_or_reflection ( a, b, "__sub__" , "__rsub__" , |vm, a, b| {
1087
1114
Err ( vm. new_unsupported_operand_error ( a, b, "-" ) )
0 commit comments