@@ -126,11 +126,10 @@ pub struct PyGlobalState {
126
126
127
127
pub const NSIG : usize = 64 ;
128
128
129
- #[ derive( Copy , Clone ) ]
129
+ #[ derive( Copy , Clone , PartialEq , Eq ) ]
130
130
pub enum InitParameter {
131
- NoInitialize ,
132
- InitializeInternal ,
133
- InitializeExternal ,
131
+ Internal ,
132
+ External ,
134
133
}
135
134
136
135
/// Struct containing all kind of settings for the python vm.
@@ -168,10 +167,6 @@ pub struct PySettings {
168
167
/// sys.argv
169
168
pub argv : Vec < String > ,
170
169
171
- /// Initialization parameter to decide to initialize or not,
172
- /// and to decide the importer required external filesystem access or not
173
- pub initialization_parameter : InitParameter ,
174
-
175
170
/// PYTHONHASHSEED=x
176
171
pub hash_seed : Option < u32 > ,
177
172
}
@@ -207,7 +202,6 @@ impl Default for PySettings {
207
202
dont_write_bytecode : false ,
208
203
path_list : vec ! [ ] ,
209
204
argv : vec ! [ ] ,
210
- initialization_parameter : InitParameter :: InitializeExternal ,
211
205
hash_seed : None ,
212
206
}
213
207
}
@@ -278,62 +272,55 @@ impl VirtualMachine {
278
272
vm
279
273
}
280
274
281
- pub fn initialize ( & mut self , initialize_parameter : InitParameter ) {
275
+ fn initialize ( & mut self , initialize_parameter : InitParameter ) {
282
276
flame_guard ! ( "init VirtualMachine" ) ;
283
277
284
- match initialize_parameter {
285
- InitParameter :: NoInitialize => { }
286
- _ => {
287
- if self . initialized {
288
- panic ! ( "Double Initialize Error" ) ;
289
- }
290
-
291
- builtins:: make_module ( self , self . builtins . clone ( ) ) ;
292
- sysmodule:: make_module ( self , self . sys_module . clone ( ) , self . builtins . clone ( ) ) ;
293
-
294
- let mut inner_init = || -> PyResult < ( ) > {
295
- #[ cfg( not( target_arch = "wasm32" ) ) ]
296
- import:: import_builtin ( self , "signal" ) ?;
297
-
298
- import:: init_importlib ( self , initialize_parameter) ?;
299
-
300
- #[ cfg( any( not( target_arch = "wasm32" ) , target_os = "wasi" ) ) ]
301
- {
302
- // this isn't fully compatible with CPython; it imports "io" and sets
303
- // builtins.open to io.OpenWrapper, but this is easier, since it doesn't
304
- // require the Python stdlib to be present
305
- let io = self . import ( "_io" , & [ ] , 0 ) ?;
306
- let io_open = self . get_attribute ( io, "open" ) ?;
307
- let set_stdio = |name, fd, mode : & str | {
308
- let stdio = self . invoke (
309
- & io_open,
310
- vec ! [ self . ctx. new_int( fd) , self . new_pyobj( mode) ] ,
311
- ) ?;
312
- self . set_attr (
313
- & self . sys_module ,
314
- format ! ( "__{}__" , name) , // e.g. __stdin__
315
- stdio. clone ( ) ,
316
- ) ?;
317
- self . set_attr ( & self . sys_module , name, stdio) ?;
318
- Ok ( ( ) )
319
- } ;
320
- set_stdio ( "stdin" , 0 , "r" ) ?;
321
- set_stdio ( "stdout" , 1 , "w" ) ?;
322
- set_stdio ( "stderr" , 2 , "w" ) ?;
323
-
324
- self . set_attr ( & self . builtins , "open" , io_open) ?;
325
- }
278
+ if self . initialized {
279
+ panic ! ( "Double Initialize Error" ) ;
280
+ }
326
281
282
+ builtins:: make_module ( self , self . builtins . clone ( ) ) ;
283
+ sysmodule:: make_module ( self , self . sys_module . clone ( ) , self . builtins . clone ( ) ) ;
284
+
285
+ let mut inner_init = || -> PyResult < ( ) > {
286
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
287
+ import:: import_builtin ( self , "signal" ) ?;
288
+
289
+ import:: init_importlib ( self , initialize_parameter) ?;
290
+
291
+ #[ cfg( any( not( target_arch = "wasm32" ) , target_os = "wasi" ) ) ]
292
+ {
293
+ // this isn't fully compatible with CPython; it imports "io" and sets
294
+ // builtins.open to io.OpenWrapper, but this is easier, since it doesn't
295
+ // require the Python stdlib to be present
296
+ let io = self . import ( "_io" , & [ ] , 0 ) ?;
297
+ let io_open = self . get_attribute ( io, "open" ) ?;
298
+ let set_stdio = |name, fd, mode : & str | {
299
+ let stdio =
300
+ self . invoke ( & io_open, vec ! [ self . ctx. new_int( fd) , self . new_pyobj( mode) ] ) ?;
301
+ self . set_attr (
302
+ & self . sys_module ,
303
+ format ! ( "__{}__" , name) , // e.g. __stdin__
304
+ stdio. clone ( ) ,
305
+ ) ?;
306
+ self . set_attr ( & self . sys_module , name, stdio) ?;
327
307
Ok ( ( ) )
328
308
} ;
309
+ set_stdio ( "stdin" , 0 , "r" ) ?;
310
+ set_stdio ( "stdout" , 1 , "w" ) ?;
311
+ set_stdio ( "stderr" , 2 , "w" ) ?;
312
+
313
+ self . set_attr ( & self . builtins , "open" , io_open) ?;
314
+ }
315
+
316
+ Ok ( ( ) )
317
+ } ;
329
318
330
- let res = inner_init ( ) ;
319
+ let res = inner_init ( ) ;
331
320
332
- self . expect_pyresult ( res, "initializiation failed" ) ;
321
+ self . expect_pyresult ( res, "initializiation failed" ) ;
333
322
334
- self . initialized = true ;
335
- }
336
- }
323
+ self . initialized = true ;
337
324
}
338
325
339
326
#[ cfg( feature = "threading" ) ]
@@ -1582,8 +1569,7 @@ pub struct Interpreter {
1582
1569
}
1583
1570
1584
1571
impl Interpreter {
1585
- pub fn new ( settings : PySettings ) -> Self {
1586
- let init = settings. initialization_parameter ;
1572
+ pub fn new ( settings : PySettings , init : InitParameter ) -> Self {
1587
1573
Self :: new_with_init ( settings, |_| init)
1588
1574
}
1589
1575
@@ -1618,7 +1604,7 @@ impl Interpreter {
1618
1604
1619
1605
impl Default for Interpreter {
1620
1606
fn default ( ) -> Self {
1621
- Self :: new ( PySettings :: default ( ) )
1607
+ Self :: new ( PySettings :: default ( ) , InitParameter :: External )
1622
1608
}
1623
1609
}
1624
1610
0 commit comments