11from __future__ import absolute_import , print_function
22
33import multiprocessing
4- import os
54import subprocess
65import sys
76from concurrent .futures import ThreadPoolExecutor
87from io import open
9- from typing import List , Optional , Tuple , Union
8+ from typing import List , Optional , Tuple , Union , cast
109
1110from cyaron .consts import *
12- from cyaron .graders import CYaRonGraders , GraderType
11+ from cyaron .graders import CYaRonGraders , GraderType3
1312from cyaron .utils import *
1413
1514from . import log
@@ -27,14 +26,16 @@ def __str__(self):
2726 return "In program: '{}'. {}" .format (self .name , self .mismatch )
2827
2928
30- PrgoramType = Optional [ Union [str , Tuple [str , ...], List [str ] ]]
29+ PrgoramType = Union [str , Tuple [str , ...], List [str ]]
3130
3231
3332class Compare :
3433
3534 @staticmethod
36- def __compare_two (name , content , std , grader ):
37- result , info = CYaRonGraders .invoke (grader , content , std )
35+ def __compare_two (name : PrgoramType , content : str , std : str ,
36+ input_content : str , grader : Union [str , GraderType3 ]):
37+ result , info = CYaRonGraders .invoke (grader , content , std ,
38+ input_content )
3839 status = "Correct" if result else "!!!INCORRECT!!!"
3940 info = info if info is not None else ""
4041 log .debug ("{}: {} {}" .format (name , status , info ))
@@ -77,7 +78,7 @@ def output(cls, *files, **kwargs):
7778 ("stop_on_incorrect" , None ),
7879 ),
7980 )
80- std = kwargs ["std" ]
81+ std : IO = kwargs ["std" ]
8182 grader = kwargs ["grader" ]
8283 max_workers = kwargs ["max_workers" ]
8384 job_pool = kwargs ["job_pool" ]
@@ -101,13 +102,18 @@ def get_std():
101102 return cls .__process_output_file (std )[1 ]
102103
103104 if job_pool is not None :
104- std = job_pool .submit (get_std ).result ()
105+ std_answer = job_pool .submit (get_std ).result ()
105106 else :
106- std = get_std ()
107+ std_answer = get_std ()
108+
109+ with open (std .input_filename , "r" , newline = "\n " ,
110+ encoding = "utf-8" ) as input_file :
111+ input_text = input_file .read ()
107112
108113 def do (file ):
109114 (file_name , content ) = cls .__process_output_file (file )
110- cls .__compare_two (file_name , content , std , grader )
115+ cls .__compare_two (file_name , content , std_answer , input_text ,
116+ grader )
111117
112118 if job_pool is not None :
113119 job_pool .map (do , files )
@@ -121,8 +127,8 @@ def program(cls,
121127 std : Optional [Union [str , IO ]] = None ,
122128 std_program : Optional [Union [str , Tuple [str , ...],
123129 List [str ]]] = None ,
124- grader : Union [str , GraderType ] = DEFAULT_GRADER ,
125- max_workers : int = - 1 ,
130+ grader : Union [str , GraderType3 ] = DEFAULT_GRADER ,
131+ max_workers : Optional [ int ] = - 1 ,
126132 job_pool : Optional [ThreadPoolExecutor ] = None ,
127133 stop_on_incorrect = None ):
128134 """
@@ -182,7 +188,7 @@ def get_std_from_std_program():
182188 elif std is not None :
183189
184190 def get_std_from_std_file ():
185- return cls .__process_output_file (std )[1 ]
191+ return cls .__process_output_file (cast ( Union [ str , IO ], std ) )[1 ]
186192
187193 if job_pool is not None :
188194 std = job_pool .submit (get_std_from_std_file ).result ()
@@ -197,33 +203,29 @@ def get_std_from_std_file():
197203 "r" ,
198204 newline = "\n " ,
199205 encoding = "utf-8" ) as input_file :
200-
201- def do (program_name ):
202- timeout = None
203- if (list_like (program_name ) and len (program_name ) == 2
204- and int_like (program_name [- 1 ])):
205- program_name , timeout = program_name
206- if timeout is None :
207- content = subprocess .check_output (
208- program_name ,
209- shell = (not list_like (program_name )),
210- stdin = input_file ,
211- universal_newlines = True ,
212- encoding = "utf-8" ,
213- )
214- else :
215- content = subprocess .check_output (
216- program_name ,
217- shell = (not list_like (program_name )),
218- stdin = input_file ,
219- universal_newlines = True ,
220- timeout = timeout ,
221- encoding = "utf-8" ,
222- )
223- cls .__compare_two (program_name , content , std , grader )
224-
225- if job_pool is not None :
226- job_pool .map (do , programs )
206+ input_text = input_file .read ()
207+
208+ def do (program_name : Union [PrgoramType , Tuple [PrgoramType , float ]]):
209+ timeout = None
210+ if isinstance (program_name , tuple ) and len (program_name ) == 2 and (
211+ isinstance (program_name [1 ], float )
212+ or isinstance (program_name [1 ], int )):
213+ program_name , timeout = cast (Tuple [PrgoramType , float ],
214+ program_name )
227215 else :
228- for program in programs :
229- do (program )
216+ program_name = cast (PrgoramType , program_name )
217+ content = subprocess .check_output (
218+ list (program_name )
219+ if isinstance (program_name , tuple ) else program_name ,
220+ shell = (not list_like (program_name )),
221+ input = input_text ,
222+ universal_newlines = True ,
223+ encoding = "utf-8" ,
224+ timeout = timeout )
225+ cls .__compare_two (program_name , content , std , input_text , grader )
226+
227+ if job_pool is not None :
228+ job_pool .map (do , programs )
229+ else :
230+ for program in programs :
231+ do (program )
0 commit comments