Spaces:
Sleeping
Sleeping
Updated `return_output` logic
Browse files
restrictedpython_code_eval.py
CHANGED
@@ -525,7 +525,6 @@ def _unsafe_execute(check_program, result, timeout,
|
|
525 |
allow_underscore_variable_names: bool = False, return_output: bool = False, output_variable: str = "output"):
|
526 |
|
527 |
with create_tempdir():
|
528 |
-
|
529 |
# These system calls are needed when cleaning up tempdir.
|
530 |
import os
|
531 |
import shutil
|
@@ -537,9 +536,6 @@ def _unsafe_execute(check_program, result, timeout,
|
|
537 |
# Disable functionalities that can make destructive changes to the test.
|
538 |
reliability_guard()
|
539 |
|
540 |
-
if return_output and additional_locals is None:
|
541 |
-
additional_locals = {}
|
542 |
-
|
543 |
# Run program.
|
544 |
try:
|
545 |
builtins = {}
|
@@ -606,12 +602,15 @@ def _unsafe_execute(check_program, result, timeout,
|
|
606 |
if '_apply_' not in exec_globals:
|
607 |
exec_globals['_apply_'] = _apply # type: ignore
|
608 |
|
|
|
|
|
|
|
609 |
with swallow_io():
|
610 |
policy_class = AllowAugmentedAssignAndUnderscoreVariableNamesRestrictingTransformer if allow_underscore_variable_names else AllowAugmentedAssignRestrictingTransformer
|
611 |
|
612 |
with time_limit(timeout):
|
613 |
byte_code = compile_restricted(check_program, filename="<model output>", mode="exec", policy=policy_class)
|
614 |
-
exec(byte_code, exec_globals,
|
615 |
|
616 |
result['result'] = "passed"
|
617 |
|
@@ -623,8 +622,8 @@ def _unsafe_execute(check_program, result, timeout,
|
|
623 |
result['result'] = f"failed ({type(e)}): {str(e)}"
|
624 |
result['exception'] = e
|
625 |
|
626 |
-
if return_output and output_variable in
|
627 |
-
result[output_variable] =
|
628 |
|
629 |
# Needed for cleaning up.
|
630 |
shutil.rmtree = rmtree
|
|
|
525 |
allow_underscore_variable_names: bool = False, return_output: bool = False, output_variable: str = "output"):
|
526 |
|
527 |
with create_tempdir():
|
|
|
528 |
# These system calls are needed when cleaning up tempdir.
|
529 |
import os
|
530 |
import shutil
|
|
|
536 |
# Disable functionalities that can make destructive changes to the test.
|
537 |
reliability_guard()
|
538 |
|
|
|
|
|
|
|
539 |
# Run program.
|
540 |
try:
|
541 |
builtins = {}
|
|
|
602 |
if '_apply_' not in exec_globals:
|
603 |
exec_globals['_apply_'] = _apply # type: ignore
|
604 |
|
605 |
+
if additional_locals is not None:
|
606 |
+
additional_globals.update(additional_locals)
|
607 |
+
|
608 |
with swallow_io():
|
609 |
policy_class = AllowAugmentedAssignAndUnderscoreVariableNamesRestrictingTransformer if allow_underscore_variable_names else AllowAugmentedAssignRestrictingTransformer
|
610 |
|
611 |
with time_limit(timeout):
|
612 |
byte_code = compile_restricted(check_program, filename="<model output>", mode="exec", policy=policy_class)
|
613 |
+
exec(byte_code, exec_globals, None)
|
614 |
|
615 |
result['result'] = "passed"
|
616 |
|
|
|
622 |
result['result'] = f"failed ({type(e)}): {str(e)}"
|
623 |
result['exception'] = e
|
624 |
|
625 |
+
if return_output and output_variable in exec_globals: # type: ignore
|
626 |
+
result[output_variable] = exec_globals[output_variable] # type: ignore
|
627 |
|
628 |
# Needed for cleaning up.
|
629 |
shutil.rmtree = rmtree
|