Skip to content

Commit c0aab60

Browse files
committed
fixed stdout bypass
1 parent 56402b8 commit c0aab60

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/lib/python/unit-test-runner.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1+
import builtins
12
import contextlib
3+
import io
24
import json
35
import os
46
import sys
5-
import io
67
import time
78

89
from unittests import test
910

11+
# Save the real stdout/stderr for our own use
12+
_real_stdout = sys.__stdout__
13+
_real_stderr = sys.__stderr__
14+
15+
# Close and replace sys.__stdout__ and sys.__stderr__ before importing usercode
16+
# This prevents usercode from using sys.__stdout__ to bypass redirection
17+
try:
18+
# Create closed file objects that will raise ValueError if accessed
19+
closed_file = open(os.devnull, "w")
20+
closed_file.close()
21+
22+
# Replace the dunder attributes with closed files
23+
sys.__stdout__ = closed_file
24+
sys.__stderr__ = closed_file
25+
except Exception as e:
26+
_real_stderr.write(f"Warning: Could not close __stdout__/__stderr__: {e}\n")
27+
1028
try:
1129
from usercode import solution
1230
except ImportError:
13-
sys.__stdout__.write(
31+
_real_stdout.write(
1432
json.dumps(
1533
{
1634
"runalyzer_errors": "Solution function not found.",
@@ -51,7 +69,7 @@ def truncate_output(output, max_chars=256):
5169
duration = end - start
5270

5371
except Exception as e:
54-
sys.__stderr__.write(str(e))
72+
_real_stderr.write(str(e))
5573
exit(1)
5674

5775
# Get captured output and truncate
@@ -62,7 +80,7 @@ def truncate_output(output, max_chars=256):
6280
with open(os.devnull, "w") as devnull:
6381
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):
6482
# Print final results (this goes to the original stdout before redirection)
65-
sys.__stdout__.write(
83+
_real_stdout.write(
6684
json.dumps(
6785
{
6886
"test_result": test_res,

0 commit comments

Comments
 (0)