From ab34b8ea72e6168b243e1b9b24fc2c53104ac74d Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 23 Aug 2023 12:57:05 +0200 Subject: [PATCH] test_audit: Test "time.sleep" hook raising an exception --- Lib/test/audit-tests.py | 7 +++++-- Lib/test/test_audit.py | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py index 737417b2e54ece6..74152e3a45bd4dd 100644 --- a/Lib/test/audit-tests.py +++ b/Lib/test/audit-tests.py @@ -518,12 +518,15 @@ def test_not_in_gc(): assert hook not in o -def test_time(): +def test_time(mode): import time def hook(event, args): if event.startswith("time."): - print(event, *args) + if mode == 'print': + print(event, *args) + elif mode == 'fail': + raise AssertionError('hook failed') sys.addaudithook(hook) time.sleep(0) diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py index eac967ec33c7b89..ee5b8e8a8647460 100644 --- a/Lib/test/test_audit.py +++ b/Lib/test/test_audit.py @@ -256,7 +256,7 @@ def test_not_in_gc(self): self.fail(stderr) def test_time(self): - returncode, events, stderr = self.run_python("test_time") + returncode, events, stderr = self.run_python("test_time", "print") if returncode: self.fail(stderr) @@ -270,6 +270,11 @@ def test_time(self): self.assertEqual(actual, expected) + def test_time_fail(self): + returncode, events, stderr = self.run_python("test_time", "fail", + expect_stderr=True) + self.assertNotEqual(returncode, 0) + self.assertIn('hook failed', stderr.splitlines()[-1]) def test_sys_monitoring_register_callback(self): returncode, events, stderr = self.run_python("test_sys_monitoring_register_callback")