]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-108294: Add time.sleep audit event (GH-108298)
authorPetr Viktorin <encukou@gmail.com>
Wed, 23 Aug 2023 09:00:22 +0000 (11:00 +0200)
committerGitHub <noreply@github.com>
Wed, 23 Aug 2023 09:00:22 +0000 (11:00 +0200)
Doc/library/time.rst
Lib/test/audit-tests.py
Lib/test/test_audit.py
Misc/NEWS.d/next/Library/2023-08-22-16-18-49.gh-issue-108294.KEeUcM.rst [new file with mode: 0644]
Modules/timemodule.c

index 9f23a6fc7d534130d1f9cfedd3446648d4c09ac7..6ffe4ac4284140ac82b307754c2541b22ea26a44 100644 (file)
@@ -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
 
index 9504829e96f00e2bf5a1040a2d2b49a04852e255..cc614eab908500ac5ca7aeec7fbef31513c5e172 100644 (file)
@@ -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
 
index b12ffa5d872e8334118ec3bf465d0d19507e8182..3a15835917cc327ce127cb683ad760acd2f8f416 100644 (file)
@@ -256,6 +256,21 @@ class AuditTest(unittest.TestCase):
         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 (file)
index 0000000..de2a3a8
--- /dev/null
@@ -0,0 +1 @@
+:func:`time.sleep` now raises an auditing event.
index 912710219bd01416398dd2135c1c27bc580e9ebd..68948b6be1a61a2e4d8786723e141e9b39b908ab 100644 (file)
@@ -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;