[Python-checkins] gh-91348: Restore frame argument to sys._getframe audit event (GH-94928)

miss-islington webhook-mailer at python.org
Sun Jul 17 11:50:06 EDT 2022


https://github.com/python/cpython/commit/1c63734217be75c9fd024ba8fad28ccb108b4d55
commit: 1c63734217be75c9fd024ba8fad28ccb108b4d55
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年07月17日T08:49:57-07:00
summary:
gh-91348: Restore frame argument to sys._getframe audit event (GH-94928)
(cherry picked from commit 044a593cbbe1639e906e06c47504dd1020ddfee4)
Co-authored-by: Steve Dower <steve.dower at python.org>
files:
M Doc/library/sys.rst
M Lib/test/audit-tests.py
M Lib/test/test_audit.py
M Python/sysmodule.c
diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst
index d9799f8358c26..e10efb10ff5a8 100644
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -774,7 +774,7 @@ always available.
 that is deeper than the call stack, :exc:`ValueError` is raised. The default
 for *depth* is zero, returning the frame at the top of the call stack.
 
- .. audit-event:: sys._getframe "" sys._getframe
+ .. audit-event:: sys._getframe frame sys._getframe
 
 .. impl-detail::
 
diff --git a/Lib/test/audit-tests.py b/Lib/test/audit-tests.py
index ccec9fedc4460..00333cc9036a3 100644
--- a/Lib/test/audit-tests.py
+++ b/Lib/test/audit-tests.py
@@ -408,6 +408,17 @@ def hook(event, *args):
 raise RuntimeError("Expected sqlite3.load_extension to fail")
 
 
+def test_sys_getframe():
+ import sys
+
+ def hook(event, args):
+ if event.startswith("sys."):
+ print(event, args[0].f_code.co_name)
+
+ sys.addaudithook(hook)
+ sys._getframe()
+
+
 if __name__ == "__main__":
 from test.support import suppress_msvcrt_asserts
 
diff --git a/Lib/test/test_audit.py b/Lib/test/test_audit.py
index 0fa2d74835cba..6a025f39912e6 100644
--- a/Lib/test/test_audit.py
+++ b/Lib/test/test_audit.py
@@ -176,5 +176,17 @@ def test_sqlite3(self):
 self.assertEqual(actual, expected)
 
 
+ def test_sys_getframe(self):
+ returncode, events, stderr = self.run_python("test_sys_getframe")
+ if returncode:
+ self.fail(stderr)
+
+ if support.verbose:
+ print(*events, sep='\n')
+ actual = [(ev[0], ev[2]) for ev in events]
+ expected = [("sys._getframe", "test_sys_getframe")]
+
+ self.assertEqual(actual, expected)
+
 if __name__ == "__main__":
 unittest.main()
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 261ebb96ba285..e45a26404581c 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1780,10 +1780,6 @@ sys__getframe_impl(PyObject *module, int depth)
 PyThreadState *tstate = _PyThreadState_GET();
 _PyInterpreterFrame *frame = tstate->cframe->current_frame;
 
- if (_PySys_Audit(tstate, "sys._getframe", NULL) < 0) {
- return NULL;
- }
-
 if (frame != NULL) {
 while (depth > 0) {
 frame = frame->previous;
@@ -1801,7 +1797,13 @@ sys__getframe_impl(PyObject *module, int depth)
 "call stack is not deep enough");
 return NULL;
 }
- return _Py_XNewRef((PyObject *)_PyFrame_GetFrameObject(frame));
+
+ PyObject *pyFrame = Py_XNewRef((PyObject *)_PyFrame_GetFrameObject(frame));
+ if (pyFrame && _PySys_Audit(tstate, "sys._getframe", "(O)", pyFrame) < 0) {
+ Py_DECREF(pyFrame);
+ return NULL;
+ }
+ return pyFrame;
 }
 
 /*[clinic input]


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /