8000 gh-118746: Fix crash in frame_getlocals and _PyFrame_GetLocals (#118748) · python/cpython@e7aec87 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7aec87

Browse files
gh-118746: Fix crash in frame_getlocals and _PyFrame_GetLocals (#118748)
We don't know how to create an unoptimized frame with f_locals == NULL, but they are seen in the wild, and this fixes the crash.
1 parent 2f0a338 commit e7aec87

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Objects/frameobject.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,15 @@ frame_getlocals(PyFrameObject *f, void *closure)
742742
PyCodeObject *co = _PyFrame_GetCode(f->f_frame);
743743

744744
if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(f->f_frame)) {
745+
if (f->f_frame->f_locals == NULL) {
746+
// We found cases when f_locals is NULL for non-optimized code.
747+
// We fill the f_locals with an empty dict to avoid crash until
748+
// we find the root cause.
749+
f->f_frame->f_locals = PyDict_New();
750+
if (f->f_frame->f_locals == NULL) {
751+
return NULL;
752+
}
753+
}
745754
return Py_NewRef(f->f_frame->f_locals);
746755
}
747756

@@ -1937,6 +1946,15 @@ _PyFrame_GetLocals(_PyInterpreterFrame *frame)
19371946
PyCodeObject *co = _PyFrame_GetCode(frame);
19381947

19391948
if (!(co->co_flags & CO_OPTIMIZED) && !_PyFrame_HasHiddenLocals(frame)) {
1949+
if (frame->f_locals == NULL) {
1950+
// We found cases when f_locals is NULL for non-optimized code.
1951+
// We fill the f_locals with an empty dict to avoid crash until
1952+
// we find the root cause.
1953+
frame->f_locals = PyDict_New();
1954+
if (frame->f_locals == NULL) {
1955+
return NULL;
1956+
}
1957+
}
19401958
return Py_NewRef(frame->f_locals);
19411959
}
19421960

0 commit comments

Comments
 (0)
0