Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1058cda

Browse files
committed
#23792: Ignore KeyboardInterrupt when the pydoc pager is active.
Previously, if you hit ctl-c while the pager was active, the python that launched the subprocess for the pager would see the KeyboardInterrupt in the __exit__ method of the subprocess context manager where it was waiting for the subprocess to complete, ending the wait. This would leave the pager running, while the interactive interpreter, after handling the exception by printing it, would go back to trying to post a prompt...but the pager would generally have the terminal in raw mode, and in any case would be still trying to read from stdin. On some systems, even exiting python at that point would not restore the terminal mode. The problem with raw mode could also happen if ctl-C was hit when pydoc was called from the shell command line and the pager was active. Instead, we now wait on the subprocess in a loop, ignoring KeyboardInterrupt just like the pager does, until the pager actually exits. (Note: this was a regression relative to python2...in python2 the pager is called via system, and system does not return until the pager exits.)
1 parent 48070c1 commit 1058cda

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/pydoc.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,11 +1452,18 @@ def pipepager(text, cmd):
14521452
import subprocess
14531453
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
14541454
try:
1455-
with proc:
1456-
with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
1457-
pipe.write(text)
1455+
with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
1456+
pipe.write(text)
14581457
except OSError:
14591458
pass # Ignore broken pipes caused by quitting the pager program.
1459+
while True:
1460+
try:
1461+
proc.wait()
1462+
break
1463+
except KeyboardInterrupt:
1464+
# Ignore ctl-c like the pager itself does. Otherwise the pager is
1465+
# left running and the terminal is in raw mode and unusable.
1466+
pass
14601467

14611468
def tempfilepager(text, cmd):
14621469
"""Page through text by invoking a program on a temporary file."""

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #23792: Ignore KeyboardInterrupt when the pydoc pager is active.
25+
This mimics the behavior of the standard unix pagers, and prevents
26+
pipepager from shutting down while the pager itself is still running.
27+
2428
- Issue #23742: ntpath.expandvars() no longer loses unbalanced single quotes.
2529

2630
- Issue #21802: The reader in BufferedRWPair now is closed even when closing

0 commit comments

Comments
 (0)