summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_audit.py
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@python.org>2019-11-28 16:46:11 (GMT)
committerGitHub <noreply@github.com>2019-11-28 16:46:11 (GMT)
commitbea33f5e1db6e4a554919a82894f44568576e979 (patch)
tree37789378933d7091eb0012d6ad3277aa8685a979 /Lib/test/test_audit.py
parent02519f75d15b063914a11351da30178ca4ceb54b (diff)
downloadcpython-bea33f5e1db6e4a554919a82894f44568576e979.zip
cpython-bea33f5e1db6e4a554919a82894f44568576e979.tar.gz
cpython-bea33f5e1db6e4a554919a82894f44568576e979.tar.bz2
bpo-38920: Add audit hooks for when sys.excepthook and sys.unraisable hooks are invoked (GH-17392)
Also fixes some potential segfaults in unraisable hook handling.
Diffstat (limited to 'Lib/test/test_audit.py')
-rw-r--r--Lib/test/test_audit.py56
1 files changed, 42 insertions, 14 deletions
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index 41f9fae..31a0855 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -24,7 +24,23 @@ class AuditTest(unittest.TestCase):
sys.stdout.writelines(p.stdout)
sys.stderr.writelines(p.stderr)
if p.returncode:
- self.fail(''.join(p.stderr))
+ self.fail("".join(p.stderr))
+
+ def run_python(self, *args):
+ events = []
+ with subprocess.Popen(
+ [sys.executable, "-X utf8", AUDIT_TESTS_PY, *args],
+ encoding="utf-8",
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ ) as p:
+ p.wait()
+ sys.stderr.writelines(p.stderr)
+ return (
+ p.returncode,
+ [line.strip().partition(" ") for line in p.stdout],
+ "".join(p.stderr),
+ )
def test_basic(self):
self.do_test("test_basic")
@@ -36,19 +52,11 @@ class AuditTest(unittest.TestCase):
self.do_test("test_block_add_hook_baseexception")
def test_finalize_hooks(self):
- events = []
- with subprocess.Popen(
- [sys.executable, "-X utf8", AUDIT_TESTS_PY, "test_finalize_hooks"],
- encoding="utf-8",
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- ) as p:
- p.wait()
- for line in p.stdout:
- events.append(line.strip().partition(" "))
- sys.stderr.writelines(p.stderr)
- if p.returncode:
- self.fail(''.join(p.stderr))
+ returncode, events, stderr = self.run_python("test_finalize_hooks")
+ if stderr:
+ print(stderr, file=sys.stderr)
+ if returncode:
+ self.fail(stderr)
firstId = events[0][2]
self.assertSequenceEqual(
@@ -76,6 +84,26 @@ class AuditTest(unittest.TestCase):
def test_mmap(self):
self.do_test("test_mmap")
+ def test_excepthook(self):
+ returncode, events, stderr = self.run_python("test_excepthook")
+ if not returncode:
+ self.fail(f"Expected fatal exception\n{stderr}")
+
+ self.assertSequenceEqual(
+ [("sys.excepthook", " ", "RuntimeError('fatal-error')")], events
+ )
+
+ def test_unraisablehook(self):
+ returncode, events, stderr = self.run_python("test_unraisablehook")
+ if returncode:
+ self.fail(stderr)
+
+ self.assertEqual(events[0][0], "sys.unraisablehook")
+ self.assertEqual(
+ events[0][2],
+ "RuntimeError('nonfatal-error') Exception ignored for audit hook test",
+ )
+
if __name__ == "__main__":
unittest.main()