summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorƁukasz Langa <lukasz@langa.pl>2020-09-24 14:34:21 (GMT)
committerGitHub <noreply@github.com>2020-09-24 14:34:21 (GMT)
commitca8d46dd422e5aa10f444796d93faec5a8cbc1e0 (patch)
tree0f35307f860c08bd2ed8c65b426b1c47e273bd4c /Lib
parent57e7d5c19e934f450f5a3539eff48162a78af994 (diff)
downloadcpython-ca8d46dd422e5aa10f444796d93faec5a8cbc1e0.zip
cpython-ca8d46dd422e5aa10f444796d93faec5a8cbc1e0.tar.gz
cpython-ca8d46dd422e5aa10f444796d93faec5a8cbc1e0.tar.bz2
[3.9] bpo-41602: raise SIGINT exit code on KeyboardInterrupt from pymain_run_module (GH-21956) (#22397)
Closes bpo issue 41602. (cherry picked from commit a68a2ad19c891faa891904b3da537911cc77df21) Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_runpy.py92
1 files changed, 86 insertions, 6 deletions
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py
index 84834d3..4aaf75b 100644
--- a/Lib/test/test_runpy.py
+++ b/Lib/test/test_runpy.py
@@ -1,14 +1,17 @@
# Test the runpy module
-import unittest
-import os
+import contextlib
+import importlib.machinery, importlib.util
import os.path
-import sys
+import pathlib
+import py_compile
import re
+import signal
+import subprocess
+import sys
import tempfile
-import importlib, importlib.machinery, importlib.util
-import py_compile
+import textwrap
+import unittest
import warnings
-import pathlib
from test.support import (
forget, make_legacy_pyc, unload, verbose, no_tracing,
create_empty_file, temp_dir)
@@ -752,5 +755,82 @@ s = "non-ASCII: h\xe9"
self.assertEqual(result['s'], "non-ASCII: h\xe9")
+class TestExit(unittest.TestCase):
+ STATUS_CONTROL_C_EXIT = 0xC000013A
+ EXPECTED_CODE = (
+ STATUS_CONTROL_C_EXIT
+ if sys.platform == "win32"
+ else -signal.SIGINT
+ )
+ @staticmethod
+ @contextlib.contextmanager
+ def tmp_path(*args, **kwargs):
+ with temp_dir() as tmp_fn:
+ yield pathlib.Path(tmp_fn)
+
+
+ def run(self, *args, **kwargs):
+ with self.tmp_path() as tmp:
+ self.ham = ham = tmp / "ham.py"
+ ham.write_text(
+ textwrap.dedent(
+ """\
+ raise KeyboardInterrupt
+ """
+ )
+ )
+ super().run(*args, **kwargs)
+
+ def assertSigInt(self, *args, **kwargs):
+ proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
+ self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
+ self.assertEqual(proc.returncode, self.EXPECTED_CODE)
+
+ def test_pymain_run_file(self):
+ self.assertSigInt([sys.executable, self.ham])
+
+ def test_pymain_run_file_runpy_run_module(self):
+ tmp = self.ham.parent
+ run_module = tmp / "run_module.py"
+ run_module.write_text(
+ textwrap.dedent(
+ """\
+ import runpy
+ runpy.run_module("ham")
+ """
+ )
+ )
+ self.assertSigInt([sys.executable, run_module], cwd=tmp)
+
+ def test_pymain_run_file_runpy_run_module_as_main(self):
+ tmp = self.ham.parent
+ run_module_as_main = tmp / "run_module_as_main.py"
+ run_module_as_main.write_text(
+ textwrap.dedent(
+ """\
+ import runpy
+ runpy._run_module_as_main("ham")
+ """
+ )
+ )
+ self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
+
+ def test_pymain_run_command_run_module(self):
+ self.assertSigInt(
+ [sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
+ cwd=self.ham.parent,
+ )
+
+ def test_pymain_run_command(self):
+ self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
+
+ def test_pymain_run_stdin(self):
+ self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
+
+ def test_pymain_run_module(self):
+ ham = self.ham
+ self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
+
+
if __name__ == "__main__":
unittest.main()