MCPcopy Index your code
hub / github.com/zai-org/CodeGeeX2 / unsafe_execute

Function unsafe_execute

evaluation/execution.py:58–361  ·  view source on GitHub ↗
(tmp_dir)

Source from the content-addressed store, hash-verified

56 """
57
58 def unsafe_execute(tmp_dir):
59 random_id = random.uniform(1, 1000)
60 if "python" in language_type.lower():
61 with create_tempdir():
62
63 # These system calls are needed when cleaning up tempdir.
64 import os
65 import shutil
66 rmtree = shutil.rmtree
67 rmdir = os.rmdir
68 chdir = os.chdir
69
70 # Disable functionalities that can make destructive changes to the test.
71 reliability_guard()
72
73 try:
74 exec_globals = {}
75 with swallow_io():
76 with time_limit(timeout):
77 # WARNING
78 # This program exists to execute untrusted model-generated code. Although
79 # it is highly unlikely that model-generated code will do something overtly
80 # malicious in response to this test suite, model-generated code may act
81 # destructively due to a lack of model capability or alignment.
82 # Users are strongly encouraged to sandbox this evaluation suite so that it
83 # does not perform destructive actions on their host or network.
84 # Once you have read this disclaimer and taken appropriate precautions,
85 # uncomment the following line and proceed at your own risk:
86 exec(sample["test_code"], exec_globals)
87 result.append("passed")
88 except TimeoutException:
89 result.append("timed out")
90 except AssertionError as e:
91 result.append(f"failed: AssertionError")
92 except BaseException as e:
93 result.append(f"failed: {e}")
94
95 # Needed for cleaning up.
96 shutil.rmtree = rmtree
97 os.rmdir = rmdir
98 os.chdir = chdir
99
100 elif "go" in language_type.lower():
101 assert tmp_dir is not None, "Go should be evaluated in a dir where necessary module files installed."
102
103 import os
104 import shutil
105
106 if "tmp" not in tmp_dir:
107 tmp_dir = os.path.join(tmp_dir, "tmp")
108 tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
109 if not os.path.exists(tmp_dir):
110 os.makedirs(tmp_dir)
111
112 os.chdir(tmp_dir)
113 open(f"main_test.go", 'w').write(sample["test_code"])
114 try:
115 exec_result = None

Callers

nothing calls this directly

Calls 4

create_tempdirFunction · 0.85
reliability_guardFunction · 0.85
swallow_ioFunction · 0.85
time_limitFunction · 0.85

Tested by

no test coverage detected