@@ -1079,13 +1079,6 @@ def run_code_and_maybe_finish(self, for_code=None):
1079
1079
if err :
1080
1080
indent = 0
1081
1081
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
-
1089
1082
if self .rl_history .index == 0 :
1090
1083
self ._set_current_line (' ' * indent , update_completion = True )
1091
1084
else :
@@ -1137,11 +1130,26 @@ def clear_current_block(self, remove_from_history=True):
1137
1130
def get_current_block (self ):
1138
1131
return '\n ' .join (self .buffer + [self .current_line ])
1139
1132
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
+
1140
1140
def send_to_stdout (self , output ):
1141
1141
"""Send unicode string to Repl stdout"""
1142
+ if not output : return
1142
1143
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 ]
1143
1148
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 ()
1145
1153
if len (lines ) > 1 :
1146
1154
self .display_lines .extend (paint .display_linize (
1147
1155
self .current_stdouterr_line , self .width , blank_line = True ))
@@ -1157,9 +1165,16 @@ def send_to_stderr(self, error):
1157
1165
1158
1166
Must be able to handle FmtStrs because interpreter pass in
1159
1167
tracebacks already formatted."""
1168
+ if not error : return
1160
1169
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 ]
1161
1174
if lines [- 1 ]:
1162
1175
self .current_stdouterr_line += lines [- 1 ]
1176
+ else :
1177
+ self .move_current_stdouterr_line_up ()
1163
1178
self .display_lines .extend (sum ((paint .display_linize (line , self .width ,
1164
1179
blank_line = True )
1165
1180
for line in lines [:- 1 ]), []))
0 commit comments