@@ -1079,13 +1079,6 @@ def run_code_and_maybe_finish(self, for_code=None):
10791079 if err :
10801080 indent = 0
10811081
1082- # TODO This should be printed ABOVE the error that just happened
1083- # instead or maybe just thrown away and not shown
1084- if self .current_stdouterr_line :
1085- self .display_lines .extend (paint .display_linize (
1086- self .current_stdouterr_line , self .width ))
1087- self .current_stdouterr_line = ''
1088-
10891082 if self .rl_history .index == 0 :
10901083 self ._set_current_line (' ' * indent , update_completion = True )
10911084 else :
@@ -1137,11 +1130,26 @@ def clear_current_block(self, remove_from_history=True):
11371130 def get_current_block (self ):
11381131 return '\n ' .join (self .buffer + [self .current_line ])
11391132
1133+ def move_current_stdouterr_line_up (self ):
1134+ """Append self.current_stdouterr_line to self.display_lines
1135+ then clean it."""
1136+ self .display_lines .extend (paint .display_linize (
1137+ self .current_stdouterr_line , self .width ))
1138+ self .current_stdouterr_line = ''
1139+
11401140 def send_to_stdout (self , output ):
11411141 """Send unicode string to Repl stdout"""
1142+ if not output : return
11421143 lines = output .split ('\n ' )
1144+ if output == len (output ) * '\n ' :
1145+ # If the string consist only of newline characters,
1146+ # str.split returns one more empty strings.
1147+ lines = lines [:- 1 ]
11431148 logger .debug ('display_lines: %r' , self .display_lines )
1144- self .current_stdouterr_line += lines [0 ]
1149+ if lines [0 ]:
1150+ self .current_stdouterr_line += lines [0 ]
1151+ else :
1152+ self .move_current_stdouterr_line_up ()
11451153 if len (lines ) > 1 :
11461154 self .display_lines .extend (paint .display_linize (
11471155 self .current_stdouterr_line , self .width , blank_line = True ))
@@ -1157,9 +1165,16 @@ def send_to_stderr(self, error):
11571165
11581166 Must be able to handle FmtStrs because interpreter pass in
11591167 tracebacks already formatted."""
1168+ if not error : return
11601169 lines = error .split ('\n ' )
1170+ if error == len (error ) * '\n ' :
1171+ # If the string consist only of newline characters,
1172+ # str.split returns one more empty strings.
1173+ lines = lines [:- 1 ]
11611174 if lines [- 1 ]:
11621175 self .current_stdouterr_line += lines [- 1 ]
1176+ else :
1177+ self .move_current_stdouterr_line_up ()
11631178 self .display_lines .extend (sum ((paint .display_linize (line , self .width ,
11641179 blank_line = True )
11651180 for line in lines [:- 1 ]), []))
0 commit comments