giulio98 commited on
Commit
e25d0da
·
1 Parent(s): b24aec2

Update execute.py

Browse files
Files changed (1) hide show
  1. execute.py +25 -15
execute.py CHANGED
@@ -70,22 +70,24 @@ def unsafe_execute(check_program, output, result, timeout):
70
  chdir = os.chdir
71
 
72
  # Disable functionalities that can make destructive changes to the test.
73
- # reliability_guard()
74
 
75
  # Run program.
76
- try:
77
- exec_globals = {}
78
- with time_limit(timeout):
79
- with stdoutIO() as s:
80
- exec(check_program, exec_globals)
81
- if s.getvalue().strip() == output[0].strip():
82
- result.append("passed")
83
- else:
84
- result.append("not passed")
85
- except TimeoutException:
86
- result.append("timed out")
87
- except BaseException as e:
88
- result.append(f"failed: {e}")
 
 
89
 
90
  # Needed for cleaning up.
91
  shutil.rmtree = rmtree
@@ -100,6 +102,14 @@ def stdoutIO(stdout=None):
100
  sys.stdout = stdout
101
  yield stdout
102
  sys.stdout = old
 
 
 
 
 
 
 
 
103
 
104
 
105
  @contextlib.contextmanager
@@ -229,7 +239,7 @@ def reliability_guard(maximum_memory_bytes=None):
229
 
230
  import subprocess
231
 
232
- #subprocess.Popen = None # type: ignore
233
 
234
  __builtins__["help"] = None
235
 
 
70
  chdir = os.chdir
71
 
72
  # Disable functionalities that can make destructive changes to the test.
73
+ reliability_guard()
74
 
75
  # Run program.
76
+
77
+ with stdoutIO() as s:
78
+ try:
79
+ exec_globals = {}
80
+ with swallow_io():
81
+ with time_limit(timeout):
82
+ exec(check_program, exec_globals)
83
+ except TimeoutException:
84
+ result.append("timed out")
85
+ except BaseException as e:
86
+ result.append(f"failed: {e}")
87
+ if s.getvalue().strip() == output.strip():
88
+ result.append("passed")
89
+ else:
90
+ result.append("not passed")
91
 
92
  # Needed for cleaning up.
93
  shutil.rmtree = rmtree
 
102
  sys.stdout = stdout
103
  yield stdout
104
  sys.stdout = old
105
+
106
+ @contextlib.contextmanager
107
+ def swallow_io():
108
+ stream = WriteOnlyStringIO()
109
+ with contextlib.redirect_stdout(stream):
110
+ with contextlib.redirect_stderr(stream):
111
+ with redirect_stdin(stream):
112
+ yield
113
 
114
 
115
  @contextlib.contextmanager
 
239
 
240
  import subprocess
241
 
242
+ subprocess.Popen = None # type: ignore
243
 
244
  __builtins__["help"] = None
245