Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.13] gh-121957: Emit audit events for python -i and python -m asyncio (GH-121958) #122115

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
Jul 22, 2024
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
15 changes: 13 additions & 2 deletions Doc/library/asyncio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ Additionally, there are **low-level** APIs for
* :ref:`bridge <asyncio-futures>` callback-based libraries and code
with async/await syntax.

.. include:: ../includes/wasm-notavail.rst

.. _asyncio-cli:

You can experiment with an ``asyncio`` concurrent context in the REPL:
.. rubric:: asyncio REPL

You can experiment with an ``asyncio`` concurrent context in the :term:`REPL`:

.. code-block:: pycon

Expand All @@ -70,7 +74,14 @@ You can experiment with an ``asyncio`` concurrent context in the REPL:
>>> await asyncio.sleep(10, result='hello')
'hello'

.. include:: ../includes/wasm-notavail.rst
.. audit-event:: cpython.run_stdin "" ""

.. versionchanged:: 3.12.5 (also 3.11.10, 3.10.15, 3.9.20, and 3.8.20)
Emits audit events.

.. versionchanged:: 3.13
Uses PyREPL if possible, in which case :envvar:`PYTHONSTARTUP` is
also executed. Emits audit events.

.. We use the "rubric" directive here to avoid creating
the "Reference" subsection in the TOC.
Expand Down
9 changes: 9 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,15 @@ conflict.
This variable can also be modified by Python code using :data:`os.environ`
to force inspect mode on program termination.

.. audit-event:: cpython.run_stdin "" ""

.. versionchanged:: 3.12.5 (also 3.11.10, 3.10.15, 3.9.20, and 3.8.20)
Emits audit events.

.. versionchanged:: 3.13
Uses PyREPL if possible, in which case :envvar:`PYTHONSTARTUP` is
also executed. Emits audit events.


.. envvar:: PYTHONUNBUFFERED

Expand Down
2 changes: 2 additions & 0 deletions Lib/_pyrepl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
# sys._baserepl() above does this internally, we do it here
startup_path = os.getenv("PYTHONSTARTUP")
if pythonstartup and startup_path:
sys.audit("cpython.run_startup", startup_path)

import tokenize
with tokenize.open(startup_path) as f:
startup_code = compile(f.read(), startup_path, "exec")
Expand Down
5 changes: 5 additions & 0 deletions Lib/asyncio/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def run(self):
console.write(banner)

if startup_path := os.getenv("PYTHONSTARTUP"):
sys.audit("cpython.run_startup", startup_path)

import tokenize
with tokenize.open(startup_path) as f:
startup_code = compile(f.read(), startup_path, "exec")
Expand Down Expand Up @@ -127,6 +129,8 @@ def run(self):


if __name__ == '__main__':
sys.audit("cpython.run_stdin")

if os.getenv('PYTHON_BASIC_REPL'):
CAN_USE_PYREPL = False
else:
Expand Down Expand Up @@ -155,6 +159,7 @@ def run(self):
interactive_hook = getattr(sys, "__interactivehook__", None)

if interactive_hook is not None:
sys.audit("cpython.run_interactivehook", interactive_hook)
interactive_hook()

if interactive_hook is site.register_readline:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed missing audit events around interactive use of Python, now also
properly firing for ``python -i``, as well as for ``python -m asyncio``. The
events in question are ``cpython.run_stdin`` and ``cpython.run_startup``.
4 changes: 4 additions & 0 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ pymain_repl(PyConfig *config, int *exitcode)
return;
}

if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
return;
}

if (!isatty(fileno(stdin))
|| _Py_GetEnv(config->use_environment, "PYTHON_BASIC_REPL")) {
PyCompilerFlags cf = _PyCompilerFlags_INIT;
Expand Down
Loading