8000 GH-94262: Don't create frame objects for frames that aren't complete. by markshannon · Pull Request #94371 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-94262: Don't create frame objects for frames that aren't complete. #94371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 1, 2022
Prev Previous commit
Next Next commit
Don't expose incomplete frames as frame objects.
  • Loading branch information
markshannon committed Jun 27, 2022
commit 583d16470101800ceac6fc73bb46f37dc853404b
7 changes: 7 additions & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
return (PyGenObject *)(((char *)frame) - offset_in_gen);
}


static inline bool
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
{
return frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
}

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 6 additions & 2 deletions Python/frame.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
f->f_frame = frame;
frame->owner = FRAME_OWNED_BY_FRAME_OBJECT;
assert(f->f_back == NULL);
if (frame->previous != NULL) {
_PyInterpreterFrame *prev = frame->previous;
while (prev && _PyFrame_IsIncomplete(prev)) {
prev = prev->previous;
}
if (prev) {
/* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
PyFrameObject *back = _PyFrame_GetFrameObject(frame->previous);
PyFrameObject *back = _PyFrame_GetFrameObject(prev);
if (back == NULL) {
/* Memory error here. */
assert(PyErr_ExceptionMatches(PyExc_MemoryError));
Expand Down
14 changes: 11 additions & 3 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1776,9 +1776,17 @@ sys__getframe_impl(PyObject *module, int depth)
return NULL;
}

while (depth > 0 && frame != NULL) {
frame = frame->previous;
--depth;
if (frame != NULL) {
while (depth > 0) {
frame = frame->previous;
if (frame == NULL) {
break;
}
if (_PyFrame_IsIncomplete(frame)) {
continue;
}
--depth;
}
}
if (frame == NULL) {
_PyErr_SetString(tstate, PyExc_ValueError,
Expand Down
0