Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
79aeaf6
Turn specials array into struct and add 'lasti' to it.
markshannon Jun 21, 2021
3b6a4e8
Move stack-depth from frame object to specials struct.
markshannon Jun 22, 2021
6f0ba40
Rename 'specials' to 'frame' as it now includes most of the data for …
markshannon Jun 22, 2021
8543c27
Refactor, pushing PyFrame upward toward _PyEval_Vector.
markshannon Jun 22, 2021
423d403
Add pointer from stack frame to frame object and rename tstate.frame …
markshannon Jun 22, 2021
7bf6c30
More refactoring. Add tstate.frame for the top stack frame.
markshannon Jun 23, 2021
861a8d9
Convert use of PyFrameObject to _PyFrame.
markshannon Jun 23, 2021
35e793c
Replace most remaining uses frame object in the interpreter with stac…
markshannon Jun 24, 2021
192094e
Convert more uses of frameobject to frame.
markshannon Jun 24, 2021
3f601a7
Move f_state from frame object to frame.
markshannon Jun 29, 2021
bd95c32
Compute f_back when on thread stack, only filling in value when frame…
markshannon Jun 30, 2021
9961abc
Add NULL check
markshannon Jun 30, 2021
32af707
Get lazy f_back working (it still leaks).
markshannon Jul 2, 2021
ac7dbe8
Use frames not frameobjects in sys._getframe()
markshannon Jul 2, 2021
f33d291
NULL out frame->previous when leaving frame.
markshannon Jul 2, 2021
5c23a36
Frames now include nlocalspuls, so they have valid layout after code …
markshannon Jul 5, 2021
910e991
Move ownership of frame in generator from frame object ot generator o…
markshannon Jul 5, 2021
22e1c9b
Remove localsptr field from frame object.
markshannon Jul 6, 2021
f84a3f0
Add new _PyEval_EvalNoFrame function for evaluating frames directly.
markshannon Jul 6, 2021
1180a44
Allow for lazily created frames.
markshannon Jul 6, 2021
c76de89
Do not create frame objects for Python calls.
markshannon Jul 6, 2021
15aeef1
Don't create frame objects for generators.
markshannon Jul 6, 2021
1d2e1ce
Fix memory leak
markshannon Jul 7, 2021
1b19f8b
Merge branch 'main' into lazy-frame-updated
markshannon Jul 7, 2021
d619bae
Restore support for PEP 523.
markshannon Jul 8, 2021
d147d03
Streamline pushing and popping stack frames a bit.
markshannon Jul 9, 2021
25c6a71
Merge branch 'main' into lazy-frame
markshannon Jul 9, 2021
618b094
Add f_ prefix back to several frame fields to ease porting C code tha…
markshannon Jul 9, 2021
e5da338
Add NEWS
markshannon Jul 9, 2021
dda0b0c
Remove debugging artifact.
markshannon Jul 9, 2021
5ecc067
Make symbol private
markshannon Jul 9, 2021
3b65a0f
Fix use-after-free error.
markshannon Jul 9, 2021
596213d
Add some explanatory comments.
markshannon Jul 9, 2021
386275e
Remove debugging artifact.
markshannon Jul 9, 2021
596c041
Merge branch 'main' into lazy-frame
markshannon Jul 15, 2021
039bca7
Rename _PyFrame to InterpreterFrame.
markshannon Jul 15, 2021
2cad33b
Remove use-after-free in assert.
markshannon Jul 19, 2021
77cf187
Merge branch 'main' into lazy-frame
markshannon Jul 19, 2021
decf209
Make name of frame argument consistent across _PyEval_Vector, _PyEval…
markshannon Jul 19, 2021
666b618
Allow for old gdbs still using Python 2.
markshannon Jul 19, 2021
90ed5b6
Various small clarifications as suggested by Pablo.
markshannon Jul 21, 2021
593a348
Refactor interpreter frame code into its own file. Improve a few names.
markshannon Jul 21, 2021
b775f13
Tidy up assert.
markshannon Jul 21, 2021
e8476b2
Fix warning on Windows.
markshannon Jul 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename _PyFrame to InterpreterFrame.
  • Loading branch information
markshannon committed Jul 15, 2021
commit 039bca731cbfea403fe63402738d7b3fa70efa17
2 changes: 1 addition & 1 deletion Include/cpython/ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
flag was set, else return 0. */
PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);

PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(PyThreadState *tstate, struct _py_frame *f, int exc);
PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(PyThreadState *tstate, struct _interpreter_frame *f, int exc);

PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
Expand Down
4 changes: 2 additions & 2 deletions Include/cpython/frameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
struct _frame {
PyObject_HEAD
struct _frame *f_back; /* previous frame, or NULL */
Comment thread
pablogsal marked this conversation as resolved.
struct _py_frame *f_frame; /* points to the frame data */
struct _interpreter_frame *f_frame; /* points to the frame data */
PyObject *f_trace; /* Trace function */
int f_lineno; /* Current line number. Only valid if non-zero */
char f_trace_lines; /* Emit per-line trace events? */
Expand All @@ -26,7 +26,7 @@ PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *,

/* only internal use */
PyFrameObject*
_PyFrame_New_NoTrack(struct _py_frame *, int);
_PyFrame_New_NoTrack(struct _interpreter_frame *, int);


/* The rest of the interface is specific for frame objects */
Expand Down
4 changes: 2 additions & 2 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct _ts {
PyInterpreterState *interp;

/* Borrowed reference to the current frame (it can be NULL) */
struct _py_frame *frame;
struct _interpreter_frame *frame;
int recursion_depth;
int recursion_headroom; /* Allow 50 more calls to handle any errors. */
int stackcheck_counter;
Expand Down Expand Up @@ -223,7 +223,7 @@ PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);

/* Frame evaluation API */

typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, struct _py_frame *, int);
typedef PyObject* (*_PyFrameEvalFunction)(PyThreadState *tstate, struct _interpreter_frame *, int);

PyAPI_FUNC(_PyFrameEvalFunction) _PyInterpreterState_GetEvalFrameFunc(
PyInterpreterState *interp);
Expand Down
2 changes: 1 addition & 1 deletion Include/genobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C" {
#define _PyGenObject_HEAD(prefix) \
PyObject_HEAD \
/* Note: gi_frame can be NULL if the generator is "finished" */ \
struct _py_frame *prefix##_xframe; \
struct _interpreter_frame *prefix##_xframe; \
/* The code object backing the generator */ \
PyCodeObject *prefix##_code; \
/* List of weak reference. */ \
Expand Down
6 changes: 3 additions & 3 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ extern PyObject *_PyEval_BuiltinsFromGlobals(


static inline PyObject*
_PyEval_EvalFrame(PyThreadState *tstate, struct _py_frame *f, int throwflag)
_PyEval_EvalFrame(PyThreadState *tstate, struct _interpreter_frame *f, int throwflag)
{
return tstate->interp->eval_frame(tstate, f, throwflag);
}
Expand Down Expand Up @@ -107,9 +107,9 @@ static inline void _Py_LeaveRecursiveCall_inline(void) {

#define Py_LeaveRecursiveCall() _Py_LeaveRecursiveCall_inline()

struct _py_frame *_PyEval_GetFrame(void);
struct _interpreter_frame *_PyEval_GetFrame(void);

PyObject *_Py_MakeCoro(PyFrameConstructor *, struct _py_frame *);
PyObject *_Py_MakeCoro(PyFrameConstructor *, struct _interpreter_frame *);

#ifdef __cplusplus
}
Expand Down
34 changes: 18 additions & 16 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,42 @@ enum _framestate {

typedef signed char PyFrameState;

typedef struct _py_frame {
typedef struct _interpreter_frame {
PyObject *f_globals;
PyObject *f_builtins;
PyObject *f_locals;
PyCodeObject *f_code;
PyFrameObject *frame_obj;
/* Borrowed reference to a generator, or NULL */
PyObject *generator;
struct _py_frame *previous;
struct _interpreter_frame *previous;
int f_lasti; /* Last instruction if called */
int stackdepth; /* Depth of value stack */
int nlocalsplus;
PyFrameState f_state; /* What state the frame is in */
PyObject *stack[1];
} _PyFrame;
} InterpreterFrame;

static inline int _PyFrame_IsRunnable(_PyFrame *f) {
static inline int _PyFrame_IsRunnable(InterpreterFrame *f) {
return f->f_state < FRAME_EXECUTING;
}

static inline int _PyFrame_IsExecuting(_PyFrame *f) {
static inline int _PyFrame_IsExecuting(InterpreterFrame *f) {
return f->f_state == FRAME_EXECUTING;
}

static inline int _PyFrameHasCompleted(_PyFrame *f) {
static inline int _PyFrameHasCompleted(InterpreterFrame *f) {
return f->f_state > FRAME_EXECUTING;
}

#define FRAME_SPECIALS_SIZE ((sizeof(_PyFrame)-1)/sizeof(PyObject *))
#define FRAME_SPECIALS_SIZE ((sizeof(InterpreterFrame)-1)/sizeof(PyObject *))

void _PyFrame_TakeLocals(PyFrameObject *f, _PyFrame *locals);
void _PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *locals);

static inline void
_PyFrame_InitializeSpecials(_PyFrame *frame, PyFrameConstructor *con, PyObject *locals, int nlocalsplus)
_PyFrame_InitializeSpecials(
InterpreterFrame *frame, PyFrameConstructor *con,
PyObject *locals, int nlocalsplus)
{
frame->f_code = (PyCodeObject *)Py_NewRef(con->fc_code);
frame->f_builtins = Py_NewRef(con->fc_builtins);
Expand All @@ -67,7 +69,7 @@ _PyFrame_InitializeSpecials(_PyFrame *frame, PyFrameConstructor *con, PyObject *
}

static inline void
_PyFrame_ClearSpecials(_PyFrame *frame)
_PyFrame_ClearSpecials(InterpreterFrame *frame)
{
frame->generator = NULL;
Py_XDECREF(frame->frame_obj);
Expand All @@ -81,21 +83,21 @@ _PyFrame_ClearSpecials(_PyFrame *frame)
* that precedes this frame.
*/
static inline PyObject**
_PyFrame_GetLocalsArray(_PyFrame *frame)
_PyFrame_GetLocalsArray(InterpreterFrame *frame)
{
return ((PyObject **)frame) - frame->nlocalsplus;
}

/* For use by _PyFrame_GetFrameObject
Do not call directly. */
PyFrameObject *
_PyFrame_MakeAndSetFrameObject(_PyFrame *frame);
_PyFrame_MakeAndSetFrameObject(InterpreterFrame *frame);

/* Gets the PyFrameObject for this frame, lazily
* creating it if necessary.
* Returns a borrowed referennce */
static inline PyFrameObject *
_PyFrame_GetFrameObject(_PyFrame *frame)
_PyFrame_GetFrameObject(InterpreterFrame *frame)
{
PyFrameObject *res = frame->frame_obj;
if (res != NULL) {
Expand All @@ -114,13 +116,13 @@ _PyFrame_GetFrameObject(_PyFrame *frame)
* frames.
*/
Comment thread
markshannon marked this conversation as resolved.
int
_PyFrame_Clear(_PyFrame * frame, int take);
_PyFrame_Clear(InterpreterFrame * frame, int take);

int
_PyFrame_FastToLocalsWithError(_PyFrame *frame);
_PyFrame_FastToLocalsWithError(InterpreterFrame *frame);

void
_PyFrame_LocalsToFast(_PyFrame *frame, int clear);
_PyFrame_LocalsToFast(InterpreterFrame *frame, int clear);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ PyAPI_FUNC(int) _PyState_AddModule(

PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);

struct _py_frame *_PyThreadState_PushFrame(
struct _interpreter_frame *_PyThreadState_PushFrame(
Comment thread
markshannon marked this conversation as resolved.
Outdated
PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);

void _PyThreadState_PopFrame(PyThreadState *tstate, struct _py_frame *frame);
void _PyThreadState_PopFrame(PyThreadState *tstate, struct _interpreter_frame *frame);

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ hashtable_compare_traceback(const void *key1, const void *key2)


static void
tracemalloc_get_frame(_PyFrame *pyframe, frame_t *frame)
tracemalloc_get_frame(InterpreterFrame *pyframe, frame_t *frame)
{
frame->filename = unknown_filename;
int lineno = PyCode_Addr2Line(pyframe->f_code, pyframe->f_lasti*2);
Expand Down Expand Up @@ -393,7 +393,7 @@ traceback_get_frames(traceback_t *traceback)
return;
}

_PyFrame *pyframe = tstate->frame;
InterpreterFrame *pyframe = tstate->frame;
for (; pyframe != NULL;) {
if (traceback->nframe < _Py_tracemalloc_config.max_nframe) {
tracemalloc_get_frame(pyframe, &traceback->frames[traceback->nframe]);
Expand All @@ -404,7 +404,7 @@ traceback_get_frames(traceback_t *traceback)
traceback->total_nframe++;
}

_PyFrame *back = pyframe->previous;
InterpreterFrame *back = pyframe->previous;
pyframe = back;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,7 @@ _is_running(PyInterpreterState *interp)
}

assert(!PyErr_Occurred());
_PyFrame *frame = tstate->frame;
InterpreterFrame *frame = tstate->frame;
if (frame == NULL) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ _PyErr_CheckSignalsTstate(PyThreadState *tstate)
*/
_Py_atomic_store(&is_tripped, 0);

_PyFrame *frame = tstate->frame;
InterpreterFrame *frame = tstate->frame;
signal_state_t *state = &signal_global_state;
for (int i = 1; i < NSIG; i++) {
if (!_Py_atomic_load_relaxed(&Handlers[i].tripped)) {
Expand Down
20 changes: 10 additions & 10 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ frame_dealloc(PyFrameObject *f)
/* Kill all local variables including specials, if we own them */
if (f->f_own_locals_memory) {
f->f_own_locals_memory = 0;
_PyFrame *frame = f->f_frame;
InterpreterFrame *frame = f->f_frame;
/* Don't clear code object until the end */
co = frame->f_code;
frame->f_code = NULL;
Expand Down Expand Up @@ -803,7 +803,7 @@ PyTypeObject PyFrame_Type = {

_Py_IDENTIFIER(__builtins__);

static _PyFrame *
static InterpreterFrame *
allocate_heap_frame(PyFrameConstructor *con, PyObject *locals)
{
PyCodeObject *code = (PyCodeObject *)con->fc_code;
Expand All @@ -816,13 +816,13 @@ allocate_heap_frame(PyFrameConstructor *con, PyObject *locals)
for (Py_ssize_t i=0; i < code->co_nlocalsplus; i++) {
localsarray[i] = NULL;
}
_PyFrame *frame = (_PyFrame *)(localsarray + code->co_nlocalsplus);
InterpreterFrame *frame = (InterpreterFrame *)(localsarray + code->co_nlocalsplus);
_PyFrame_InitializeSpecials(frame, con, locals, code->co_nlocalsplus);
return frame;
}

static inline PyFrameObject*
frame_alloc(_PyFrame *frame, int owns)
frame_alloc(InterpreterFrame *frame, int owns)
{
PyFrameObject *f;
struct _Py_frame_state *state = get_frame_state();
Expand Down Expand Up @@ -857,7 +857,7 @@ frame_alloc(_PyFrame *frame, int owns)
}

void
_PyFrame_TakeLocals(PyFrameObject *f, _PyFrame *frame)
_PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *frame)
Comment thread
markshannon marked this conversation as resolved.
Outdated
{
assert(f->f_own_locals_memory == 0);
assert(frame->frame_obj == NULL);
Expand All @@ -883,7 +883,7 @@ _PyFrame_TakeLocals(PyFrameObject *f, _PyFrame *frame)
}

PyFrameObject* _Py_HOT_FUNCTION
_PyFrame_New_NoTrack(_PyFrame *frame, int owns)
_PyFrame_New_NoTrack(InterpreterFrame *frame, int owns)
{
PyFrameObject *f = frame_alloc(frame, owns);
if (f == NULL) {
Expand Down Expand Up @@ -916,7 +916,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
.fc_kwdefaults = NULL,
.fc_closure = NULL
};
_PyFrame *frame = allocate_heap_frame(&desc, locals);
InterpreterFrame *frame = allocate_heap_frame(&desc, locals);
if (frame == NULL) {
return NULL;
}
Expand All @@ -928,7 +928,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
}

static int
_PyFrame_OpAlreadyRan(_PyFrame *frame, int opcode, int oparg)
_PyFrame_OpAlreadyRan(InterpreterFrame *frame, int opcode, int oparg)
{
const _Py_CODEUNIT *code =
(const _Py_CODEUNIT *)PyBytes_AS_STRING(frame->f_code->co_code);
Expand All @@ -941,7 +941,7 @@ _PyFrame_OpAlreadyRan(_PyFrame *frame, int opcode, int oparg)
}

int
_PyFrame_FastToLocalsWithError(_PyFrame *frame) {
_PyFrame_FastToLocalsWithError(InterpreterFrame *frame) {
/* Merge fast locals into f->f_locals */
PyObject *locals;
PyObject **fast;
Expand Down Expand Up @@ -1041,7 +1041,7 @@ PyFrame_FastToLocals(PyFrameObject *f)
}

void
_PyFrame_LocalsToFast(_PyFrame *frame, int clear)
_PyFrame_LocalsToFast(InterpreterFrame *frame, int clear)
{
/* Merge locals into fast locals */
PyObject *locals;
Expand Down
Loading