Spaces:
Runtime error
Runtime error
Muennighoff
commited on
Commit
•
be2051a
1
Parent(s):
9382ca3
Add Java
Browse files- execute.py +40 -0
execute.py
CHANGED
@@ -42,6 +42,8 @@ def check_correctness(check_program, timeout, task_id, completion_id, language):
|
|
42 |
p = multiprocessing.Process(target=unsafe_execute, args=(check_program, result, timeout))
|
43 |
elif language == "cpp":
|
44 |
p = multiprocessing.Process(target=unsafe_execute_cpp, args=(check_program, result, timeout))
|
|
|
|
|
45 |
elif language == "javascript":
|
46 |
p = multiprocessing.Process(target=unsafe_execute_js, args=(check_program, result, timeout))
|
47 |
else:
|
@@ -135,6 +137,44 @@ def unsafe_execute_cpp(check_program, result, timeout):
|
|
135 |
except TimeoutException:
|
136 |
result.append("timed out")
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
def unsafe_execute_js(check_program, result, timeout):
|
139 |
|
140 |
with create_tempdir():
|
|
|
42 |
p = multiprocessing.Process(target=unsafe_execute, args=(check_program, result, timeout))
|
43 |
elif language == "cpp":
|
44 |
p = multiprocessing.Process(target=unsafe_execute_cpp, args=(check_program, result, timeout))
|
45 |
+
elif language == "java":
|
46 |
+
p = multiprocessing.Process(target=unsafe_execute_java, args=(check_program, result, timeout))
|
47 |
elif language == "javascript":
|
48 |
p = multiprocessing.Process(target=unsafe_execute_js, args=(check_program, result, timeout))
|
49 |
else:
|
|
|
137 |
except TimeoutException:
|
138 |
result.append("timed out")
|
139 |
|
140 |
+
def unsafe_execute_java(check_program, result, timeout):
|
141 |
+
|
142 |
+
with create_tempdir():
|
143 |
+
|
144 |
+
open(f"Main.java", 'w').write(check_program)
|
145 |
+
|
146 |
+
res = "failed: unknown error"
|
147 |
+
compile_returncode = -1
|
148 |
+
for _ in range(5):
|
149 |
+
try:
|
150 |
+
compilation_result = subprocess.run(['javac', "Main.java"], timeout=5,
|
151 |
+
capture_output=True)
|
152 |
+
compile_returncode = compilation_result.returncode
|
153 |
+
break
|
154 |
+
except subprocess.TimeoutExpired as e:
|
155 |
+
continue
|
156 |
+
if compile_returncode != 0:
|
157 |
+
res = "failed: compilation error"
|
158 |
+
else:
|
159 |
+
try:
|
160 |
+
with time_limit(timeout):
|
161 |
+
exec_result = subprocess.run([f'java', '-cp', tmp_dir, 'Main'], timeout=timeout, capture_output=True)
|
162 |
+
if exec_result.returncode == 0:
|
163 |
+
res = "passed"
|
164 |
+
elif exec_result.returncode == 1:
|
165 |
+
if "AssertionError" in exec_result.stderr.decode('unicode-escape'):
|
166 |
+
res = "failed: wrong answer"
|
167 |
+
else:
|
168 |
+
res = f"failed: {exec_result.stderr.decode()}"
|
169 |
+
except subprocess.TimeoutExpired as e:
|
170 |
+
res = "timed out"
|
171 |
+
except BaseException as e:
|
172 |
+
res = f"failed: {e}"
|
173 |
+
result.append(res)
|
174 |
+
|
175 |
+
except TimeoutException:
|
176 |
+
result.append("timed out")
|
177 |
+
|
178 |
def unsafe_execute_js(check_program, result, timeout):
|
179 |
|
180 |
with create_tempdir():
|