10000 gh-108294: Add time.sleep audit event by encukou · Pull Request #108298 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-108294: Add time.sleep audit event #108298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -389,6 +391,9 @@ Functions
:pep:`475` for the rationale).


.. versionchanged:: 3.13
Raises an auditing event.

.. index::
single: % (percent); datetime format

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/audit-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`time.sleep` now raises an auditing event.
2 changes: 2 additions & 0 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no error handling?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could tell you but it would sound like a lame excuse. (I picked up a low-priority task from May, and didn't realize how much work-in-progress it was...)

Thank you very much for spotting this! Fix incoming.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix in #108363.


_PyTime_t timeout;
if (_PyTime_FromSecondsObject(&timeout, timeout_obj, _PyTime_ROUND_TIMEOUT))
return NULL;
Expand Down
0