33
33
import mx_benchmark
34
34
from mx_benchmark import StdOutRule , java_vm_registry , Vm , GuestVm , VmBenchmarkSuite , AveragingBenchmarkMixin
35
35
from mx_graalpython_bench_param import HARNESS_PATH
36
+ from contextlib import contextmanager
36
37
37
38
# ----------------------------------------------------------------------------------------------------------------------
38
39
#
@@ -75,6 +76,27 @@ def _check_vm_args(name, args):
75
76
"got {} instead" .format (args ))
76
77
77
78
79
+ @contextmanager
80
+ def environ (env ):
81
+ def _handle_var ((k , v )):
82
+ if v is None :
83
+ del os .environ [k ]
84
+ else :
85
+ os .environ [k ] = str (v )
86
+
87
+ if env :
88
+ prev_env = {v : os .getenv (v ) for v in env }
89
+ map (_handle_var , env .items ())
90
+ else :
91
+ prev_env = None
92
+
93
+ try :
94
+ yield
95
+ finally :
96
+ if prev_env :
97
+ map (_handle_var , prev_env .items ())
98
+
99
+
78
100
# ----------------------------------------------------------------------------------------------------------------------
79
101
#
80
102
# the vm definitions
@@ -194,14 +216,15 @@ def name(self):
194
216
195
217
class GraalPythonVm (GuestVm ):
196
218
def __init__ (self , config_name = CONFIGURATION_DEFAULT , distributions = None , cp_suffix = None , cp_prefix = None ,
197
- host_vm = None , extra_vm_args = None , extra_polyglot_args = None ):
219
+ host_vm = None , extra_vm_args = None , extra_polyglot_args = None , env = None ):
198
220
super (GraalPythonVm , self ).__init__ (host_vm = host_vm )
199
221
self ._config_name = config_name
200
222
self ._distributions = distributions
201
223
self ._cp_suffix = cp_suffix
202
224
self ._cp_prefix = cp_prefix
203
225
self ._extra_vm_args = extra_vm_args
204
226
self ._extra_polyglot_args = extra_polyglot_args
227
+ self ._env = env
205
228
206
229
def hosting_registry (self ):
207
230
return java_vm_registry
@@ -240,10 +263,11 @@ def run(self, cwd, args):
240
263
cmd = truffle_options + vm_args + extra_polyglot_args + args
241
264
242
265
host_vm = self .host_vm ()
243
- if hasattr (host_vm , 'run_lang' ):
244
- return host_vm .run_lang ('graalpython' , extra_polyglot_args + args , cwd )
245
- else :
246
- return host_vm .run (cwd , cmd )
266
+ with environ (self ._env ):
267
+ if hasattr (host_vm , 'run_lang' ):
268
+ return host_vm .run_lang ('graalpython' , extra_polyglot_args + args , cwd )
269
+ else :
270
+ return host_vm .run (cwd , cmd )
247
271
248
272
def name (self ):
249
273
return VM_NAME_GRAALPYTHON
@@ -254,7 +278,8 @@ def config_name(self):
254
278
def with_host_vm (self , host_vm ):
255
279
return self .__class__ (config_name = self ._config_name , distributions = self ._distributions ,
256
280
cp_suffix = self ._cp_suffix , cp_prefix = self ._cp_prefix , host_vm = host_vm ,
257
- extra_vm_args = self ._extra_vm_args , extra_polyglot_args = self ._extra_polyglot_args )
281
+ extra_vm_args = self ._extra_vm_args , extra_polyglot_args = self ._extra_polyglot_args ,
282
+ env = self ._env )
258
283
259
284
260
285
# ----------------------------------------------------------------------------------------------------------------------
0 commit comments