Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-41602: raise SIGINT exit code on KeyboardInterrupt from pymain_run_module #21956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 22, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-41602: add tests for sigint handling in runpy
  • Loading branch information
graingert committed Aug 25, 2020
commit ec2856cb052c0b61645cea81dab0ac605560cd29
87 changes: 80 additions & 7 deletions 87 Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
@@ -1,15 +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 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 verbose, no_tracing
from test.support import no_tracing, verbose
from test.support.import_helper import forget, make_legacy_pyc, unload
from test.support.os_helper import create_empty_file, temp_dir
from test.support.script_helper import make_script, make_zip_script
Expand Down Expand Up @@ -752,5 +754,76 @@ def test_encoding(self):
self.assertEqual(result['s'], "non-ASCII: h\xe9")


class TestExit(unittest.TestCase):
@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, -2)

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):
graingert marked this conversation as resolved.
Show resolved Hide resolved
ham = self.ham
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)


if __name__ == "__main__":
unittest.main()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.