mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
gh-91348: Restore frame argument to sys._getframe audit event (GH-94928)
This commit is contained in:
parent
5c19ddab65
commit
044a593cbb
4 changed files with 31 additions and 6 deletions
|
@ -774,7 +774,7 @@ always available.
|
||||||
that is deeper than the call stack, :exc:`ValueError` is raised. The default
|
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.
|
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::
|
.. impl-detail::
|
||||||
|
|
||||||
|
|
|
@ -408,6 +408,17 @@ def test_sqlite3():
|
||||||
raise RuntimeError("Expected sqlite3.load_extension to fail")
|
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__":
|
if __name__ == "__main__":
|
||||||
from test.support import suppress_msvcrt_asserts
|
from test.support import suppress_msvcrt_asserts
|
||||||
|
|
||||||
|
|
|
@ -176,5 +176,17 @@ class AuditTest(unittest.TestCase):
|
||||||
self.assertEqual(actual, expected)
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -1772,10 +1772,6 @@ sys__getframe_impl(PyObject *module, int depth)
|
||||||
PyThreadState *tstate = _PyThreadState_GET();
|
PyThreadState *tstate = _PyThreadState_GET();
|
||||||
_PyInterpreterFrame *frame = tstate->cframe->current_frame;
|
_PyInterpreterFrame *frame = tstate->cframe->current_frame;
|
||||||
|
|
||||||
if (_PySys_Audit(tstate, "sys._getframe", NULL) < 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (frame != NULL) {
|
if (frame != NULL) {
|
||||||
while (depth > 0) {
|
while (depth > 0) {
|
||||||
frame = frame->previous;
|
frame = frame->previous;
|
||||||
|
@ -1793,7 +1789,13 @@ sys__getframe_impl(PyObject *module, int depth)
|
||||||
"call stack is not deep enough");
|
"call stack is not deep enough");
|
||||||
return NULL;
|
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]
|
/*[clinic input]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue