diff --git a/bpython/curtsiesfrontend/repl.py b/bpython/curtsiesfrontend/repl.py index d29722c43..4f32abb93 100644 --- a/bpython/curtsiesfrontend/repl.py +++ b/bpython/curtsiesfrontend/repl.py @@ -1079,13 +1079,6 @@ def run_code_and_maybe_finish(self, for_code=None): if err: indent = 0 - # TODO This should be printed ABOVE the error that just happened - # instead or maybe just thrown away and not shown - if self.current_stdouterr_line: - self.display_lines.extend(paint.display_linize( - self.current_stdouterr_line, self.width)) - self.current_stdouterr_line = '' - if self.rl_history.index == 0: self._set_current_line(' ' * indent, update_completion=True) else: @@ -1137,11 +1130,27 @@ def clear_current_block(self, remove_from_history=True): def get_current_block(self): return '\n'.join(self.buffer + [self.current_line]) + def move_current_stdouterr_line_up(self): + """Append self.current_stdouterr_line to self.display_lines + then clean it.""" + self.display_lines.extend(paint.display_linize( + self.current_stdouterr_line, self.width)) + self.current_stdouterr_line = '' + def send_to_stdout(self, output): """Send unicode string to Repl stdout""" + if not output: + return lines = output.split('\n') + if all(not line for line in lines): + # If the string consist only of newline characters, + # str.split returns one more empty strings. + lines = lines[:-1] logger.debug('display_lines: %r', self.display_lines) - self.current_stdouterr_line += lines[0] + if lines[0]: + self.current_stdouterr_line += lines[0] + else: + self.move_current_stdouterr_line_up() if len(lines) > 1: self.display_lines.extend(paint.display_linize( self.current_stdouterr_line, self.width, blank_line=True)) @@ -1157,9 +1166,17 @@ def send_to_stderr(self, error): Must be able to handle FmtStrs because interpreter pass in tracebacks already formatted.""" + if not error: + return lines = error.split('\n') + if all(not line for line in lines): + # If the string consist only of newline characters, + # str.split returns one more empty strings. + lines = lines[:-1] if lines[-1]: self.current_stdouterr_line += lines[-1] + else: + self.move_current_stdouterr_line_up() self.display_lines.extend(sum((paint.display_linize(line, self.width, blank_line=True) for line in lines[:-1]), []))