Skip to content

gh-128067: Add newline to new REPL output when needed to avoid chopping characters #131011

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from abc import ABC, abstractmethod
import ast
import code
import io
from dataclasses import dataclass, field
import os.path
import sys
Expand Down Expand Up @@ -177,7 +178,19 @@ def _excepthook(self, typ, value, tb):

def runcode(self, code):
try:
temp_output = io.StringIO()
old_stdout = sys.stdout
sys.stdout = temp_output
exec(code, self.locals)
output = ""
if hasattr(sys.stdout, "getvalue"):
output = sys.stdout.getvalue()
# Avoid restoring old stdout if it has been changed during exec
if sys.stdout is temp_output:
sys.stdout = old_stdout
if not output.endswith("\n"):
output += "\n"
print(output, end="")
except SystemExit:
raise
except BaseException:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,16 @@ def test_keyboard_interrupt_after_isearch(self):
output, exit_code = self.run_repl(["\x12", "\x03", "exit"])
self.assertEqual(exit_code, 0)

def test_newline_after_print_ending_in_space(self):
commands = ("print('abcdefg' * 250 + 'Z', end=' ')\n"
"exit()\n")
output, exit_code = self.run_repl(commands)
self.assertEqual(exit_code, 0)
print(output)
self.assertIn("Z \r\n", output)
expected = "abcdefg" * 200 + "Z " + "\r\n"
self.assertIn(expected, output)

def test_prompt_after_help(self):
output, exit_code = self.run_repl(["help", "q", "exit"])

Expand Down
Loading