|
3 | 3 | import sys |
4 | 4 | import types |
5 | 5 | import unittest |
| 6 | +import tempfile |
6 | 7 | import multiprocessing |
7 | 8 | from multiprocessing import Array, Value, Manager |
8 | 9 | from bigcodebench.eval.utils import ( |
|
17 | 18 |
|
18 | 19 | def trusted_exec(code, test_code, task_id, max_as_limit, max_data_limit, max_stack_limit, times): |
19 | 20 | """Execute trusted code in place.""" |
| 21 | + # Specify a unique cache dir by modifying XDG_CONFIG_HOME |
| 22 | + old_xdg = os.environ.get("XDG_CONFIG_HOME") |
| 23 | + temp_xdg = tempfile.mkdtemp(prefix="xdg_config_") |
| 24 | + os.environ["XDG_CONFIG_HOME"] = temp_xdg |
20 | 25 |
|
21 | | - with create_tempdir(): |
22 | | - import os |
23 | | - import shutil |
24 | | - import builtins |
25 | | - |
26 | | - rmtree = shutil.rmtree |
27 | | - rmdir = os.rmdir |
28 | | - chdir = os.chdir |
29 | | - module_name = "__test__" |
30 | | - new_module = types.ModuleType(module_name) |
31 | | - reliability_guard(max_as_limit, max_data_limit, max_stack_limit) |
32 | | - # Set necessary attributes for the module |
33 | | - new_module.__dict__.update({ |
34 | | - '__builtins__': builtins, |
35 | | - '__file__': f"{module_name}.py", |
36 | | - '__package__': None, |
37 | | - '__doc__': None, |
38 | | - 'sys': sys, |
39 | | - 'os': os, |
40 | | - 'environ': os.environ, |
41 | | - }) |
42 | | - |
43 | | - # Combine the user code and the test code |
44 | | - full_code = code + "\n" + test_code |
45 | | - |
46 | | - # Compile and execute the combined code within the new module |
47 | | - exec(compile(full_code, f"{module_name}.py", 'exec'), |
48 | | - new_module.__dict__) |
49 | | - sys.modules[module_name] = new_module |
50 | | - TestCases = getattr(new_module, 'TestCases') |
51 | | - loader = unittest.TestLoader() |
52 | | - suite = loader.loadTestsFromTestCase(TestCases) |
53 | | - test_result = unittest.TestResult() |
54 | | - start = time.time() |
55 | | - with safe_environment(), swallow_io(), time_limit(seconds=TIMEOUT_LIMIT): |
56 | | - suite.run(test_result) |
57 | | - |
58 | | - errors = test_result.failures + test_result.errors |
59 | | - if len(errors) > 0: |
60 | | - print(errors) |
61 | | - times.value = -1 |
| 26 | + try: |
| 27 | + with create_tempdir(): |
| 28 | + import shutil |
| 29 | + import builtins |
| 30 | + |
| 31 | + rmtree = shutil.rmtree |
| 32 | + rmdir = os.rmdir |
| 33 | + chdir = os.chdir |
| 34 | + module_name = "__test__" |
| 35 | + new_module = types.ModuleType(module_name) |
| 36 | + |
| 37 | + reliability_guard(max_as_limit, max_data_limit, max_stack_limit) |
| 38 | + |
| 39 | + # Set necessary attributes for the module |
| 40 | + new_module.__dict__.update({ |
| 41 | + '__builtins__': builtins, |
| 42 | + '__file__': f"{module_name}.py", |
| 43 | + '__package__': None, |
| 44 | + '__doc__': None, |
| 45 | + 'sys': sys, |
| 46 | + 'os': os, |
| 47 | + 'environ': os.environ, |
| 48 | + }) |
| 49 | + |
| 50 | + # Combine the user code and the test code |
| 51 | + full_code = code + "\n" + test_code |
| 52 | + |
| 53 | + # Compile and execute the combined code within the new module |
| 54 | + exec(compile(full_code, f"{module_name}.py", 'exec'), |
| 55 | + new_module.__dict__) |
| 56 | + sys.modules[module_name] = new_module |
| 57 | + TestCases = getattr(new_module, 'TestCases') |
| 58 | + loader = unittest.TestLoader() |
| 59 | + suite = loader.loadTestsFromTestCase(TestCases) |
| 60 | + test_result = unittest.TestResult() |
| 61 | + start = time.time() |
| 62 | + with safe_environment(), swallow_io(), time_limit(seconds=TIMEOUT_LIMIT): |
| 63 | + suite.run(test_result) |
| 64 | + |
| 65 | + errors = test_result.failures + test_result.errors |
| 66 | + if len(errors) > 0: |
| 67 | + print(errors) |
| 68 | + times.value = -1 |
| 69 | + else: |
| 70 | + times.value = time.time() - start |
| 71 | + |
| 72 | + # Needed for cleaning up. |
| 73 | + shutil.rmtree = rmtree |
| 74 | + os.rmdir = rmdir |
| 75 | + os.chdir = chdir |
| 76 | + |
| 77 | + finally: |
| 78 | + # Restore the original environment variable |
| 79 | + if old_xdg is None: |
| 80 | + os.environ.pop("XDG_CONFIG_HOME", None) |
62 | 81 | else: |
63 | | - times.value = time.time() - start |
64 | | - |
65 | | - # Needed for cleaning up. |
66 | | - shutil.rmtree = rmtree |
67 | | - os.rmdir = rmdir |
68 | | - os.chdir = chdir |
| 82 | + os.environ["XDG_CONFIG_HOME"] = old_xdg |
| 83 | + shutil.rmtree(temp_xdg, ignore_errors=True) |
69 | 84 |
|
70 | 85 |
|
71 | 86 | def trusted_check_exec(code, inputs): |
|
0 commit comments