@@ -403,16 +403,18 @@ def check_output(self, name, input_file, output_file_path, output, answer_file_p
403403 output_file .write ("\n " .join (output ) + "\n " )
404404 return self .check_output_checker (name , input_file , output_file_path , answer_file_path )
405405
406- def execute_oiejq (self , command , name , result_file_path , input_file_path , output_file_path , answer_file_path ,
406+ def execute_oiejq (self , name , timetool_path , executable , result_file_path , input_file_path , output_file_path , answer_file_path ,
407407 time_limit , memory_limit , hard_time_limit ):
408+ command = f'"{ timetool_path } " "{ executable } "'
408409 env = os .environ .copy ()
409410 env ["MEM_LIMIT" ] = f'{ memory_limit } K'
410411 env ["MEASURE_MEM" ] = "1"
411412
412413 timeout = False
413- with open (input_file_path , "r" ) as input_file :
414- process = subprocess .Popen (command , shell = True , stdin = input_file , stdout = subprocess .PIPE ,
415- stderr = subprocess .PIPE , env = env , preexec_fn = os .setsid )
414+ with open (input_file_path , "r" ) as input_file , open (output_file_path , "w" ) as output_file , \
415+ open (result_file_path , "w" ) as result_file :
416+ process = subprocess .Popen (command , shell = True , stdin = input_file , stdout = output_file ,
417+ stderr = result_file , env = env , preexec_fn = os .setsid )
416418
417419 def sigint_handler (signum , frame ):
418420 try :
@@ -423,7 +425,7 @@ def sigint_handler(signum, frame):
423425 signal .signal (signal .SIGINT , sigint_handler )
424426
425427 try :
426- output , lines = process .communicate (timeout = hard_time_limit )
428+ process .wait (timeout = time_limit )
427429 except subprocess .TimeoutExpired :
428430 timeout = True
429431 try :
@@ -432,11 +434,15 @@ def sigint_handler(signum, frame):
432434 pass
433435 process .communicate ()
434436
437+ with open (result_file_path , "r" ) as result_file :
438+ lines = result_file .read ()
439+ with open (output_file_path , "r" ) as output_file :
440+ output = output_file .read ()
435441 result = ExecutionResult ()
436442
437443 if not timeout :
438- lines = lines .decode ( 'utf-8' ). splitlines ()
439- output = output .decode ( 'utf-8' ). splitlines ()
444+ lines = lines .splitlines ()
445+ output = output .splitlines ()
440446
441447 for line in lines :
442448 line = line .strip ()
@@ -476,14 +482,20 @@ def sigint_handler(signum, frame):
476482 return result
477483
478484
479- def execute_time (self , command , name , result_file_path , input_file_path , output_file_path , answer_file_path ,
485+ def execute_time (self , name , executable , result_file_path , input_file_path , output_file_path , answer_file_path ,
480486 time_limit , memory_limit , hard_time_limit ):
481-
482- executable = package_util .get_executable (name )
487+ if sys .platform == 'darwin' :
488+ time_name = 'gtime'
489+ elif sys .platform == 'linux' :
490+ time_name = 'time'
491+ elif sys .platform == 'win32' or sys .platform == 'cygwin' :
492+ raise Exception ("Measuring time with GNU time on Windows is not supported." )
493+
494+ command = [f'{ time_name } ' , '-f' , '%U\\ n%M\\ n%x' , '-o' , result_file_path , executable ]
483495 timeout = False
484496 mem_limit_exceeded = False
485- with open (input_file_path , "r" ) as input_file :
486- process = subprocess .Popen (command , stdin = input_file , stdout = subprocess . PIPE , stderr = subprocess .DEVNULL ,
497+ with open (input_file_path , "r" ) as input_file , open ( output_file_path , "w" ) as output_file :
498+ process = subprocess .Popen (command , stdin = input_file , stdout = output_file , stderr = subprocess .DEVNULL ,
487499 preexec_fn = os .setsid )
488500
489501 def sigint_handler (signum , frame ):
@@ -520,12 +532,13 @@ def sigint_handler(signum, frame):
520532 pass
521533 timeout = True
522534 break
523- output , _ = process .communicate ()
524535
536+ with open (output_file_path , "r" ) as output_file :
537+ output = output_file .read ()
525538 result = ExecutionResult ()
526539 program_exit_code = None
527540 if not timeout :
528- output = output .decode ( "utf-8" ). splitlines ()
541+ output = output .splitlines ()
529542 with open (result_file_path , "r" ) as result_file :
530543 lines = result_file .readlines ()
531544 if len (lines ) == 3 :
@@ -589,22 +602,10 @@ def run_solution(self, data_for_execution: ExecutionData):
589602 hard_time_limit_in_s = math .ceil (2 * time_limit / 1000.0 )
590603
591604 if self .timetool_name == 'oiejq' :
592- command = f'"{ timetool_path } " "{ executable } "'
593-
594- return self .execute_oiejq (command , name , result_file , test , output_file , self .get_output_file (test ),
605+ return self .execute_oiejq (name , timetool_path , executable , result_file , test , output_file , self .get_output_file (test ),
595606 time_limit , memory_limit , hard_time_limit_in_s )
596607 elif self .timetool_name == 'time' :
597- if sys .platform == 'darwin' :
598- timeout_name = 'gtimeout'
599- time_name = 'gtime'
600- elif sys .platform == 'linux' :
601- timeout_name = 'timeout'
602- time_name = 'time'
603- elif sys .platform == 'win32' or sys .platform == 'cygwin' :
604- raise Exception ("Measuring time with GNU time on Windows is not supported." )
605-
606- command = [f'{ time_name } ' , '-f' , '%U\\ n%M\\ n%x' , '-o' , result_file , executable ]
607- return self .execute_time (command , name , result_file , test , output_file , self .get_output_file (test ),
608+ return self .execute_time (name , executable , result_file , test , output_file , self .get_output_file (test ),
608609 time_limit , memory_limit , hard_time_limit_in_s )
609610
610611 def run_solutions (self , compiled_commands , names , solutions ):
@@ -1179,11 +1180,11 @@ def run(self, args):
11791180 self .check_errors (all_results )
11801181 try :
11811182 validation_results = self .validate_expected_scores (results )
1182- except :
1183+ except Exception :
11831184 self .config = util .try_fix_config (self .config )
11841185 try :
11851186 validation_results = self .validate_expected_scores (results )
1186- except :
1187+ except Exception :
11871188 util .exit_with_error ("Validating expected scores failed. "
11881189 "This probably means that `sinol_expected_scores` is broken. "
11891190 "Delete it and run `sinol-make run --apply-suggestions` again." )
0 commit comments