|
20 | 20 | import time |
21 | 21 | import zlib |
22 | 22 | from io import StringIO |
23 | | -from typing import Optional |
| 23 | +from typing import Optional, Any, Dict |
| 24 | + |
| 25 | +# Global storage for original references used by reliability_guard |
| 26 | +originals: Dict[str, Any] = {} |
24 | 27 |
|
25 | 28 |
|
26 | 29 | def has_code(response): |
@@ -126,8 +129,7 @@ def post_process_tests_inputs(raw_text, is_stdin): |
126 | 129 |
|
127 | 130 | # If no matches are found, fall back to line-by-line parsing |
128 | 131 | cleaned_lines = cleaned_string.split("\n") |
129 | | - if test_cases is None: |
130 | | - test_cases = [] |
| 132 | + test_cases = [] |
131 | 133 | for line in cleaned_lines: |
132 | 134 | try: |
133 | 135 | test_case = json.loads(line) |
@@ -229,10 +231,9 @@ def prepare_test_input_output_std(test_case): |
229 | 231 |
|
230 | 232 | def run_test_func(completion, is_extracted, test_input, test_output): |
231 | 233 | # print(f"inside: {completion}") |
| 234 | + # Define the namespace in which to execute the completion code |
| 235 | + namespace: Dict[str, Any] = {} |
232 | 236 | if not is_extracted: |
233 | | - # Define the namespace in which to execute the completion code |
234 | | - namespace = {} |
235 | | - |
236 | 237 | # Execute the generated code in the namespace |
237 | 238 |
|
238 | 239 | exec(completion, namespace) |
@@ -273,8 +274,6 @@ def run_test_func(completion, is_extracted, test_input, test_output): |
273 | 274 |
|
274 | 275 | return True, result_output |
275 | 276 | else: |
276 | | - namespace = {} |
277 | | - |
278 | 277 | # Execute the generated code in the namespace |
279 | 278 |
|
280 | 279 | exec(completion, namespace) |
@@ -313,7 +312,7 @@ def run_test_std(completion, test_input, test_output): |
313 | 312 | # Simulate that the code is being run as the main script |
314 | 313 | completion = '__name__ = "__main__"\n' + completion |
315 | 314 |
|
316 | | - namespace = {} |
| 315 | + namespace: Dict[str, Any] = {} |
317 | 316 | exec(completion, namespace) |
318 | 317 |
|
319 | 318 | output_value = output.getvalue().strip() |
@@ -409,7 +408,7 @@ def swallow_io(redirect_input=True): |
409 | 408 |
|
410 | 409 | with contextlib.redirect_stdout(stream), contextlib.redirect_stderr(stream): |
411 | 410 | if redirect_input: |
412 | | - with contextlib.redirect_stdin(StringIO()): # Redirect stdin if enabled |
| 411 | + with redirect_stdin(StringIO()): # Redirect stdin if enabled |
413 | 412 | yield stream |
414 | 413 | else: |
415 | 414 | yield stream # Do not redirect stdin |
@@ -443,8 +442,18 @@ def readable(self, *args, **kwargs): |
443 | 442 | return False |
444 | 443 |
|
445 | 444 |
|
446 | | -class redirect_stdin(contextlib._RedirectStream): # type: ignore |
447 | | - _stream = "stdin" |
| 445 | +class redirect_stdin: |
| 446 | + def __init__(self, new_target: Any): |
| 447 | + self._new_target = new_target |
| 448 | + self._old_target: Any = None |
| 449 | + |
| 450 | + def __enter__(self) -> Any: |
| 451 | + self._old_target = sys.stdin |
| 452 | + sys.stdin = self._new_target |
| 453 | + return self._new_target |
| 454 | + |
| 455 | + def __exit__(self, exc_type, exc, tb) -> None: |
| 456 | + sys.stdin = self._old_target |
448 | 457 |
|
449 | 458 |
|
450 | 459 | @contextlib.contextmanager |
@@ -484,7 +493,9 @@ def reliability_guard(maximum_memory_bytes: Optional[int] = None): |
484 | 493 | builtins.exit = cast(Any, None) # type: ignore[assignment] |
485 | 494 | builtins.quit = cast(Any, None) # type: ignore[assignment] |
486 | 495 |
|
487 | | - import os |
| 496 | + # Prepare Any-typed aliases to avoid mypy assignment errors |
| 497 | + os_mod: Any = os |
| 498 | + subprocess_mod: Any = subprocess |
488 | 499 |
|
489 | 500 | os.environ["OMP_NUM_THREADS"] = "1" |
490 | 501 |
|
@@ -516,20 +527,51 @@ def reliability_guard(maximum_memory_bytes: Optional[int] = None): |
516 | 527 | os.getcwd = cast(Any, None) # type: ignore[assignment] |
517 | 528 | os.chdir = cast(Any, None) # type: ignore[assignment] |
518 | 529 |
|
519 | | - import shutil |
| 530 | + # Disable destructive os functions (guard where platform-specific) |
| 531 | + for name in [ |
| 532 | + "kill", |
| 533 | + "system", |
| 534 | + "putenv", |
| 535 | + "remove", |
| 536 | + "removedirs", |
| 537 | + "rmdir", |
| 538 | + "fchdir", |
| 539 | + "setuid", |
| 540 | + "fork", |
| 541 | + "forkpty", |
| 542 | + "killpg", |
| 543 | + "rename", |
| 544 | + "renames", |
| 545 | + "truncate", |
| 546 | + "replace", |
| 547 | + "unlink", |
| 548 | + "fchmod", |
| 549 | + "fchown", |
| 550 | + "chmod", |
| 551 | + "chown", |
| 552 | + "chroot", |
| 553 | + "getcwd", |
| 554 | + "chdir", |
| 555 | + "lchflags", |
| 556 | + "lchmod", |
| 557 | + "lchown", |
| 558 | + ]: |
| 559 | + try: |
| 560 | + setattr(os_mod, name, None) |
| 561 | + except Exception: |
| 562 | + pass |
520 | 563 |
|
521 | 564 | shutil.rmtree = cast(Any, None) # type: ignore[assignment] |
522 | 565 | shutil.move = cast(Any, None) # type: ignore[assignment] |
523 | 566 | shutil.chown = cast(Any, None) # type: ignore[assignment] |
524 | 567 |
|
525 | | - import subprocess |
| 568 | + # Disable subprocess.Popen |
| 569 | + setattr(subprocess_mod, "Popen", None) |
526 | 570 |
|
527 | 571 | setattr(subprocess, "Popen", cast(Any, None)) # type: ignore[misc] |
528 | 572 |
|
529 | 573 | # __builtins__["help"] = None # this line is commented out as it results into error |
530 | 574 |
|
531 | | - import sys |
532 | | - |
533 | 575 | sys.modules["ipdb"] = None # type: ignore[assignment] |
534 | 576 | sys.modules["joblib"] = None # type: ignore[assignment] |
535 | 577 | sys.modules["resource"] = None # type: ignore[assignment] |
@@ -600,7 +642,7 @@ def restore_original_references(): |
600 | 642 | setattr(shutil, func_name, original_func) |
601 | 643 |
|
602 | 644 | # Restore 'subprocess' functions |
603 | | - subprocess.Popen = originals["subprocess"]["Popen"] |
| 645 | + setattr(subprocess, "Popen", originals["subprocess"]["Popen"]) |
604 | 646 |
|
605 | 647 | # Restore sys modules |
606 | 648 | for module_name, original_module in originals["sys_modules"].items(): |
|
0 commit comments