Skip to content

Commit 06dbe73

Browse files
authored
Let subprocess launched by run handle its own sigint (#246)
Otherwise, the subprocess is aborted by the parent process. This change makes `spin run python -i ...` usable. Tested on Windows, but on that platform no changes seem to be necessary. Closes #235
1 parent 5e91741 commit 06dbe73

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

spin/cmds/meson.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import re
66
import shutil
7+
import signal
78
import sys
89
from enum import Enum
910
from pathlib import Path
@@ -815,7 +816,27 @@ def run(ctx, *, args, build_dir=None):
815816
cmd_args = ["bash", "-c", cmd_args]
816817

817818
_set_pythonpath(build_dir, quiet=True)
818-
p = _run(cmd_args, echo=False, shell=shell, sys_exit=False)
819+
820+
is_windows = sys.platform == "win32"
821+
822+
if not is_windows:
823+
# Let the subprocess handle its own signals
824+
# Except on Windows, where it already seems to work as intended,
825+
# and `preexec_fn` is not supported
826+
signal.signal(signal.SIGINT, signal.SIG_IGN)
827+
828+
def attach_sigint():
829+
# Reset SIGINT handler to default
830+
signal.signal(signal.SIGINT, signal.SIG_DFL)
831+
832+
# --- launch subprocess ---
833+
p = _run(
834+
cmd_args,
835+
echo=False,
836+
shell=shell,
837+
sys_exit=False,
838+
preexec_fn=None if is_windows else attach_sigint,
839+
)
819840

820841
# Is the user trying to run a Python script, without calling the Python interpreter?
821842
executable = args[0]

0 commit comments

Comments
 (0)