Skip to content
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)
Comment thread
markshannon marked this conversation as resolved.
Outdated
{
return frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
Comment thread
markshannon marked this conversation as resolved.
Outdated
}

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 6 additions & 2 deletions Python/frame.c
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