mirror of
https://github.com/python/cpython
synced 2024-09-06 13:20:04 +00:00
gh-108294: Add time.sleep audit event (GH-108298)
This commit is contained in:
parent
2dfbd4f36d
commit
31b61d19ab
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
:func:`time.sleep` now raises an auditing event.
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue