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

Skip to content

Commit dc93d11

Browse files
authored
gh-121957: Emit audit events for python -i and python -m asyncio (GH-121958)
Relatedly, emit the `cpython.run_startup` event from the Python version of `PYTHONSTARTUP` handling.
1 parent cad11a2 commit dc93d11

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

Doc/library/asyncio.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ Additionally, there are **low-level** APIs for
5656
* :ref:`bridge <asyncio-futures>` callback-based libraries and code
5757
with async/await syntax.
5858

59+
.. include:: ../includes/wasm-notavail.rst
60+
5961
.. _asyncio-cli:
6062

61-
You can experiment with an ``asyncio`` concurrent context in the REPL:
63+
.. rubric:: asyncio REPL
64+
65+
You can experiment with an ``asyncio`` concurrent context in the :term:`REPL`:
6266

6367
.. code-block:: pycon
6468
@@ -70,7 +74,14 @@ You can experiment with an ``asyncio`` concurrent context in the REPL:
7074
>>> await asyncio.sleep(10, result='hello')
7175
'hello'
7276
73-
.. include:: ../includes/wasm-notavail.rst
77+
.. audit-event:: cpython.run_stdin "" ""
78+
79+
.. versionchanged:: 3.12.5 (also 3.11.10, 3.10.15, 3.9.20, and 3.8.20)
80+
Emits audit events.
81+
82+
.. versionchanged:: 3.13
83+
Uses PyREPL if possible, in which case :envvar:`PYTHONSTARTUP` is
84+
also executed. Emits audit events.
7485

7586
.. We use the "rubric" directive here to avoid creating
7687
the "Reference" subsection in the TOC.

Doc/using/cmdline.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,15 @@ conflict.
793793
This variable can also be modified by Python code using :data:`os.environ`
794794
to force inspect mode on program termination.
795795

796+
.. audit-event:: cpython.run_stdin "" ""
797+
798+
.. versionchanged:: 3.12.5 (also 3.11.10, 3.10.15, 3.9.20, and 3.8.20)
799+
Emits audit events.
800+
801+
.. versionchanged:: 3.13
802+
Uses PyREPL if possible, in which case :envvar:`PYTHONSTARTUP` is
803+
also executed. Emits audit events.
804+
796805

797806
.. envvar:: PYTHONUNBUFFERED
798807

Lib/_pyrepl/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def interactive_console(mainmodule=None, quiet=False, pythonstartup=False):
3939
# sys._baserepl() above does this internally, we do it here
4040
startup_path = os.getenv("PYTHONSTARTUP")
4141
if pythonstartup and startup_path:
42+
sys.audit("cpython.run_startup", startup_path)
43+
4244
import tokenize
4345
with tokenize.open(startup_path) as f:
4446
startup_code = compile(f.read(), startup_path, "exec")

Lib/asyncio/__main__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def run(self):
9191
console.write(banner)
9292

9393
if startup_path := os.getenv("PYTHONSTARTUP"):
94+
sys.audit("cpython.run_startup", startup_path)
95+
9496
import tokenize
9597
with tokenize.open(startup_path) as f:
9698
startup_code = compile(f.read(), startup_path, "exec")
@@ -127,6 +129,8 @@ def run(self):
127129

128130

129131
if __name__ == '__main__':
132+
sys.audit("cpython.run_stdin")
133+
130134
if os.getenv('PYTHON_BASIC_REPL'):
131135
CAN_USE_PYREPL = False
132136
else:
@@ -155,6 +159,7 @@ def run(self):
155159
interactive_hook = getattr(sys, "__interactivehook__", None)
156160

157161
if interactive_hook is not None:
162+
sys.audit("cpython.run_interactivehook", interactive_hook)
158163
interactive_hook()
159164

160165
if interactive_hook is site.register_readline:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed missing audit events around interactive use of Python, now also
2+
properly firing for ``python -i``, as well as for ``python -m asyncio``. The
3+
events in question are ``cpython.run_stdin`` and ``cpython.run_startup``.

Modules/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,10 @@ pymain_repl(PyConfig *config, int *exitcode)
594594
return;
595595
}
596596

597+
if (PySys_Audit("cpython.run_stdin", NULL) < 0) {
598+
return;
599+
}
600+
597601
if (!isatty(fileno(stdin))
598602
|| _Py_GetEnv(config->use_environment, "PYTHON_BASIC_REPL")) {
599603
PyCompilerFlags cf = _PyCompilerFlags_INIT;

0 commit comments

Comments
 (0)