bpo-42197: Don't create f_locals dictionary unless we actually need it. (GH-32055)

* `PyFrame_FastToLocalsWithError` and `PyFrame_LocalsToFast` are no longer called during profile and tracing.
 (Contributed by Fabio Zadrozny)

* Make accesses to a frame's `f_locals` safe from C code, not relying on calls to `PyFrame_FastToLocals` or `PyFrame_LocalsToFast`.

* Document new `PyFrame_GetLocals` C-API function.
This commit is contained in:
Mark Shannon 2022-03-25 12:57:50 +00:00 committed by GitHub
parent b68431fadb
commit d7163bb35d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 10 deletions

View file

@ -840,6 +840,7 @@ _PyFrame_New_NoTrack(PyCodeObject *code)
f->f_trace = NULL;
f->f_trace_lines = 1;
f->f_trace_opcodes = 0;
f->f_fast_as_locals = 0;
f->f_lineno = 0;
return f;
}
@ -1004,7 +1005,11 @@ PyFrame_FastToLocalsWithError(PyFrameObject *f)
PyErr_BadInternalCall();
return -1;
}
return _PyFrame_FastToLocalsWithError(f->f_frame);
int err = _PyFrame_FastToLocalsWithError(f->f_frame);
if (err == 0) {
f->f_fast_as_locals = 1;
}
return err;
}
void
@ -1028,8 +1033,9 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
PyObject *error_type, *error_value, *error_traceback;
PyCodeObject *co;
locals = frame->f_locals;
if (locals == NULL)
if (locals == NULL) {
return;
}
fast = _PyFrame_GetLocalsArray(frame);
co = frame->f_code;
@ -1088,13 +1094,12 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
void
PyFrame_LocalsToFast(PyFrameObject *f, int clear)
{
if (f == NULL || _PyFrame_GetState(f) == FRAME_CLEARED) {
return;
if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) {
_PyFrame_LocalsToFast(f->f_frame, clear);
f->f_fast_as_locals = 0;
}
_PyFrame_LocalsToFast(f->f_frame, clear);
}
PyCodeObject *
PyFrame_GetCode(PyFrameObject *frame)
{
@ -1118,6 +1123,12 @@ PyFrame_GetBack(PyFrameObject *frame)
return back;
}
PyObject*
PyFrame_GetLocals(PyFrameObject *frame)
{
return frame_getlocals(frame, NULL);
}
PyObject*
_PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals)
{