Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion Lib/_pyrepl/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def get_prompt(self, lineno: int, cursor_on_line: bool) -> str:
prompt = self.ps1

if self.can_colorize:
t = THEME()
t = THEME(force_color=self.can_colorize)
prompt = f"{t.prompt}{prompt}{t.reset}"
return prompt

Expand Down
5 changes: 3 additions & 2 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def __init__(
self.pollob.register(self.input_fd, select.POLLIN)
self.terminfo = terminfo.TermInfo(term or None)
self.term = term
self.is_mac = platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal"

@overload
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
Expand Down Expand Up @@ -339,7 +340,7 @@ def prepare(self):
tcsetattr(self.input_fd, termios.TCSADRAIN, raw)

# In macOS terminal we need to deactivate line wrap via ANSI escape code
if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_mac:
os.write(self.output_fd, b"\033[?7l")

self.screen = []
Expand Down Expand Up @@ -370,7 +371,7 @@ def restore(self):
self.flushoutput()
tcsetattr(self.input_fd, termios.TCSADRAIN, self.__svtermstate)

if platform.system() == "Darwin" and os.getenv("TERM_PROGRAM") == "Apple_Terminal":
if self.is_mac:
os.write(self.output_fd, b"\033[?7h")

if hasattr(self, "old_sigwinch"):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,19 @@ def test_getheightwidth_with_invalid_environ(self, _os_write):
self.assertIsInstance(console.getheightwidth(), tuple)
os.environ = []
self.assertIsInstance(console.getheightwidth(), tuple)

@unittest.skipUnless(sys.platform == "darwin", "requires OS X")
def test_is_mac_with_invalid_environ(self, _os_write):
# gh-128636 for macOS
console = UnixConsole(term="xterm")
with os_helper.EnvironmentVarGuard() as env:
is_mac = os.getenv("TERM_PROGRAM") == "Apple_Terminal"
self.assertEqual(console.is_mac, is_mac)
os.environ = []
console.prepare() # Need to prepare before restore
try:
console.restore() # This should not crash
except (KeyError, AttributeError, TypeError) as e:
self.fail(f"restore() failed with invalid environ: {e}")

self.assertEqual(console.is_mac, is_mac)
Loading