@@ -37,6 +37,7 @@ def __init__(
3737 self ._main_window .clear_code_result ()
3838 self ._still_run_program : bool = False
3939 self ._program_buffer_size = program_buffer
40+ self ._program_encoding = encoding
4041 self ._run_output_queue : Queue = Queue ()
4142 self ._run_error_queue : Queue = Queue ()
4243 self ._read_program_error_output_from_thread : Union [threading .Thread , None ] = None
@@ -51,25 +52,18 @@ def __init__(
5152 self ._compiler_path = check_and_choose_venv (venv_path )
5253 else :
5354 self ._compiler_path = main_window .python_compiler
54- if sys .platform in ["win32" , "cygwin" , "msys" ]:
55- args = [
56- self ._compiler_path ,
57- "-m" ,
58- "test_pioneer" ,
59- "-e" ,
60- executable_path
61- ]
62- else :
63- args = " " .join ([
64- f"{ self ._compiler_path } " , "-m test_pioneer" , "-e" , f"{ executable_path } "
65- ])
66- self ._process : subprocess .Popen = subprocess .Popen (
55+ args = [
56+ str (self ._compiler_path ),
57+ "-m" ,
58+ "test_pioneer" ,
59+ "-e" ,
60+ executable_path
61+ ]
62+ self ._process : Union [subprocess .Popen , None ] = subprocess .Popen (
6763 args ,
6864 stdin = subprocess .PIPE ,
6965 stdout = subprocess .PIPE ,
7066 stderr = subprocess .PIPE ,
71- shell = True ,
72- encoding = encoding
7367 )
7468
7569 # Pyside UI update method
@@ -118,7 +112,7 @@ def exit_program(self):
118112 self ._read_program_output_from_thread = None
119113 if self ._read_program_error_output_from_thread is not None :
120114 self ._read_program_error_output_from_thread = None
121- self .print_and_clear_queue ()
115+ self .drain_and_clear_queue ()
122116 if self ._process is not None :
123117 self ._process .terminate ()
124118 text_cursor = self ._code_window .code_result .textCursor ()
@@ -128,27 +122,52 @@ def exit_program(self):
128122 text_cursor .insertBlock ()
129123 self ._process = None
130124
131- def print_and_clear_queue (self ):
132- self ._run_output_queue = queue .Queue ()
133- self ._run_error_queue = queue .Queue ()
125+ def drain_and_clear_queue (self ):
126+ while not self ._run_output_queue .empty ():
127+ try :
128+ output_message = self ._run_output_queue .get_nowait ()
129+ output_message = str (output_message ).strip ()
130+ if output_message :
131+ text_cursor = self ._code_window .code_result .textCursor ()
132+ text_format = QTextCharFormat ()
133+ text_format .setForeground (actually_color_dict .get ("normal_output_color" ))
134+ text_cursor .insertText (output_message , text_format )
135+ text_cursor .insertBlock ()
136+ except queue .Empty :
137+ break
138+ while not self ._run_error_queue .empty ():
139+ try :
140+ error_message = self ._run_error_queue .get_nowait ()
141+ error_message = str (error_message ).strip ()
142+ if error_message :
143+ text_cursor = self ._code_window .code_result .textCursor ()
144+ text_format = QTextCharFormat ()
145+ text_format .setForeground (actually_color_dict .get ("error_output_color" ))
146+ text_cursor .insertText (error_message , text_format )
147+ text_cursor .insertBlock ()
148+ except queue .Empty :
149+ break
134150
135151 def read_program_output_from_process (self ):
136152 while self ._still_run_program :
137- self .process : subprocess .Popen
138- program_output_data = self ._process .stdout .readline (self ._program_buffer_size ) \
139- .decode ("utf-8" , "replace" )
140- if self ._process :
141- self ._process .stdout .flush ()
142- if program_output_data .strip () != "" :
153+ proc = self ._process
154+ if proc is None :
155+ break
156+ program_output_data = proc .stdout .readline (self ._program_buffer_size )
157+ if isinstance (program_output_data , bytes ):
158+ program_output_data = program_output_data .decode (self ._program_encoding , "replace" )
159+ if program_output_data .strip ():
143160 self ._run_output_queue .put (program_output_data )
144161
145162 def read_program_error_output_from_process (self ):
146163 while self ._still_run_program :
147- program_error_output_data = self ._process .stderr .readline (self ._program_buffer_size ) \
148- .decode ("utf-8" , "replace" )
149- if self ._process :
150- self ._process .stderr .flush ()
151- if program_error_output_data .strip () != "" :
164+ proc = self ._process
165+ if proc is None :
166+ break
167+ program_error_output_data = proc .stderr .readline (self ._program_buffer_size )
168+ if isinstance (program_error_output_data , bytes ):
169+ program_error_output_data = program_error_output_data .decode (self ._program_encoding , "replace" )
170+ if program_error_output_data .strip ():
152171 self ._run_error_queue .put (program_error_output_data )
153172
154173 def start_test_pioneer_process (self ):
0 commit comments