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