execution.py
  1  import contextlib
  2  import faulthandler
  3  import io
  4  import multiprocessing
  5  import os
  6  import platform
  7  import signal
  8  import random
  9  import subprocess
 10  import tempfile
 11  from typing import *
 12  from pathlib import Path
 13  
 14  java_exec = ""
 15  node_exec = ""
 16  tsc_exec = ""
 17  go_exec = ""
 18  php_exec = ""
 19  cs_exec = ""
 20  
 21  
 22  JAVA_JAR = str(Path(__file__).joinpath("../../javatuples-1.2.jar").resolve())
 23  print(f"{JAVA_JAR = }")
 24  
 25  
 26  def check_correctness(
 27      task_id: str,
 28      sample: dict,
 29      language_type: str,
 30      timeout: float = 3.0,
 31      tmp_dir: str = None,
 32      completion_id: Optional[int] = None,
 33  ) -> Dict:
 34      """
 35      Evaluates the functional correctness of a completion by running the test
 36      suite provided in the problem.
 37      """
 38  
 39      def unsafe_execute(tmp_dir):
 40          random_id = random.randint(1, 100000)
 41          if "python" in language_type.lower():
 42              with create_tempdir():
 43  
 44                  # These system calls are needed when cleaning up tempdir.
 45                  import os
 46                  import shutil
 47  
 48                  rmtree = shutil.rmtree
 49                  rmdir = os.rmdir
 50                  chdir = os.chdir
 51  
 52                  # Disable functionalities that can make destructive changes to the test.
 53                  reliability_guard()
 54  
 55                  try:
 56                      exec_globals = {}
 57                      with swallow_io():
 58                          with time_limit(timeout):
 59                              # WARNING
 60                              # This program exists to execute untrusted model-generated code. Although
 61                              # it is highly unlikely that model-generated code will do something overtly
 62                              # malicious in response to this test suite, model-generated code may act
 63                              # destructively due to a lack of model capability or alignment.
 64                              # Users are strongly encouraged to sandbox this evaluation suite so that it
 65                              # does not perform destructive actions on their host or network.
 66                              # Once you have read this disclaimer and taken appropriate precautions,
 67                              # uncomment the following line and proceed at your own risk:
 68                              exec(sample["test_code"], exec_globals)
 69                          result.append("passed")
 70                  except TimeoutException:
 71                      result.append("timed out")
 72                  except AssertionError as e:
 73                      result.append(f"failed: AssertionError")
 74                  except BaseException as e:
 75                      result.append(f"failed: {e}")
 76                  # print(sample["test_code"])
 77                  # print(result)
 78                  # Needed for cleaning up.
 79                  shutil.rmtree = rmtree
 80                  os.rmdir = rmdir
 81                  os.chdir = chdir
 82  
 83          elif "go" in language_type.lower():
 84              assert tmp_dir is not None, "Go should be evaluated in a dir where necessary module files installed."
 85  
 86              import os
 87              import shutil
 88  
 89              if "tmp" not in tmp_dir:
 90                  tmp_dir = os.path.join(tmp_dir, "tmp")
 91              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
 92              if not os.path.exists(tmp_dir):
 93                  os.makedirs(tmp_dir)
 94              origin_path = os.getcwd()
 95              os.chdir(tmp_dir)
 96              open(f"main_test.go", "w").write(sample["test_code"])
 97              try:
 98                  exec_result = None
 99                  with time_limit(timeout):
100                      # WARNING
101                      # This program exists to execute untrusted model-generated code. Although
102                      # it is highly unlikely that model-generated code will do something overtly
103                      # malicious in response to this test suite, model-generated code may act
104                      # destructively due to a lack of model capability or alignment.
105                      # Users are strongly encouraged to sandbox this evaluation suite so that it
106                      # does not perform destructive actions on their host or network.
107                      # Once you have read this disclaimer and taken appropriate precautions,
108                      # uncomment the following line and proceed at your own risk:
109                      exec_result = subprocess.run(
110                          [f"{go_exec}go", "test", f"-timeout={timeout}s", "main_test.go"], timeout=timeout, capture_output=True
111                      )
112  
113                  if exec_result.returncode == 0:
114                      result.append("passed")
115                  else:
116                      if exec_result.stderr:
117                          try:
118                              err = exec_result.stderr.decode()
119                          except:
120                              err = exec_result.stderr
121                      else:
122                          try:
123                              err = exec_result.stdout.decode()
124                          except:
125                              err = exec_result.stdout
126                      result.append(f"failed: {err}")
127  
128              except TimeoutException:
129                  result.append("timed out")
130              os.chdir(origin_path)
131              shutil.rmtree(tmp_dir, ignore_errors=True)
132          elif "js" in language_type.lower():
133              import os
134              import shutil
135  
136              if "tmp" not in tmp_dir:
137                  tmp_dir = os.path.join(tmp_dir, "tmp")
138              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
139              if not os.path.exists(tmp_dir):
140                  os.makedirs(tmp_dir)
141              origin_path = os.getcwd()
142              os.chdir(tmp_dir)
143              open(f"test.js", "w").write(sample["test_code"])
144              try:
145                  exec_result = None
146                  with time_limit(timeout):
147                      # WARNING
148                      # This program exists to execute untrusted model-generated code. Although
149                      # it is highly unlikely that model-generated code will do something overtly
150                      # malicious in response to this test suite, model-generated code may act
151                      # destructively due to a lack of model capability or alignment.
152                      # Users are strongly encouraged to sandbox this evaluation suite so that it
153                      # does not perform destructive actions on their host or network.
154                      # Once you have read this disclaimer and taken appropriate precautions,
155                      # uncomment the following line and proceed at your own risk:
156                      exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True)
157  
158                  if exec_result.stderr.decode():
159                      err = exec_result.stderr.decode()
160                      result.append(f"failed: {err}")
161                  elif exec_result.stdout.decode():
162                      err = exec_result.stdout.decode()
163                      result.append(f"failed: {err}")
164                  else:
165                      result.append("passed")
166  
167              except TimeoutException:
168                  result.append("timed out")
169              os.chdir(origin_path)
170              shutil.rmtree(tmp_dir, ignore_errors=True)
171          elif "cpp" in language_type.lower():
172              import os
173              import shutil
174  
175              origin_path = os.getcwd()
176              if "tmp" not in tmp_dir:
177                  tmp_dir = os.path.join(tmp_dir, "tmp")
178              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
179              if not os.path.exists(tmp_dir):
180                  os.makedirs(tmp_dir)
181  
182              os.chdir(tmp_dir)
183              open(f"test.cpp", "w").write(sample["test_code"])
184              if "162" in task_id:
185                  compilation_result = subprocess.run(
186                      ["/usr/bin/g++", "-std=c++17", "test.cpp", "-lcrypto", "-lssl"], timeout=timeout, capture_output=True
187                  )
188              else:
189                  compilation_result = subprocess.run(["/usr/bin/g++", "-std=c++17", "test.cpp"], timeout=timeout, capture_output=True)
190              if compilation_result.returncode != 0:
191                  if compilation_result.stderr:
192                      err = compilation_result.stderr.decode()
193                  else:
194                      err = compilation_result.stdout.decode()
195                  result.append(f"failed: compilation error: {err}")
196              else:
197                  try:
198                      exec_result = None
199                      with time_limit(timeout):
200                          # WARNING
201                          # This program exists to execute untrusted model-generated code. Although
202                          # it is highly unlikely that model-generated code will do something overtly
203                          # malicious in response to this test suite, model-generated code may act
204                          # destructively due to a lack of model capability or alignment.
205                          # Users are strongly encouraged to sandbox this evaluation suite so that it
206                          # does not perform destructive actions on their host or network.
207                          # Once you have read this disclaimer and taken appropriate precautions,
208                          # uncomment the following line and proceed at your own risk:
209                          exec_result = subprocess.run(["./a.out"], timeout=timeout, capture_output=True)
210  
211                      if exec_result.returncode == 0:
212                          result.append("passed")
213                      else:
214                          if exec_result.stderr:
215                              try:
216                                  err = exec_result.stderr.decode()
217                              except:
218                                  err = exec_result.stderr
219                          else:
220                              try:
221                                  err = exec_result.stdout.decode()
222                              except:
223                                  err = exec_result.stdout
224                          result.append(f"failed: {err}")
225                  except TimeoutException:
226                      result.append("timed out")
227              # print(result[-1])
228              # print(sample["test_code"])
229              os.chdir(origin_path)
230              shutil.rmtree(tmp_dir, ignore_errors=True)
231          elif "php" in language_type.lower():
232              import os
233              import shutil
234  
235              origin_path = os.getcwd()
236              if "tmp" not in tmp_dir:
237                  tmp_dir = os.path.join(tmp_dir, "tmp")
238              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
239              if not os.path.exists(tmp_dir):
240                  os.makedirs(tmp_dir)
241  
242              os.chdir(tmp_dir)
243              open(f"test.php", "w").write(sample["test_code"])
244              try:
245                  exec_result = None
246                  with time_limit(timeout):
247                      cmd = f"{php_exec}php -f test.php"
248                      exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True)
249  
250                  if exec_result.returncode == 0:
251                      result.append("passed")
252                  else:
253                      if exec_result.stderr:
254                          try:
255                              err = exec_result.stderr.decode()
256                          except:
257                              err = exec_result.stderr
258                      else:
259                          try:
260                              err = exec_result.stdout.decode()
261                          except:
262                              err = exec_result.stdout
263                      result.append(f"failed: {err}")
264              except TimeoutException:
265                  result.append("timed out")
266              # print(result[-1])
267              # print(sample["test_code"])
268              os.chdir(origin_path)
269              shutil.rmtree(tmp_dir, ignore_errors=True)
270          elif "sh" in language_type.lower():
271              import os
272              import shutil
273  
274              origin_path = os.getcwd()
275              if "tmp" not in tmp_dir:
276                  tmp_dir = os.path.join(tmp_dir, "tmp")
277              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
278              if not os.path.exists(tmp_dir):
279                  os.makedirs(tmp_dir)
280  
281              os.chdir(tmp_dir)
282              open(f"test.sh", "w").write(sample["test_code"])
283              try:
284                  exec_result = None
285                  with time_limit(timeout):
286                      cmd = "/bin/bash test.sh"
287                      exec_result = subprocess.run(cmd, timeout=10, capture_output=True, shell=True)
288  
289                  if exec_result.returncode == 0:
290                      result.append("passed")
291                  else:
292                      if exec_result.stderr:
293                          try:
294                              err = exec_result.stderr.decode()
295                          except:
296                              err = exec_result.stderr
297                      else:
298                          try:
299                              err = exec_result.stdout.decode()
300                          except:
301                              err = exec_result.stdout
302                      result.append(f"failed: {err}")
303              except TimeoutException:
304                  result.append("timed out")
305              # print(result[-1])
306              # print(sample["test_code"])
307              os.chdir(origin_path)
308              shutil.rmtree(tmp_dir, ignore_errors=True)
309          elif "ts" in language_type.lower():
310              import os
311              import shutil
312  
313              origin_path = os.getcwd()
314              if "tmp" not in tmp_dir:
315                  tmp_dir = os.path.join(tmp_dir, "tmp")
316              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
317              if not os.path.exists(tmp_dir):
318                  os.makedirs(tmp_dir)
319  
320              os.chdir(tmp_dir)
321              env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]}
322              open(f"test.ts", "w").write(sample["test_code"])
323              cmd = f"{tsc_exec}tsc test.ts --target ES2015 --lib ES2015,DOM"
324              compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True)
325              if compilation_result.returncode != 0:
326                  if compilation_result.stderr:
327                      err = compilation_result.stderr.decode()
328                  else:
329                      err = compilation_result.stdout.decode()
330                  result.append(f"failed: compilation error: {err}")
331              else:
332                  try:
333                      exec_result = None
334                      with time_limit(timeout):
335                          exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True)
336  
337                      if exec_result.returncode == 0:
338                          result.append("passed")
339                      else:
340                          if exec_result.stderr:
341                              try:
342                                  err = exec_result.stderr.decode()
343                              except:
344                                  err = exec_result.stderr
345                          else:
346                              try:
347                                  err = exec_result.stdout.decode()
348                              except:
349                                  err = exec_result.stdout
350                          result.append(f"failed: {err}")
351                  except TimeoutException:
352                      result.append("timed out")
353              if result[-1] != "passed":
354                  env = {"PATH": f"{node_exec}:" + subprocess.os.environ["PATH"]}
355                  cmd = f"{tsc_exec}tsc test.ts"
356                  compilation_result = subprocess.run(cmd, timeout=timeout, capture_output=True, env=env, shell=True)
357                  if compilation_result.returncode != 0:
358                      if compilation_result.stderr:
359                          err = compilation_result.stderr.decode()
360                      else:
361                          err = compilation_result.stdout.decode()
362                      result[-1] = f"failed: compilation error: {err}"
363                  else:
364                      try:
365                          exec_result = None
366                          with time_limit(timeout):
367                              exec_result = subprocess.run([f"{node_exec}node", "test.js"], timeout=timeout, capture_output=True)
368  
369                          if exec_result.returncode == 0:
370                              result[-1] = "passed"
371                          else:
372                              if exec_result.stderr:
373                                  try:
374                                      err = exec_result.stderr.decode()
375                                  except:
376                                      err = exec_result.stderr
377                              else:
378                                  try:
379                                      err = exec_result.stdout.decode()
380                                  except:
381                                      err = exec_result.stdout
382                              result[-1] = f"failed: {err}"
383                      except TimeoutException:
384                          result[-1] = "timed out"
385  
386              os.chdir(origin_path)
387              shutil.rmtree(tmp_dir, ignore_errors=True)
388          elif "cs" in language_type.lower():
389              import os
390              import shutil
391  
392              origin_path = os.getcwd()
393              if "tmp" not in tmp_dir:
394                  tmp_dir = os.path.join(tmp_dir, "tmp")
395              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
396              if not os.path.exists(tmp_dir):
397                  os.makedirs(tmp_dir)
398              os.chdir(tmp_dir)
399              open(f"Program.cs", "w").write(sample["test_code"])
400              cmd = f"{cs_exec}mcs -d:DEBUG Program.cs"
401              compilation_result = subprocess.run(cmd, shell=True, capture_output=True)
402              if compilation_result.returncode != 0:
403                  if compilation_result.stderr:
404                      err = compilation_result.stderr.decode()
405                  else:
406                      err = compilation_result.stdout.decode()
407                  result.append(f"failed: compilation error: {err}")
408              else:
409                  try:
410                      exec_result = None
411                      cmd = f"{cs_exec}mono Program.exe"
412                      env = dict(MONO_TRACE_LISTENER="Console.Error")
413                      with time_limit(timeout):
414                          exec_result = subprocess.run(cmd, timeout=timeout, shell=True, capture_output=True, env=env)
415  
416                      if "Fail" not in exec_result.stderr.decode():
417                          result.append("passed")
418                      else:
419                          if exec_result.stderr:
420                              try:
421                                  err = exec_result.stderr.decode()
422                              except:
423                                  err = exec_result.stderr
424                          else:
425                              try:
426                                  err = exec_result.stdout.decode()
427                              except:
428                                  err = exec_result.stdout
429                          result.append(f"failed: {err}")
430                  except TimeoutException:
431                      result.append("timed out")
432                  except Exception as e:
433                      result.append(f"failed: {e}")
434              os.chdir(origin_path)
435              shutil.rmtree(tmp_dir, ignore_errors=True)
436          elif "rs" in language_type.lower():
437              import os
438  
439              WD: str = os.path.dirname(os.path.abspath(__file__))
440              RUST_DIR: str = os.path.join(WD, "rust")
441              RUST_SRC: str = os.path.join(RUST_DIR, "src")
442              RUST_BIN: str = os.path.join(RUST_SRC, "bin")
443              RUST_TMP_DIR: str = os.path.join(RUST_DIR, "tmp")
444              RUST_LOGS: str = os.path.join(RUST_TMP_DIR, "logs")
445              RUST_EXT: str = ".rs"
446  
447              # Create mandatory tmp directories
448              os.makedirs(RUST_TMP_DIR, exist_ok=True)
449              os.makedirs(RUST_LOGS, exist_ok=True)
450              os.makedirs(RUST_SRC, exist_ok=True)
451              os.makedirs(RUST_BIN, exist_ok=True)
452  
453              with tempfile.NamedTemporaryFile(dir=RUST_BIN, delete=False) as f:
454                  # temporal file name
455                  file_prefix = sample["task_id"].lower().replace("/", "_")
456                  file_name: str = file_prefix + RUST_EXT
457  
458                  os.rename(f.name, os.path.join(RUST_BIN, file_name))
459  
460                  # Sample to pure Rust function
461                  rust_code: str = sample["test_code"]
462  
463                  # dump the rust source code in the target temporal file
464                  f.write(rust_code.encode("utf-8"))
465  
466              # Proceed towards Rust binaries compilation. Therefore move to Rust module root dir.
467              os.chdir(RUST_DIR)
468  
469              # Two possible outcomes
470              # Pass OR Fail compilation
471              log_filename: str = file_prefix + ".jsonl"
472              log_path: str = os.path.join(RUST_LOGS, log_filename)
473              cargo_check: str = "cargo check --bin " + file_prefix + " --message-format json >> " + log_path
474              # Compilation build status
475              returned_val_compilation: int
476  
477              # Overwrite file content
478              if os.path.exists(log_path):
479                  if (file_size := os.path.getsize(log_path)) >= 0:
480                      os.remove(log_path)
481                      returned_val_compilation = os.system(cargo_check)
482  
483              else:
484                  returned_val_compilation = os.system(cargo_check)
485  
486              # 0 means success
487              if returned_val_compilation == 0:
488  
489                  # Execution pipeline
490                  cargo_test: str = "cargo test --bin " + file_prefix + " --message-format json >> " + log_path
491                  returned_val_execution = os.system(cargo_test)
492  
493                  if returned_val_execution == 0:
494                      result.append("passed")
495                  else:
496                      result.append(f"failed: execution error")
497  
498              else:
499                  result.append(f"failed: compilation error")
500  
501          elif "java" in language_type.lower():
502              assert tmp_dir is not None, "Java should be evaluated in a temporary dir."
503  
504              import os
505              import shutil
506  
507              if "tmp" not in tmp_dir:
508                  tmp_dir = os.path.join(tmp_dir, "tmp")
509              tmp_dir = os.path.join(tmp_dir, f"{task_id.replace('/', '-')}-{random_id}")
510              if not os.path.exists(tmp_dir):
511                  os.makedirs(tmp_dir)
512              open(os.path.join(tmp_dir, "Problem.java"), "w").write(sample["test_code"])
513              origin_path = os.getcwd()
514              os.system(f"cp {JAVA_JAR} {tmp_dir}/")
515              os.chdir(tmp_dir)
516              res = "failed: unknown error"
517              compile_returncode = -1
518              for _ in range(5):
519                  try:
520                      cmd = f"{java_exec}javac -cp javatuples-1.2.jar Problem.java"
521                      compilation_result = subprocess.run(cmd, timeout=60, capture_output=True, shell=True)
522                      compile_returncode = compilation_result.returncode
523                      break
524                  except subprocess.TimeoutExpired as e:
525                      continue
526              if compile_returncode != 0:
527                  res = "failed: compilation error"
528              else:
529                  exec_result = None
530                  try:
531                      # WARNING
532                      # This program exists to execute untrusted model-generated code. Although
533                      # it is highly unlikely that model-generated code will do something overtly
534                      # malicious in response to this test suite, model-generated code may act
535                      # destructively due to a lack of model capability or alignment.
536                      # Users are strongly encouraged to sandbox this evaluation suite so that it
537                      # does not perform destructive actions on their host or network.
538                      # Once you have read this disclaimer and taken appropriate precautions,
539                      # uncomment the following line and proceed at your own risk:
540                      cmd = f"{java_exec}java -ea -cp .:javatuples-1.2.jar Problem"
541                      exec_result = subprocess.run(cmd, timeout=timeout, capture_output=True, shell=True)
542                      if exec_result.returncode == 0:
543                          res = "passed"
544                      elif exec_result.returncode == 1:
545                          if "AssertionError" in exec_result.stderr.decode("unicode-escape"):
546                              res = "failed: wrong answer"
547                          else:
548                              res = f"failed: {exec_result.stderr.decode()}"
549                  except subprocess.TimeoutExpired as e:
550                      res = "time out"
551                  except BaseException as e:
552                      res = f"failed: {e}"
553  
554              result.append(res)
555              os.chdir(origin_path)
556              shutil.rmtree(tmp_dir, ignore_errors=True)
557  
558      manager = multiprocessing.Manager()
559      result = manager.list()
560  
561      p = multiprocessing.Process(target=unsafe_execute, args=(tmp_dir,))
562      p.start()
563      p.join(timeout=timeout + 1)
564      if p.is_alive():
565          p.kill()
566  
567      if not result:
568          result.append("timed out")
569  
570      return {
571          "task_id": task_id,
572          "completion_id": completion_id,
573          "result": result[0],
574          "passed": result[0] == "passed",
575          "finish": -1 if "finish" not in sample else sample["finish"],
576          "code": sample["test_code"],
577      }
578  
579  
580  # Copyright (c) OpenAI (https://openai.com)
581  
582  # Permission is hereby granted, free of charge, to any person obtaining a copy
583  # of this software and associated documentation files (the "Software"), to deal
584  # in the Software without restriction, including without limitation the rights
585  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
586  # copies of the Software, and to permit persons to whom the Software is
587  # furnished to do so, subject to the following conditions:
588  
589  # The above copyright notice and this permission notice shall be included in
590  # all copies or substantial portions of the Software.
591  
592  
593  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
594  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
595  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
596  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
597  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
598  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
599  # THE SOFTWARE.
600  # ============================================================================
601  @contextlib.contextmanager
602  def time_limit(seconds: float):
603      def signal_handler(signum, frame):
604          raise TimeoutException("Timed out!")
605  
606      signal.setitimer(signal.ITIMER_REAL, seconds)
607      signal.signal(signal.SIGALRM, signal_handler)
608      try:
609          yield
610      finally:
611          signal.setitimer(signal.ITIMER_REAL, 0)
612  
613  
614  @contextlib.contextmanager
615  def swallow_io():
616      stream = WriteOnlyStringIO()
617      with contextlib.redirect_stdout(stream):
618          with contextlib.redirect_stderr(stream):
619              with redirect_stdin(stream):
620                  yield
621  
622  
623  @contextlib.contextmanager
624  def create_tempdir():
625      with tempfile.TemporaryDirectory() as dirname:
626          with chdir(dirname):
627              yield dirname
628  
629  
630  class TimeoutException(Exception):
631      pass
632  
633  
634  class WriteOnlyStringIO(io.StringIO):
635      """StringIO that throws an exception when it's read from"""
636  
637      def read(self, *args, **kwargs):
638          raise IOError
639  
640      def readline(self, *args, **kwargs):
641          raise IOError
642  
643      def readlines(self, *args, **kwargs):
644          raise IOError
645  
646      def readable(self, *args, **kwargs):
647          """Returns True if the IO object can be read."""
648          return False
649  
650  
651  class redirect_stdin(contextlib._RedirectStream):  # type: ignore
652      _stream = "stdin"
653  
654  
655  @contextlib.contextmanager
656  def chdir(root):
657      if root == ".":
658          yield
659          return
660      cwd = os.getcwd()
661      os.chdir(root)
662      try:
663          yield
664      except BaseException as exc:
665          raise exc
666      finally:
667          os.chdir(cwd)
668  
669  
670  def reliability_guard(maximum_memory_bytes: Optional[int] = None):
671      """
672      This disables various destructive functions and prevents the generated code
673      from interfering with the test (e.g. fork bomb, killing other processes,
674      removing filesystem files, etc.)
675  
676      WARNING
677      This function is NOT a security sandbox. Untrusted code, including, model-
678      generated code, should not be blindly executed outside of one. See the
679      Codex paper for more information about OpenAI's code sandbox, and proceed
680      with caution.
681      """
682  
683      if maximum_memory_bytes is not None:
684          import resource
685  
686          resource.setrlimit(resource.RLIMIT_AS, (maximum_memory_bytes, maximum_memory_bytes))
687          resource.setrlimit(resource.RLIMIT_DATA, (maximum_memory_bytes, maximum_memory_bytes))
688          if not platform.uname().system == "Darwin":
689              resource.setrlimit(resource.RLIMIT_STACK, (maximum_memory_bytes, maximum_memory_bytes))
690  
691      faulthandler.disable()
692  
693      import builtins
694  
695      builtins.exit = None
696      builtins.quit = None
697  
698      import os
699  
700      os.environ["OMP_NUM_THREADS"] = "1"
701  
702      os.kill = None
703      os.system = None
704      os.putenv = None
705      os.remove = None
706      os.removedirs = None
707      os.rmdir = None
708      os.fchdir = None
709      os.setuid = None
710      os.fork = None
711      os.forkpty = None
712      os.killpg = None
713      os.rename = None
714      os.renames = None
715      os.truncate = None
716      os.replace = None
717      os.unlink = None
718      os.fchmod = None
719      os.fchown = None
720      os.chmod = None
721      os.chown = None
722      os.chroot = None
723      os.fchdir = None
724      os.lchflags = None
725      os.lchmod = None
726      os.lchown = None
727      os.getcwd = None
728      os.chdir = None
729  
730      import shutil
731  
732      shutil.rmtree = None
733      shutil.move = None
734      shutil.chown = None
735  
736      import subprocess
737  
738      subprocess.Popen = None  # type: ignore
739  
740      __builtins__["help"] = None
741  
742      import sys
743  
744      sys.modules["ipdb"] = None
745      sys.modules["joblib"] = None
746      sys.modules["resource"] = None
747      sys.modules["psutil"] = None
748      sys.modules["tkinter"] = None