8000 Ignoring SIGINT in pager process and fire process while the pager pro… · randomprin/python-fire@074f4b2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 074f4b2

Browse files
joejoevictorcopybara-github
authored andcommitted
Ignoring SIGINT in pager process and fire process while the pager process is alive.
PiperOrigin-RevId: 290290435 Change-Id: Ia34edcce58619be8a7ff4296a388b5b6a46c8544
1 parent cd95ae2 commit 074f4b2

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

fire/console/console_io.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from __future__ import print_function
2121

2222
import os
23+
import signal
2324
import subprocess
2425
import sys
2526

@@ -68,6 +69,10 @@ def IsInteractive(output=False, error=False, heuristic=False):
6869
return True
6970

7071

72+
def PreexecFunc():
73+
signal.signal(signal.SIGINT, signal.SIG_IGN)
74+
75+
7176
def More(contents, out, prompt=None, check_pager=True):
7277
"""Run a user specified pager or fall back to the internal pager.
7378
@@ -97,10 +102,19 @@ def More(contents, out, prompt=None, check_pager=True):
97102
less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None)
98103
less = '-R' + (less_orig or '')
99104
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)
101112
enc = console_attr.GetConsoleAttr().GetEncoding()
102113
p.communicate(input=contents.encode(enc))
103114
p.wait()
115+
# Starts using default disposition for SIGINT again after the child has
116+
# exited.
117+
signal.signal(signal.SIGINT, signal.SIG_DFL)
104118
if less_orig is None:
105119
encoding.SetEncodedValue(os.environ, 'LESS', None)
106120
return

0 commit comments

Comments
 (0)
0