|
20 | 20 | from __future__ import print_function
|
21 | 21 |
|
22 | 22 | import os
|
| 23 | +import signal |
23 | 24 | import subprocess
|
24 | 25 | import sys
|
25 | 26 |
|
@@ -68,6 +69,10 @@ def IsInteractive(output=False, error=False, heuristic=False):
|
68 | 69 | return True
|
69 | 70 |
|
70 | 71 |
|
| 72 | +def PreexecFunc(): |
| 73 | + signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 74 | + |
| 75 | + |
71 | 76 | def More(contents, out, prompt=None, check_pager=True):
|
72 | 77 | """Run a user specified pager or fall back to the internal pager.
|
73 | 78 |
|
@@ -97,10 +102,19 @@ def More(contents, out, prompt=None, check_pager=True):
|
97 | 102 | less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None)
|
98 | 103 | less = '-R' + (less_orig or '')
|
99 | 104 | encoding.SetEncodedValue(os.environ, 'LESS', less)
|
100 |
| - p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True) |
| 105 | + # Ignores SIGINT from this point on since the child process has started |
| 106 | + # and we don't want to terminate either one when the child is still alive. |
| 107 | + signal.signal(signal.SIGINT, signal.SIG_IGN) |
| 108 | + # Runs PreexecFunc before starting the child so SIGINT is ignored for the |
| 109 | + # child process as well. |
| 110 | + p = subprocess.Popen( |
| 111 | + pager, stdin=subprocess.PIPE, shell=True, preexec_fn=PreexecFunc) |
101 | 112 | enc = console_attr.GetConsoleAttr().GetEncoding()
|
102 | 113 | p.communicate(input=contents.encode(enc))
|
103 | 114 | p.wait()
|
| 115 | + # Starts using default disposition for SIGINT again after the child has |
| 116 | + # exited. |
| 117 | + signal.signal(signal.SIGINT, signal.SIG_DFL) |
104 | 118 | if less_orig is None:
|
105 | 119 | encoding.SetEncodedValue(os.environ, 'LESS', None)
|
106 | 120 | return
|
|
0 commit comments