From 084402aa7243c57b92690e02f734078cfe02918b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Sz=C3=B6ll=C5=91si?= Date: Sat, 27 May 2017 00:14:36 +0200 Subject: [PATCH 1/2] Fix newline handling in stdout and stderr --- bpython/curtsiesfrontend/repl.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/bpython/curtsiesfrontend/repl.py b/bpython/curtsiesfrontend/repl.py index d29722c43..a24d38c7e 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,26 @@ 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 output == len(output) * '\n': + # 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 +1165,16 @@ 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 error == len(error) * '\n': + # 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]), [])) From 7d2a6b7b5a200b6385293a1296424edfe1a9f3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Sz=C3=B6ll=C5=91si?= Date: Sun, 28 May 2017 12:53:17 +0200 Subject: [PATCH 2/2] Changes requested by sebastinas --- bpython/curtsiesfrontend/repl.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bpython/curtsiesfrontend/repl.py b/bpython/curtsiesfrontend/repl.py index a24d38c7e..4f32abb93 100644 --- a/bpython/curtsiesfrontend/repl.py +++ b/bpython/curtsiesfrontend/repl.py @@ -1139,9 +1139,10 @@ def move_current_stdouterr_line_up(self): def send_to_stdout(self, output): """Send unicode string to Repl stdout""" - if not output: return + if not output: + return lines = output.split('\n') - if output == len(output) * '\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] @@ -1165,9 +1166,10 @@ def send_to_stderr(self, error): Must be able to handle FmtStrs because interpreter pass in tracebacks already formatted.""" - if not error: return + if not error: + return lines = error.split('\n') - if error == len(error) * '\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]