Skip to content

Commit f82ae64

Browse files
Use temp file to get SPJ output
1 parent 8ec5bc2 commit f82ae64

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

cyaron/graders/testlib_checker.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import tempfile
22
import subprocess
3-
import os
43
import xml.etree.ElementTree as xmlElementTree
54
from typing import Optional
6-
7-
STDOUT_DEV = "con" if os.name == "nt" else "/dev/stdout"
5+
from os.path import join as path_join
86

97
__all__ = ["TestlibChecker"]
108

@@ -32,27 +30,31 @@ def __init__(self, checker_path: str):
3230
self.checker_path = checker_path
3331

3432
def __call__(self, outs: str, ans: str, ins: str):
35-
with tempfile.NamedTemporaryFile(
36-
'w') as inf, tempfile.NamedTemporaryFile(
37-
'w') as outf, tempfile.NamedTemporaryFile('w') as ansf:
33+
with tempfile.NamedTemporaryFile('w') as inf, \
34+
tempfile.NamedTemporaryFile('w') as outf, \
35+
tempfile.NamedTemporaryFile('w') as ansf, \
36+
tempfile.TemporaryDirectory() as checker_output_dir:
3837
inf.write(ins)
3938
outf.write(outs)
4039
ansf.write(ans)
4140
inf.flush()
4241
outf.flush()
4342
ansf.flush()
43+
checker_output_file = path_join(checker_output_dir,
44+
'checker_output.xml')
45+
4446
result = subprocess.run((self.checker_path, inf.name, outf.name,
45-
ansf.name, STDOUT_DEV, '-appes'),
47+
ansf.name, checker_output_file, '-appes'),
4648
stdout=subprocess.PIPE,
4749
stderr=subprocess.PIPE,
4850
text=True,
4951
check=False)
5052
if result.stderr.strip() != 'See file to check exit message':
5153
raise ValueError("Invalid output from checker: " +
5254
result.stderr)
53-
checker_output = result.stdout
5455

55-
result_element = xmlElementTree.fromstring(checker_output)
56+
result_element = xmlElementTree.parse(
57+
checker_output_file).getroot()
5658
if result_element.tag != 'result':
5759
raise ValueError("Invalid output from checker")
5860
result_text = result_element.text

0 commit comments

Comments
 (0)