Muennighoff commited on
Commit
2e80bdf
1 Parent(s): ee457bf
Files changed (1) hide show
  1. execute.py +48 -13
execute.py CHANGED
@@ -40,8 +40,12 @@ def check_correctness(check_program, timeout, task_id, completion_id, language):
40
 
41
  if language == "python":
42
  p = multiprocessing.Process(target=unsafe_execute, args=(check_program, result, timeout))
 
 
43
  elif language == "javascript":
44
  p = multiprocessing.Process(target=unsafe_execute_js, args=(check_program, result, timeout))
 
 
45
 
46
  p.start()
47
  p.join(timeout=timeout + 1)
@@ -91,19 +95,55 @@ def unsafe_execute(check_program, result, timeout):
91
  os.rmdir = rmdir
92
  os.chdir = chdir
93
 
94
- def unsafe_execute_js(check_program, result, timeout):
95
 
96
  with create_tempdir():
 
97
 
98
- open(f"test.js", 'w').write(check_program)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- # These system calls are needed when cleaning up tempdir.
101
- import os
102
- import shutil
103
 
104
- rmtree = shutil.rmtree
105
- rmdir = os.rmdir
106
- chdir = os.chdir
107
 
108
  # Run program.
109
  try:
@@ -122,11 +162,6 @@ def unsafe_execute_js(check_program, result, timeout):
122
  except TimeoutException:
123
  result.append("timed out")
124
 
125
- # Needed for cleaning up.
126
- shutil.rmtree = rmtree
127
- os.rmdir = rmdir
128
- os.chdir = chdir
129
-
130
 
131
  @contextlib.contextmanager
132
  def time_limit(seconds):
 
40
 
41
  if language == "python":
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:
48
+ raise ValueError(f"Language {language} not supported. Feel free to add it :)")
49
 
50
  p.start()
51
  p.join(timeout=timeout + 1)
 
95
  os.rmdir = rmdir
96
  os.chdir = chdir
97
 
98
+ def unsafe_execute_cpp(check_program, result, timeout):
99
 
100
  with create_tempdir():
101
+ open(f"test.cpp", 'w').write(check_program)
102
 
103
+ # Run program.
104
+ try:
105
+ exec_globals = {}
106
+
107
+ # -lcrypto & -lssl are needed for the crypto library required by HumanEval-X-Bugs Task 162
108
+ compilation_result = subprocess.run(
109
+ ["/usr/bin/g++", "-std=c++11", "test.cpp", "-lcrypto", "-lssl"],
110
+ timeout=timeout,
111
+ capture_output=True,
112
+ )
113
+ if compilation_result.returncode != 0:
114
+ if compilation_result.stderr:
115
+ err = compilation_result.stderr.decode()
116
+ else:
117
+ err = compilation_result.stdout.decode()
118
+ result.append(f"failed: compilation error: {err}")
119
+ else:
120
+ try:
121
+ with time_limit(timeout):
122
+ exec_result = subprocess.run(["./a.out"], timeout=timeout, capture_output=True)
123
+
124
+ if exec_result.returncode == 0:
125
+ result.append("passed")
126
+ else:
127
+ if exec_result.stderr:
128
+ try:
129
+ err = exec_result.stderr.decode()
130
+ except:
131
+ err = exec_result.stderr
132
+ else:
133
+ try:
134
+ err = exec_result.stdout.decode()
135
+ except:
136
+ err = exec_result.stdout
137
+ result.append(f"failed: {err}")
138
+
139
+ except TimeoutException:
140
+ result.append("timed out")
141
 
142
+ def unsafe_execute_js(check_program, result, timeout):
 
 
143
 
144
+ with create_tempdir():
145
+
146
+ open(f"test.js", 'w').write(check_program)
147
 
148
  # Run program.
149
  try:
 
162
  except TimeoutException:
163
  result.append("timed out")
164
 
 
 
 
 
 
165
 
166
  @contextlib.contextmanager
167
  def time_limit(seconds):