Skip to content

Commit ec2856c

Browse files
committed
bpo-41602: add tests for sigint handling in runpy
1 parent 8e19c8b commit ec2856c

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

Lib/test/test_runpy.py

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Test the runpy module
2-
import unittest
3-
import os
2+
import contextlib
3+
import importlib.machinery, importlib.util
44
import os.path
5-
import sys
5+
import pathlib
6+
import py_compile
67
import re
8+
import subprocess
9+
import sys
710
import tempfile
8-
import importlib, importlib.machinery, importlib.util
9-
import py_compile
11+
import textwrap
12+
import unittest
1013
import warnings
11-
import pathlib
12-
from test.support import verbose, no_tracing
14+
from test.support import no_tracing, verbose
1315
from test.support.import_helper import forget, make_legacy_pyc, unload
1416
from test.support.os_helper import create_empty_file, temp_dir
1517
from test.support.script_helper import make_script, make_zip_script
@@ -752,5 +754,76 @@ def test_encoding(self):
752754
self.assertEqual(result['s'], "non-ASCII: h\xe9")
753755

754756

757+
class TestExit(unittest.TestCase):
758+
@staticmethod
759+
@contextlib.contextmanager
760+
def tmp_path(*args, **kwargs):
761+
with temp_dir() as tmp_fn:
762+
yield pathlib.Path(tmp_fn)
763+
764+
765+
def run(self, *args, **kwargs):
766+
with self.tmp_path() as tmp:
767+
self.ham = ham = tmp / "ham.py"
768+
ham.write_text(
769+
textwrap.dedent(
770+
"""\
771+
raise KeyboardInterrupt
772+
"""
773+
)
774+
)
775+
super().run(*args, **kwargs)
776+
777+
def assertSigInt(self, *args, **kwargs):
778+
proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
779+
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
780+
self.assertEqual(proc.returncode, -2)
781+
782+
def test_pymain_run_file(self):
783+
self.assertSigInt([sys.executable, self.ham])
784+
785+
def test_pymain_run_file_runpy_run_module(self):
786+
tmp = self.ham.parent
787+
run_module = tmp / "run_module.py"
788+
run_module.write_text(
789+
textwrap.dedent(
790+
"""\
791+
import runpy
792+
runpy.run_module("ham")
793+
"""
794+
)
795+
)
796+
self.assertSigInt([sys.executable, run_module], cwd=tmp)
797+
798+
def test_pymain_run_file_runpy_run_module_as_main(self):
799+
tmp = self.ham.parent
800+
run_module_as_main = tmp / "run_module_as_main.py"
801+
run_module_as_main.write_text(
802+
textwrap.dedent(
803+
"""\
804+
import runpy
805+
runpy._run_module_as_main("ham")
806+
"""
807+
)
808+
)
809+
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
810+
811+
def test_pymain_run_command_run_module(self):
812+
self.assertSigInt(
813+
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
814+
cwd=self.ham.parent,
815+
)
816+
817+
def test_pymain_run_command(self):
818+
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
819+
820+
def test_pymain_run_stdin(self):
821+
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
822+
823+
def test_pymain_run_module(self):
824+
ham = self.ham
825+
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
826+
827+
755828
if __name__ == "__main__":
756829
unittest.main()

0 commit comments

Comments
 (0)