Skip to content

Commit 39b44f0

Browse files
committed
GR-16806: add GraalPythonVM env support
1 parent 7c2b139 commit 39b44f0

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

mx.graalpython/mx_graalpython_benchmark.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import mx_benchmark
3434
from mx_benchmark import StdOutRule, java_vm_registry, Vm, GuestVm, VmBenchmarkSuite, AveragingBenchmarkMixin
3535
from mx_graalpython_bench_param import HARNESS_PATH
36+
from contextlib import contextmanager
3637

3738
# ----------------------------------------------------------------------------------------------------------------------
3839
#
@@ -75,6 +76,27 @@ def _check_vm_args(name, args):
7576
"got {} instead".format(args))
7677

7778

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+
78100
# ----------------------------------------------------------------------------------------------------------------------
79101
#
80102
# the vm definitions
@@ -194,14 +216,15 @@ def name(self):
194216

195217
class GraalPythonVm(GuestVm):
196218
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):
198220
super(GraalPythonVm, self).__init__(host_vm=host_vm)
199221
self._config_name = config_name
200222
self._distributions = distributions
201223
self._cp_suffix = cp_suffix
202224
self._cp_prefix = cp_prefix
203225
self._extra_vm_args = extra_vm_args
204226
self._extra_polyglot_args = extra_polyglot_args
227+
self._env = env
205228

206229
def hosting_registry(self):
207230
return java_vm_registry
@@ -240,10 +263,11 @@ def run(self, cwd, args):
240263
cmd = truffle_options + vm_args + extra_polyglot_args + args
241264

242265
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)
247271

248272
def name(self):
249273
return VM_NAME_GRAALPYTHON
@@ -254,7 +278,8 @@ def config_name(self):
254278
def with_host_vm(self, host_vm):
255279
return self.__class__(config_name=self._config_name, distributions=self._distributions,
256280
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)
258283

259284

260285
# ----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)