[Python-checkins] gh-108294: Add time.sleep audit event (GH-108298)
encukou
webhook-mailer at python.org
Wed Aug 23 05:00:26 EDT 2023
https://github.com/python/cpython/commit/31b61d19abcc63aa28625a31ed75411948fc1e7e
commit: 31b61d19abcc63aa28625a31ed75411948fc1e7e
branch: main
author: Petr Viktorin <encukou at gmail.com>
committer: encukou <encukou at gmail.com>
date: 2023-08-23T11:00:22+02:00
summary:
gh-108294: Add time.sleep audit event (GH-108298)
files:
A Misc/NEWS.d/next/Library/2023-08-22-16-18-49.gh-issue-108294.KEeUcM.rst
M Doc/library/time.rst
M Lib/test/audit-tests.py
M Lib/test/test_audit.py
M Modules/timemodule.c
diff --git a/Doc/library/time.rst b/Doc/library/time.rst
index 9f23a6fc7d534..6ffe4ac428414 100644
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -379,6 +379,8 @@ Functions
* Or use ``nanosleep()`` if available (resolution: 1 nanosecond);
* Or use ``select()`` (resolution: 1 microsecond).
+ .. audit-event:: time.sleep secs
+
.. versionchanged:: 3.11
On Unix, the ``clock_nanosleep()`` and ``nanosleep()`` functions are now
used if available. On Windows, a waitable timer is now used.
@@ -389,6 +391,9 @@ Functions
:pep:`475` for the rationale).
+ .. versionchanged:: 3.13
+ Raises an auditing event.
+
.. index::
single: % (percent); datetime format
diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index 9504829e96f00..cc614eab90850 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -514,6 +514,21 @@ def test_not_in_gc():
assert hook not in o
+def test_time():
+ import time
+
+ def hook(event, args):
+ if event.startswith("time."):
+ print(event, *args)
+ sys.addaudithook(hook)
+
+ time.sleep(0)
+ time.sleep(0.0625) # 1/16, a small exact float
+ try:
+ time.sleep(-1)
+ except ValueError:
+ pass
+
def test_sys_monitoring_register_callback():
import sys
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index b12ffa5d872e8..3a15835917cc3 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -256,6 +256,21 @@ def test_not_in_gc(self):
if returncode:
self.fail(stderr)
+ def test_time(self):
+ returncode, events, stderr = self.run_python("test_time")
+ if returncode:
+ self.fail(stderr)
+
+ if support.verbose:
+ print(*events, sep='\n')
+
+ actual = [(ev[0], ev[2]) for ev in events]
+ expected = [("time.sleep", "0"),
+ ("time.sleep", "0.0625"),
+ ("time.sleep", "-1")]
+
+ self.assertEqual(actual, expected)
+
def test_sys_monitoring_register_callback(self):
returncode, events, stderr = self.run_python("test_sys_monitoring_register_callback")
diff --git a/Misc/NEWS.d/next/Library/2023-08-22-16-18-49.gh-issue-108294.KEeUcM.rst b/Misc/NEWS.d/next/Library/2023-08-22-16-18-49.gh-issue-108294.KEeUcM.rst
new file mode 100644
index 0000000000000..de2a3a8a8ad89
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-08-22-16-18-49.gh-issue-108294.KEeUcM.rst
@@ -0,0 +1 @@
+:func:`time.sleep` now raises an auditing event.
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 912710219bd01..68948b6be1a61 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -414,6 +414,8 @@ Return the clk_id of a thread's CPU time clock.");
static PyObject *
time_sleep(PyObject *self, PyObject *timeout_obj)
{
+ PySys_Audit("time.sleep", "O", timeout_obj);
+
_PyTime_t timeout;
if (_PyTime_FromSecondsObject(&timeout, timeout_obj, _PyTime_ROUND_TIMEOUT))
return NULL;
More information about the Python-checkins
mailing list