88import re
99import subprocess
1010import tempfile
11- from typing import Union , overload
11+ from typing import Union , overload , Optional
1212from io import IOBase
1313from . import log
1414from .utils import list_like , make_unicode
@@ -19,29 +19,30 @@ class IO:
1919
2020 @overload
2121 def __init__ (self ,
22- input_file : Union [IOBase , str , int , None ] = None ,
23- output_file : Union [IOBase , str , int , None ] = None ,
24- data_id : Union [ str , None ] = None ,
22+ input_file : Optional [ Union [IOBase , str , int ] ] = None ,
23+ output_file : Optional [ Union [IOBase , str , int ] ] = None ,
24+ data_id : Optional [ int ] = None ,
2525 disable_output : bool = False ):
2626 ...
2727
2828 @overload
2929 def __init__ (self ,
30- data_id : Union [ str , None ] = None ,
31- file_prefix : Union [str , None ] = None ,
32- input_suffix : Union [ str , None ] = '.in' ,
33- output_suffix : Union [ str , None ] = '.out' ,
30+ data_id : Optional [ int ] = None ,
31+ file_prefix : Optional [str ] = None ,
32+ input_suffix : str = '.in' ,
33+ output_suffix : str = '.out' ,
3434 disable_output : bool = False ):
3535 ...
3636
37- def __init__ (self ,
38- input_file : Union [IOBase , str , int , None ] = None ,
39- output_file : Union [IOBase , str , int , None ] = None ,
40- data_id : Union [str , None ] = None ,
41- file_prefix : Union [str , None ] = None ,
42- input_suffix : Union [str , None ] = '.in' ,
43- output_suffix : Union [str , None ] = '.out' ,
44- disable_output : bool = False ):
37+ def __init__ ( # type: ignore
38+ self ,
39+ input_file : Optional [Union [IOBase , str , int ]] = None ,
40+ output_file : Optional [Union [IOBase , str , int ]] = None ,
41+ data_id : Optional [int ] = None ,
42+ file_prefix : Optional [str ] = None ,
43+ input_suffix : str = '.in' ,
44+ output_suffix : str = '.out' ,
45+ disable_output : bool = False ):
4546 """
4647 Args:
4748 input_file (optional): input file object or filename or file descriptor.
@@ -216,6 +217,8 @@ def output_gen(self, shell_cmd, time_limit=None):
216217 time_limit: the time limit (seconds) of the command to run.
217218 None means infinity. Defaults to None.
218219 """
220+ if self .output_file is None :
221+ raise ValueError ("Output file is disabled" )
219222 self .flush_buffer ()
220223 origin_pos = self .input_file .tell ()
221224 self .input_file .seek (0 )
@@ -224,16 +227,16 @@ def output_gen(self, shell_cmd, time_limit=None):
224227 shell_cmd ,
225228 shell = True ,
226229 timeout = time_limit ,
227- stdin = self .input_file ,
228- stdout = self .output_file ,
230+ stdin = self .input_file . fileno () ,
231+ stdout = self .output_file . fileno () ,
229232 universal_newlines = True ,
230233 )
231234 else :
232235 subprocess .check_call (
233236 shell_cmd ,
234237 shell = True ,
235- stdin = self .input_file ,
236- stdout = self .output_file ,
238+ stdin = self .input_file . fileno () ,
239+ stdout = self .output_file . fileno () ,
237240 universal_newlines = True ,
238241 )
239242 self .input_file .seek (origin_pos )
@@ -248,6 +251,8 @@ def output_write(self, *args, **kwargs):
248251 *args: the elements to write
249252 separator: a string used to separate every element. Defaults to " ".
250253 """
254+ if self .output_file is None :
255+ raise ValueError ("Output file is disabled" )
251256 self .__write (self .output_file , * args , ** kwargs )
252257
253258 def output_writeln (self , * args , ** kwargs ):
0 commit comments