Skip to content

Fix newline handling in stdout and stderr #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 28, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix newline handling in stdout and stderr
  • Loading branch information
Attila Szöllősi committed May 27, 2017
commit 084402aa7243c57b92690e02f734078cfe02918b
31 changes: 23 additions & 8 deletions bpython/curtsiesfrontend/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add break the line after :.

lines = output.split('\n')
if output == len(output) * '\n':
# If the string consist only of newline characters,
# str.split returns one more empty strings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since at this point split was already called, I'd feel that if all(len(line) == 0 for line in lines): would be nicer.

Copy link
Contributor Author

@ata2001 ata2001 May 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about if all(not line for line in lines):?

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))
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add break the line after :.

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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the other splitcomment.

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]), []))
Expand Down