Skip to content
Open
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
11 changes: 9 additions & 2 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,6 @@ def _my_getstr(cap: str, optional: bool = False) -> bytes | None:
)
self.cursor_visible = True

signal.signal(signal.SIGCONT, self._sigcont_handler)

def _sigcont_handler(self, signum, frame):
self.restore()
self.prepare()
Expand Down Expand Up @@ -505,6 +503,7 @@ def prepare(self) -> None:

try:
self.old_sigwinch = signal.signal(signal.SIGWINCH, self.__sigwinch)
signal.signal(signal.SIGCONT, self._sigcont_handler)
except ValueError:
pass

Expand All @@ -515,6 +514,9 @@ def restore(self) -> None:
Restore the console to the default state
"""
trace("unix.restore")
if not hasattr(self, "_UnixConsole__svtermstate"):
# prepare() never ran, so there is nothing to restore
return
self.__write(ANSIColors.RESET)
self.__disable_bracketed_paste()
self.__maybe_write_code(self._rmkx)
Expand All @@ -533,6 +535,11 @@ def restore(self) -> None:
raise e
del self.old_sigwinch

try:
signal.signal(signal.SIGCONT, signal.SIG_DFL)
except ValueError:
pass

def push_char(self, char: int | bytes) -> None:
"""
Push a character to the console event queue.
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,27 @@ def test_restore_in_thread(self, _os_write):
thread.start()
thread.join() # this should not raise

@unittest.skipUnless(hasattr(signal, "SIGCONT"), "needs signal.SIGCONT")
def test_sigcont_is_armed_only_by_prepare(self, _os_write):
# gh-161254: A console must install its SIGCONT handler only while it
# owns the terminal, i.e. between prepare() and restore().
signal.signal(signal.SIGCONT, signal.SIG_DFL)
console = UnixConsole(term="xterm")
self.assertEqual(signal.getsignal(signal.SIGCONT), signal.SIG_DFL)
console.prepare()
self.assertEqual(
signal.getsignal(signal.SIGCONT), console._sigcont_handler
)
console.restore()
self.assertEqual(signal.getsignal(signal.SIGCONT), signal.SIG_DFL)

@unittest.skipUnless(hasattr(signal, "SIGCONT"), "needs signal.SIGCONT")
def test_restore_without_prepare(self, _os_write):
# gh-161254
console = UnixConsole(term="xterm")
self.assertFalse(hasattr(console, "_UnixConsole__svtermstate"))
console.restore() # this should not raise


@unittest.skipIf(sys.platform == "win32", "No Unix console on Windows")
class TestUnixConsoleEIOHandling(TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when suspending (:kbd:`Ctrl-Z`) and resuming (:kbd:`fg`) an
embedded :func:`code.interact` session after enabling interactive mode.
Loading