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
Assert frame is valid when creating frame object.
  • Loading branch information
markshannon committed Jun 28, 2022
commit c648d46ec789782cade7541c5182fc476ce1fe52
27 changes: 17 additions & 10 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer)
frame->stacktop = (int)(stack_pointer - frame->localsplus);
}

/* Determine whether a frame is incomplete.
* A frame is incomplete if it is part way through
* creating cell objects or a generator or coroutine.
*
* Frames on the frame stack are incomplete until the
* first RESUME instruction.
* Frames owned by a generator are always complete.
*/
static inline bool
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
{
return frame->owner != FRAME_OWNED_BY_GENERATOR &&
frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable;
}

/* For use by _PyFrame_GetFrameObject
Do not call directly. */
PyFrameObject *
Expand All @@ -145,6 +160,8 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame);
static inline PyFrameObject *
_PyFrame_GetFrameObject(_PyInterpreterFrame *frame)
{

assert(!_PyFrame_IsIncomplete(frame));
PyFrameObject *res = frame->frame_obj;
if (res != NULL) {
return res;
Expand Down Expand Up @@ -210,16 +227,6 @@ PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
return (PyGenObject *)(((char *)frame) - offset_in_gen);
}

/* Determine whether a frame is incomplete.
* A frame is incomplete until the first RESUME instruction
* as it may be part way through creating cell objects or a
* generator or coroutine. */
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
11 changes: 5 additions & 6 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,10 @@ PyCode_New(int argcount, int kwonlyargcount,
exceptiontable);
}

static const char assert0[4] = {
LOAD_ASSERTION_ERROR,
0,
RAISE_VARARGS,
1
static const char assert0[6] = {
RESUME, 0,
LOAD_ASSERTION_ERROR, 0,
RAISE_VARARGS, 1
};

PyCodeObject *
Expand All @@ -666,7 +665,7 @@ PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
if (filename_ob == NULL) {
goto failed;
}
code_ob = PyBytes_FromStringAndSize(assert0, 4);
code_ob = PyBytes_FromStringAndSize(assert0, 6);
if (code_ob == NULL) {
goto failed;
}
Expand Down
0