@@ -69,10 +69,18 @@ pub struct VirtualMachine {
69
69
pub settings : PySettings ,
70
70
pub recursion_limit : Cell < usize > ,
71
71
pub codec_registry : RefCell < Vec < PyObjectRef > > ,
72
+ pub initialized : bool ,
72
73
}
73
74
74
75
pub const NSIG : usize = 64 ;
75
76
77
+ #[ derive( Copy , Clone ) ]
78
+ pub enum InitParameter {
79
+ NoInitialize ,
80
+ InitializeInternal ,
81
+ InitializeExternal ,
82
+ }
83
+
76
84
/// Struct containing all kind of settings for the python vm.
77
85
pub struct PySettings {
78
86
/// -d command line switch
@@ -107,6 +115,10 @@ pub struct PySettings {
107
115
108
116
/// sys.argv
109
117
pub argv : Vec < String > ,
118
+
119
+ /// Initialization parameter to decide to initialize or not,
120
+ /// and to decide the importer required external filesystem access or not
121
+ pub initialization_parameter : InitParameter ,
110
122
}
111
123
112
124
/// Trace events for sys.settrace and sys.setprofile.
@@ -140,14 +152,15 @@ impl Default for PySettings {
140
152
dont_write_bytecode : false ,
141
153
path_list : vec ! [ ] ,
142
154
argv : vec ! [ ] ,
155
+ initialization_parameter : InitParameter :: InitializeExternal ,
143
156
}
144
157
}
145
158
}
146
159
147
160
impl VirtualMachine {
148
161
/// Create a new `VirtualMachine` structure.
149
162
pub fn new ( settings : PySettings ) -> VirtualMachine {
150
- flame_guard ! ( "init VirtualMachine" ) ;
163
+ flame_guard ! ( "new VirtualMachine" ) ;
151
164
let ctx = PyContext :: new ( ) ;
152
165
153
166
// make a new module without access to the vm; doesn't
@@ -167,8 +180,9 @@ impl VirtualMachine {
167
180
let profile_func = RefCell :: new ( ctx. none ( ) ) ;
168
181
let trace_func = RefCell :: new ( ctx. none ( ) ) ;
169
182
let signal_handlers = RefCell :: new ( arr ! [ ctx. none( ) ; 64 ] ) ;
183
+ let initialize_parameter = settings. initialization_parameter ;
170
184
171
- let vm = VirtualMachine {
185
+ let mut vm = VirtualMachine {
172
186
builtins : builtins. clone ( ) ,
173
187
sys_module : sysmod. clone ( ) ,
174
188
stdlib_inits,
@@ -185,6 +199,7 @@ impl VirtualMachine {
185
199
settings,
186
200
recursion_limit : Cell :: new ( 512 ) ,
187
201
codec_registry : RefCell :: default ( ) ,
202
+ initialized : false ,
188
203
} ;
189
204
190
205
objmodule:: init_module_dict (
@@ -199,14 +214,32 @@ impl VirtualMachine {
199
214
vm. new_str ( "sys" . to_owned ( ) ) ,
200
215
vm. get_none ( ) ,
201
216
) ;
217
+ vm. initialize ( initialize_parameter) ;
218
+ vm
219
+ }
202
220
203
- builtins :: make_module ( & vm , builtins . clone ( ) ) ;
204
- sysmodule :: make_module ( & vm , sysmod , builtins ) ;
221
+ pub fn initialize ( & mut self , initialize_parameter : InitParameter ) {
222
+ flame_guard ! ( "init VirtualMachine" ) ;
205
223
206
- #[ cfg( not( target_arch = "wasm32" ) ) ]
207
- import:: import_builtin ( & vm, "signal" ) . expect ( "Couldn't initialize signal module" ) ;
224
+ match initialize_parameter {
225
+ InitParameter :: NoInitialize => { }
226
+ _ => {
227
+ if self . initialized {
228
+ panic ! ( "Double Initialize Error" ) ;
229
+ }
208
230
209
- vm
231
+ builtins:: make_module ( self , self . builtins . clone ( ) ) ;
232
+ sysmodule:: make_module ( self , self . sys_module . clone ( ) , self . builtins . clone ( ) ) ;
233
+
234
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
235
+ import:: import_builtin ( self , "signal" ) . expect ( "Couldn't initialize signal module" ) ;
236
+
237
+ import:: init_importlib ( self , initialize_parameter)
238
+ . expect ( "Initialize importlib fail" ) ;
239
+
240
+ self . initialized = true ;
241
+ }
242
+ }
210
243
}
211
244
212
245
pub fn run_code_obj ( & self , code : PyCodeRef , scope : Scope ) -> PyResult {
0 commit comments