From c5bee134e61b80d1e4a32bb6a9baa19dca288ec1 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Tue, 7 May 2024 16:41:50 -0700 Subject: [PATCH] Fix crash in getlocals --- Objects/frameobject.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 26a04cbeea90bf..d4cc6615eaf272 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -742,6 +742,15 @@ frame_getlocals(PyFrameObject *f, void *closure) PyCodeObject *co = _PyFrame_GetCode(f->f_frame); if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(f->f_frame)) { + if (f->f_frame->f_locals == NULL) { + // We found cases when f_locals is NULL for non-optimized code. + // We fill the f_locals with an empty dict to avoid crash until + // we find the root cause. + f->f_frame->f_locals = PyDict_New(); + if (f->f_frame->f_locals == NULL) { + return NULL; + } + } return Py_NewRef(f->f_frame->f_locals); }