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

@ -924,15 +924,19 @@ static PyObject *
call_trampoline(PyThreadState *tstate, PyObject* callback,
PyFrameObject *frame, int what, PyObject *arg)
{
if (PyFrame_FastToLocalsWithError(frame) < 0) {
return NULL;
}
PyObject *stack[3];
stack[0] = (PyObject *)frame;
stack[1] = whatstrings[what];
stack[2] = (arg != NULL) ? arg : Py_None;
/* Discard any previous modifications the frame's fast locals */
if (frame->f_fast_as_locals) {
if (PyFrame_FastToLocalsWithError(frame) < 0) {
return NULL;
}
}
/* call the Python-level function */
PyObject *result = _PyObject_FastCallTstate(tstate, callback, stack, 3);