summaryrefslogtreecommitdiffstats
path: root/Lib/pydoc.py
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2015-03-29 19:15:40 (GMT)
committerR David Murray <rdmurray@bitdance.com>2015-03-29 19:15:40 (GMT)
commit1058cda38f1b409c4d52eef236f4915df592a112 (patch)
treec7bc75516c0498e01966e1f0136764b2e2d53592 /Lib/pydoc.py
parent48070c1248c97238493025553fe474ad9eca168d (diff)
downloadcpython-1058cda38f1b409c4d52eef236f4915df592a112.zip
cpython-1058cda38f1b409c4d52eef236f4915df592a112.tar.gz
cpython-1058cda38f1b409c4d52eef236f4915df592a112.tar.bz2
#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.)
Diffstat (limited to 'Lib/pydoc.py')
-rwxr-xr-xLib/pydoc.py13
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 8bbebc9..cf9e0f0 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1452,11 +1452,18 @@ def pipepager(text, cmd):
import subprocess
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE)
try:
- with proc:
- with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
- pipe.write(text)
+ with io.TextIOWrapper(proc.stdin, errors='backslashreplace') as pipe:
+ pipe.write(text)
except OSError:
pass # Ignore broken pipes caused by quitting the pager program.
+ while True:
+ try:
+ proc.wait()
+ break
+ except KeyboardInterrupt:
+ # Ignore ctl-c like the pager itself does. Otherwise the pager is
+ # left running and the terminal is in raw mode and unusable.
+ pass
def tempfilepager(text, cmd):
"""Page through text by invoking a program on a temporary file."""