From bbb99c18e4d857838057679b9a2b4395f497a620 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 9 Mar 2023 23:25:06 +0000 Subject: [PATCH 1/7] Rename local variables, names and consts, from the interpeter loop. Will allow non-code objects in frames for better introspection of C builtins and extensions. --- Python/bytecodes.c | 42 +++++++++++++++++----------------- Python/ceval.c | 7 ------ Python/generated_cases.c.h | 46 +++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 45d50726674321..2cbd7b2c7ad6f4 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -115,7 +115,7 @@ dummy_func( } inst(LOAD_CONST, (-- value)) { - value = GETITEM(consts, oparg); + value = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(value); } @@ -563,7 +563,7 @@ dummy_func( } inst(RETURN_CONST, (--)) { - PyObject *retval = GETITEM(consts, oparg); + PyObject *retval = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(retval); assert(EMPTY()); _PyFrame_SetStackPointer(frame, stack_pointer); @@ -853,7 +853,7 @@ dummy_func( } inst(STORE_NAME, (v -- )) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -871,7 +871,7 @@ dummy_func( } inst(DELETE_NAME, (--)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -965,7 +965,7 @@ dummy_func( #if ENABLE_SPECIALIZATION if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); next_instr--; _Py_Specialize_StoreAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -976,7 +976,7 @@ dummy_func( #else (void)counter; // Unused. #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyObject_SetAttr(owner, name, v); Py_DECREF(v); Py_DECREF(owner); @@ -984,21 +984,21 @@ dummy_func( } inst(DELETE_ATTR, (owner --)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyObject_SetAttr(owner, name, (PyObject *)NULL); Py_DECREF(owner); ERROR_IF(err, error); } inst(STORE_GLOBAL, (v --)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyDict_SetItem(GLOBALS(), name, v); Py_DECREF(v); ERROR_IF(err, error); } inst(DELETE_GLOBAL, (--)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err; err = PyDict_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. @@ -1012,7 +1012,7 @@ dummy_func( } inst(LOAD_NAME, ( -- v)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *locals = LOCALS(); if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, @@ -1083,7 +1083,7 @@ dummy_func( _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); next_instr--; _Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name); DISPATCH_SAME_OPARG(); @@ -1091,7 +1091,7 @@ dummy_func( STAT_INC(LOAD_GLOBAL, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); if (PyDict_CheckExact(GLOBALS()) && PyDict_CheckExact(BUILTINS())) { @@ -1445,7 +1445,7 @@ dummy_func( _PyAttrCache *cache = (_PyAttrCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); next_instr--; _Py_Specialize_LoadAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1453,7 +1453,7 @@ dummy_func( STAT_INC(LOAD_ATTR, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg >> 1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); if (oparg & 1) { /* Designed to work in tandem with CALL, pushes two values. */ PyObject* meth = NULL; @@ -1534,7 +1534,7 @@ dummy_func( PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, LOAD_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); uint16_t hint = index; DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR); if (DK_IS_UNICODE(dict->ma_keys)) { @@ -1625,7 +1625,7 @@ dummy_func( DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR); STAT_INC(LOAD_ATTR, hit); - PyObject *name = GETITEM(names, oparg >> 1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); Py_INCREF(f); _PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2); // Manipulate stack directly because we exit with DISPATCH_INLINED(). @@ -1670,7 +1670,7 @@ dummy_func( PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, STORE_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR); PyObject *old_value; uint64_t new_version; @@ -1864,14 +1864,14 @@ dummy_func( } inst(IMPORT_NAME, (level, fromlist -- res)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); res = import_name(tstate, frame, name, fromlist, level); DECREF_INPUTS(); ERROR_IF(res == NULL, error); } inst(IMPORT_FROM, (from -- from, res)) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); res = import_from(tstate, from, name); ERROR_IF(res == NULL, error); } @@ -2369,8 +2369,8 @@ dummy_func( inst(KW_NAMES, (--)) { assert(kwnames == NULL); - assert(oparg < PyTuple_GET_SIZE(consts)); - kwnames = GETITEM(consts, oparg); + assert(oparg < PyTuple_GET_SIZE(frame->f_code->co_consts)); + kwnames = GETITEM(frame->f_code->co_consts, oparg); } // Cache layout: counter/1, func_version/2, min_args/1 diff --git a/Python/ceval.c b/Python/ceval.c index b422d0ed34ede3..f4f23e88565820 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -773,18 +773,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int /* Local "register" variables. * These are cached values from the frame and code object. */ - PyObject *names; - PyObject *consts; _Py_CODEUNIT *next_instr; PyObject **stack_pointer; /* Sets the above local variables from the frame */ #define SET_LOCALS_FROM_FRAME() \ - { \ - PyCodeObject *co = frame->f_code; \ - names = co->co_names; \ - consts = co->co_consts; \ - } \ assert(_PyInterpreterFrame_LASTI(frame) >= -1); \ /* Jump back to the last instruction executed... */ \ next_instr = frame->prev_instr + 1; \ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 51357cda4c8949..8d35bc3aee7bad 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -50,7 +50,7 @@ TARGET(LOAD_CONST) { PREDICTED(LOAD_CONST); PyObject *value; - value = GETITEM(consts, oparg); + value = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(value); STACK_GROW(1); stack_pointer[-1] = value; @@ -101,7 +101,7 @@ oparg = (next_instr++)->op.arg; { PyObject *value; - value = GETITEM(consts, oparg); + value = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(value); _tmp_1 = value; } @@ -150,7 +150,7 @@ PyObject *_tmp_2; { PyObject *value; - value = GETITEM(consts, oparg); + value = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(value); _tmp_2 = value; } @@ -753,7 +753,7 @@ } TARGET(RETURN_CONST) { - PyObject *retval = GETITEM(consts, oparg); + PyObject *retval = GETITEM(frame->f_code->co_consts, oparg); Py_INCREF(retval); assert(EMPTY()); _PyFrame_SetStackPointer(frame, stack_pointer); @@ -1092,7 +1092,7 @@ TARGET(STORE_NAME) { PyObject *v = stack_pointer[-1]; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -1112,7 +1112,7 @@ } TARGET(DELETE_NAME) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -1226,7 +1226,7 @@ #if ENABLE_SPECIALIZATION if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); next_instr--; _Py_Specialize_StoreAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1237,7 +1237,7 @@ #else (void)counter; // Unused. #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyObject_SetAttr(owner, name, v); Py_DECREF(v); Py_DECREF(owner); @@ -1249,7 +1249,7 @@ TARGET(DELETE_ATTR) { PyObject *owner = stack_pointer[-1]; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyObject_SetAttr(owner, name, (PyObject *)NULL); Py_DECREF(owner); if (err) goto pop_1_error; @@ -1259,7 +1259,7 @@ TARGET(STORE_GLOBAL) { PyObject *v = stack_pointer[-1]; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err = PyDict_SetItem(GLOBALS(), name, v); Py_DECREF(v); if (err) goto pop_1_error; @@ -1268,7 +1268,7 @@ } TARGET(DELETE_GLOBAL) { - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); int err; err = PyDict_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. @@ -1284,7 +1284,7 @@ TARGET(LOAD_NAME) { PyObject *v; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); PyObject *locals = LOCALS(); if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, @@ -1356,7 +1356,7 @@ _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); next_instr--; _Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name); DISPATCH_SAME_OPARG(); @@ -1364,7 +1364,7 @@ STAT_INC(LOAD_GLOBAL, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); if (PyDict_CheckExact(GLOBALS()) && PyDict_CheckExact(BUILTINS())) { @@ -1806,7 +1806,7 @@ _PyAttrCache *cache = (_PyAttrCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); next_instr--; _Py_Specialize_LoadAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1814,7 +1814,7 @@ STAT_INC(LOAD_ATTR, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(names, oparg >> 1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); if (oparg & 1) { /* Designed to work in tandem with CALL, pushes two values. */ PyObject* meth = NULL; @@ -1925,7 +1925,7 @@ PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, LOAD_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(names, oparg>>1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); uint16_t hint = index; DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR); if (DK_IS_UNICODE(dict->ma_keys)) { @@ -2049,7 +2049,7 @@ DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR); STAT_INC(LOAD_ATTR, hit); - PyObject *name = GETITEM(names, oparg >> 1); + PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); Py_INCREF(f); _PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2); // Manipulate stack directly because we exit with DISPATCH_INLINED(). @@ -2105,7 +2105,7 @@ PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, STORE_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR); PyObject *old_value; uint64_t new_version; @@ -2358,7 +2358,7 @@ PyObject *fromlist = stack_pointer[-1]; PyObject *level = stack_pointer[-2]; PyObject *res; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); res = import_name(tstate, frame, name, fromlist, level); Py_DECREF(level); Py_DECREF(fromlist); @@ -2371,7 +2371,7 @@ TARGET(IMPORT_FROM) { PyObject *from = stack_pointer[-1]; PyObject *res; - PyObject *name = GETITEM(names, oparg); + PyObject *name = GETITEM(frame->f_code->co_names, oparg); res = import_from(tstate, from, name); if (res == NULL) goto error; STACK_GROW(1); @@ -3006,8 +3006,8 @@ TARGET(KW_NAMES) { assert(kwnames == NULL); - assert(oparg < PyTuple_GET_SIZE(consts)); - kwnames = GETITEM(consts, oparg); + assert(oparg < PyTuple_GET_SIZE(frame->f_code->co_consts)); + kwnames = GETITEM(frame->f_code->co_consts, oparg); DISPATCH(); } From 8bee1b8e3591b1a7623a813129f5fcf3a7aa7300 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 9 Mar 2023 23:36:44 +0000 Subject: [PATCH 2/7] Remove unused dummy variables. --- Python/bytecodes.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 2cbd7b2c7ad6f4..fedb47ddb6b60e 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -70,8 +70,6 @@ dummy_func( unsigned int oparg, _Py_atomic_int * const eval_breaker, _PyCFrame cframe, - PyObject *names, - PyObject *consts, _Py_CODEUNIT *next_instr, PyObject **stack_pointer, PyObject *kwnames, From f1b67c2a0310f11b5327a9507d8b5ac351c98e95 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Fri, 10 Mar 2023 18:31:31 +0000 Subject: [PATCH 3/7] All any object as 'code' field in PyInterpreterFrame. --- Include/internal/pycore_frame.h | 22 ++++++++---- Modules/_tracemalloc.c | 2 +- Objects/frameobject.c | 39 ++++++++++---------- Objects/genobject.c | 6 ++-- Objects/typeobject.c | 4 +-- Python/_warnings.c | 2 +- Python/bytecodes.c | 60 +++++++++++++++---------------- Python/ceval.c | 44 ++++++++++++----------- Python/ceval_macros.h | 8 +++-- Python/frame.c | 12 +++---- Python/generated_cases.c.h | 64 ++++++++++++++++----------------- Python/intrinsics.c | 6 ++-- Python/perf_trampoline.c | 2 +- Python/traceback.c | 4 +-- Tools/gdb/libpython.py | 25 ++++++++----- 15 files changed, 158 insertions(+), 142 deletions(-) diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h index 5806cf05f174a9..63f15aea854f9b 100644 --- a/Include/internal/pycore_frame.h +++ b/Include/internal/pycore_frame.h @@ -47,7 +47,7 @@ enum _frameowner { }; typedef struct _PyInterpreterFrame { - PyCodeObject *f_code; /* Strong reference */ + PyObject *f_executable; /* Strong reference */ struct _PyInterpreterFrame *previous; PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */ PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */ @@ -67,20 +67,25 @@ typedef struct _PyInterpreterFrame { } _PyInterpreterFrame; #define _PyInterpreterFrame_LASTI(IF) \ - ((int)((IF)->prev_instr - _PyCode_CODE((IF)->f_code))) + ((int)((IF)->prev_instr - _PyCode_CODE(_PyFrame_GetCode(IF)))) + +static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) { + assert(PyCode_Check(f->f_executable)); + return (PyCodeObject *)f->f_executable; +} static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) { - return f->localsplus + f->f_code->co_nlocalsplus; + return f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus; } static inline PyObject *_PyFrame_StackPeek(_PyInterpreterFrame *f) { - assert(f->stacktop > f->f_code->co_nlocalsplus); + assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus); assert(f->localsplus[f->stacktop-1] != NULL); return f->localsplus[f->stacktop-1]; } static inline PyObject *_PyFrame_StackPop(_PyInterpreterFrame *f) { - assert(f->stacktop > f->f_code->co_nlocalsplus); + assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus); f->stacktop--; return f->localsplus[f->stacktop]; } @@ -113,7 +118,7 @@ _PyFrame_Initialize( PyObject *locals, PyCodeObject *code, int null_locals_from) { frame->f_funcobj = (PyObject *)func; - frame->f_code = (PyCodeObject *)Py_NewRef(code); + frame->f_executable = Py_NewRef(code); frame->f_builtins = func->func_builtins; frame->f_globals = func->func_globals; frame->f_locals = locals; @@ -160,8 +165,11 @@ _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer) static inline bool _PyFrame_IsIncomplete(_PyInterpreterFrame *frame) { + if (frame->owner == FRAME_OWNED_BY_CSTACK) { + return true; + } return frame->owner != FRAME_OWNED_BY_GENERATOR && - frame->prev_instr < _PyCode_CODE(frame->f_code) + frame->f_code->_co_firsttraceable; + frame->prev_instr < _PyCode_CODE(_PyFrame_GetCode(frame)) + _PyFrame_GetCode(frame)->_co_firsttraceable; } static inline _PyInterpreterFrame * diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index d69c5636486da9..6f36df2fb2e3f2 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -263,7 +263,7 @@ tracemalloc_get_frame(_PyInterpreterFrame *pyframe, frame_t *frame) } frame->lineno = (unsigned int)lineno; - PyObject *filename = pyframe->f_code->co_filename; + PyObject *filename = _PyFrame_GetCode(pyframe)->co_filename; if (filename == NULL) { #ifdef TRACE_DEBUG diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 133c991bf701c4..d778efbcfad61a 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -644,6 +644,7 @@ _PyFrame_GetState(PyFrameObject *frame) static int frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignored)) { + PyCodeObject *code = _PyFrame_GetCode(f->f_frame); if (p_new_lineno == NULL) { PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); return -1; @@ -709,7 +710,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore } new_lineno = (int)l_new_lineno; - if (new_lineno < f->f_frame->f_code->co_firstlineno) { + if (new_lineno < code->co_firstlineno) { PyErr_Format(PyExc_ValueError, "line %d comes before the current code block", new_lineno); @@ -718,8 +719,8 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore /* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this * should never overflow. */ - int len = (int)Py_SIZE(f->f_frame->f_code); - int *lines = marklines(f->f_frame->f_code, len); + int len = (int)Py_SIZE(code); + int *lines = marklines(code, len); if (lines == NULL) { return -1; } @@ -733,7 +734,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore return -1; } - int64_t *stacks = mark_stacks(f->f_frame->f_code, len); + int64_t *stacks = mark_stacks(code, len); if (stacks == NULL) { PyMem_Free(lines); return -1; @@ -778,7 +779,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore // in the new location. Rather than crashing or changing co_code, just bind // None instead: int unbound = 0; - for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) { + for (int i = 0; i < code->co_nlocalsplus; i++) { // Counting every unbound local is overly-cautious, but a full flow // analysis (like we do in the compiler) is probably too expensive: unbound += f->f_frame->localsplus[i] == NULL; @@ -791,7 +792,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore } // Do this in a second pass to avoid writing a bunch of Nones when // warnings are being treated as errors and the previous bit raises: - for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) { + for (int i = 0; i < code->co_nlocalsplus; i++) { if (f->f_frame->localsplus[i] == NULL) { f->f_frame->localsplus[i] = Py_NewRef(Py_None); unbound--; @@ -822,7 +823,7 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore } /* Finally set the new lasti and return OK. */ f->f_lineno = 0; - f->f_frame->prev_instr = _PyCode_CODE(f->f_frame->f_code) + best_addr; + f->f_frame->prev_instr = _PyCode_CODE(code) + best_addr; return 0; } @@ -873,15 +874,15 @@ frame_dealloc(PyFrameObject *f) } Py_TRASHCAN_BEGIN(f, frame_dealloc); - PyCodeObject *co = NULL; + PyObject *co = NULL; /* Kill all local variables including specials, if we own them */ if (f->f_frame->owner == FRAME_OWNED_BY_FRAME_OBJECT) { assert(f->f_frame == (_PyInterpreterFrame *)f->_f_frame_data); _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data; /* Don't clear code object until the end */ - co = frame->f_code; - frame->f_code = NULL; + co = frame->f_executable; + frame->f_executable = NULL; Py_CLEAR(frame->f_funcobj); Py_CLEAR(frame->f_locals); PyObject **locals = _PyFrame_GetLocalsArray(frame); @@ -955,7 +956,7 @@ frame_sizeof(PyFrameObject *f, PyObject *Py_UNUSED(ignored)) { Py_ssize_t res; res = offsetof(PyFrameObject, _f_frame_data) + offsetof(_PyInterpreterFrame, localsplus); - PyCodeObject *code = f->f_frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(f->f_frame); res += _PyFrame_NumSlotsForCodeObject(code) * sizeof(PyObject *); return PyLong_FromSsize_t(res); } @@ -967,7 +968,7 @@ static PyObject * frame_repr(PyFrameObject *f) { int lineno = PyFrame_GetLineNumber(f); - PyCodeObject *code = f->f_frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(f->f_frame); return PyUnicode_FromFormat( "", f, code->co_filename, lineno, code->co_name); @@ -1089,7 +1090,7 @@ _PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg) // This only works when opcode is a non-quickened form: assert(_PyOpcode_Deopt[opcode] == opcode); int check_oparg = 0; - for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code); + for (_Py_CODEUNIT *instruction = _PyCode_CODE(_PyFrame_GetCode(frame)); instruction < frame->prev_instr; instruction++) { int check_opcode = _PyOpcode_Deopt[instruction->op.code]; @@ -1115,7 +1116,7 @@ frame_init_get_vars(_PyInterpreterFrame *frame) { // COPY_FREE_VARS has no quickened forms, so no need to use _PyOpcode_Deopt // here: - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); int lasti = _PyInterpreterFrame_LASTI(frame); if (!(lasti < 0 && _PyCode_CODE(co)->op.code == COPY_FREE_VARS && PyFunction_Check(frame->f_funcobj))) @@ -1132,7 +1133,7 @@ frame_init_get_vars(_PyInterpreterFrame *frame) frame->localsplus[offset + i] = Py_NewRef(o); } // COPY_FREE_VARS doesn't have inline CACHEs, either: - frame->prev_instr = _PyCode_CODE(frame->f_code); + frame->prev_instr = _PyCode_CODE(_PyFrame_GetCode(frame)); } @@ -1200,7 +1201,7 @@ _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) frame_init_get_vars(frame); - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); for (int i = 0; i < co->co_nlocalsplus; i++) { PyObject *value; // borrowed reference if (!frame_get_var(frame, co, i, &value)) { @@ -1240,7 +1241,7 @@ PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name) _PyInterpreterFrame *frame = frame_obj->f_frame; frame_init_get_vars(frame); - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); for (int i = 0; i < co->co_nlocalsplus; i++) { PyObject *var_name = PyTuple_GET_ITEM(co->co_localsplusnames, i); if (!_PyUnicode_Equal(var_name, name)) { @@ -1314,7 +1315,7 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear) return; } fast = _PyFrame_GetLocalsArray(frame); - co = frame->f_code; + co = _PyFrame_GetCode(frame); PyObject *exc = PyErr_GetRaisedException(); for (int i = 0; i < co->co_nlocalsplus; i++) { @@ -1400,7 +1401,7 @@ PyFrame_GetCode(PyFrameObject *frame) { assert(frame != NULL); assert(!_PyFrame_IsIncomplete(frame->f_frame)); - PyCodeObject *code = frame->f_frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame->f_frame); assert(code != NULL); return (PyCodeObject*)Py_NewRef(code); } diff --git a/Objects/genobject.c b/Objects/genobject.c index 61463774310f88..3d2777bf599110 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -28,7 +28,7 @@ static const char *ASYNC_GEN_IGNORED_EXIT_MSG = static inline PyCodeObject * _PyGen_GetCode(PyGenObject *gen) { _PyInterpreterFrame *frame = (_PyInterpreterFrame *)(gen->gi_iframe); - return frame->f_code; + return _PyFrame_GetCode(frame); } PyCodeObject * @@ -939,7 +939,7 @@ static PyObject * gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f, PyObject *name, PyObject *qualname) { - PyCodeObject *code = f->f_frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(f->f_frame); int size = code->co_nlocalsplus + code->co_stacksize; PyGenObject *gen = PyObject_GC_NewVar(PyGenObject, type, size); if (gen == NULL) { @@ -1321,7 +1321,7 @@ compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame) } frame = current_frame; for (int i = 0; i < frame_count; ++i) { - PyCodeObject *code = frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame); int line = _PyInterpreterFrame_GetLine(frame); PyObject *frameinfo = Py_BuildValue("OiO", code->co_filename, line, code->co_name); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index f486b83fd69e64..5ff8aa2dd86551 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -9496,7 +9496,7 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co, return -1; } - assert(cframe->f_code->co_nlocalsplus > 0); + assert(_PyFrame_GetCode(cframe)->co_nlocalsplus > 0); PyObject *firstarg = _PyFrame_GetLocalsArray(cframe)[0]; // The first argument might be a cell. if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) { @@ -9589,7 +9589,7 @@ super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) { "super(): no current frame"); return -1; } - int res = super_init_without_args(frame, frame->f_code, &type, &obj); + int res = super_init_without_args(frame, _PyFrame_GetCode(frame), &type, &obj); if (res < 0) { return -1; diff --git a/Python/_warnings.c b/Python/_warnings.c index d510381c365b66..b87da8cd10a569 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -901,7 +901,7 @@ setup_context(Py_ssize_t stack_level, } else { globals = f->f_frame->f_globals; - *filename = Py_NewRef(f->f_frame->f_code->co_filename); + *filename = Py_NewRef(_PyFrame_GetCode(f->f_frame)->co_filename); *lineno = PyFrame_GetLineNumber(f); Py_DECREF(f); } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index fedb47ddb6b60e..45737a5d037297 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -113,7 +113,7 @@ dummy_func( } inst(LOAD_CONST, (-- value)) { - value = GETITEM(frame->f_code->co_consts, oparg); + value = GETITEM(CONSTS(), oparg); Py_INCREF(value); } @@ -533,8 +533,6 @@ dummy_func( inst(INTERPRETER_EXIT, (retval --)) { assert(frame == &entry_frame); assert(_PyFrame_IsIncomplete(frame)); - STACK_SHRINK(1); // Since we're not going to DISPATCH() - assert(EMPTY()); /* Restore previous cframe and return. */ tstate->cframe = cframe.previous; tstate->cframe->use_tracing = cframe.use_tracing; @@ -561,7 +559,7 @@ dummy_func( } inst(RETURN_CONST, (--)) { - PyObject *retval = GETITEM(frame->f_code->co_consts, oparg); + PyObject *retval = GETITEM(CONSTS(), oparg); Py_INCREF(retval); assert(EMPTY()); _PyFrame_SetStackPointer(frame, stack_pointer); @@ -777,7 +775,7 @@ dummy_func( if (oparg) { PyObject *lasti = values[0]; if (PyLong_Check(lasti)) { - frame->prev_instr = _PyCode_CODE(frame->f_code) + PyLong_AsLong(lasti); + frame->prev_instr = _PyCode_CODE(_PyFrame_GetCode(frame)) + PyLong_AsLong(lasti); assert(!_PyErr_Occurred(tstate)); } else { @@ -851,7 +849,7 @@ dummy_func( } inst(STORE_NAME, (v -- )) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -869,7 +867,7 @@ dummy_func( } inst(DELETE_NAME, (--)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -963,7 +961,7 @@ dummy_func( #if ENABLE_SPECIALIZATION if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); next_instr--; _Py_Specialize_StoreAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -974,7 +972,7 @@ dummy_func( #else (void)counter; // Unused. #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyObject_SetAttr(owner, name, v); Py_DECREF(v); Py_DECREF(owner); @@ -982,21 +980,21 @@ dummy_func( } inst(DELETE_ATTR, (owner --)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyObject_SetAttr(owner, name, (PyObject *)NULL); Py_DECREF(owner); ERROR_IF(err, error); } inst(STORE_GLOBAL, (v --)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyDict_SetItem(GLOBALS(), name, v); Py_DECREF(v); ERROR_IF(err, error); } inst(DELETE_GLOBAL, (--)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err; err = PyDict_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. @@ -1010,7 +1008,7 @@ dummy_func( } inst(LOAD_NAME, ( -- v)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *locals = LOCALS(); if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, @@ -1081,7 +1079,7 @@ dummy_func( _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); next_instr--; _Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name); DISPATCH_SAME_OPARG(); @@ -1089,7 +1087,7 @@ dummy_func( STAT_INC(LOAD_GLOBAL, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); if (PyDict_CheckExact(GLOBALS()) && PyDict_CheckExact(BUILTINS())) { @@ -1185,7 +1183,7 @@ dummy_func( // Can't use ERROR_IF here. // Fortunately we don't need its superpower. if (oldobj == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); goto error; } PyCell_SET(cell, NULL); @@ -1195,8 +1193,8 @@ dummy_func( inst(LOAD_CLASSDEREF, ( -- value)) { PyObject *name, *locals = LOCALS(); assert(locals); - assert(oparg >= 0 && oparg < frame->f_code->co_nlocalsplus); - name = PyTuple_GET_ITEM(frame->f_code->co_localsplusnames, oparg); + assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); + name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); if (PyDict_CheckExact(locals)) { value = PyDict_GetItemWithError(locals, name); if (value != NULL) { @@ -1219,7 +1217,7 @@ dummy_func( PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); if (value == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); goto error; } Py_INCREF(value); @@ -1230,7 +1228,7 @@ dummy_func( PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); if (value == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); ERROR_IF(true, error); } Py_INCREF(value); @@ -1245,7 +1243,7 @@ dummy_func( inst(COPY_FREE_VARS, (--)) { /* Copy closure variables to free variables */ - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); assert(PyFunction_Check(frame->f_funcobj)); PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure; assert(oparg == co->co_nfreevars); @@ -1443,7 +1441,7 @@ dummy_func( _PyAttrCache *cache = (_PyAttrCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); next_instr--; _Py_Specialize_LoadAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1451,7 +1449,7 @@ dummy_func( STAT_INC(LOAD_ATTR, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); + PyObject *name = GETITEM(NAMES(), oparg >> 1); if (oparg & 1) { /* Designed to work in tandem with CALL, pushes two values. */ PyObject* meth = NULL; @@ -1532,7 +1530,7 @@ dummy_func( PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, LOAD_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); uint16_t hint = index; DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR); if (DK_IS_UNICODE(dict->ma_keys)) { @@ -1623,7 +1621,7 @@ dummy_func( DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR); STAT_INC(LOAD_ATTR, hit); - PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); + PyObject *name = GETITEM(NAMES(), oparg >> 1); Py_INCREF(f); _PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2); // Manipulate stack directly because we exit with DISPATCH_INLINED(). @@ -1668,7 +1666,7 @@ dummy_func( PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, STORE_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR); PyObject *old_value; uint64_t new_version; @@ -1862,14 +1860,14 @@ dummy_func( } inst(IMPORT_NAME, (level, fromlist -- res)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); res = import_name(tstate, frame, name, fromlist, level); DECREF_INPUTS(); ERROR_IF(res == NULL, error); } inst(IMPORT_FROM, (from -- from, res)) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); res = import_from(tstate, from, name); ERROR_IF(res == NULL, error); } @@ -2055,7 +2053,7 @@ dummy_func( /* before: [obj]; after [getiter(obj)] */ if (PyCoro_CheckExact(iterable)) { /* `iterable` is a coroutine */ - if (!(frame->f_code->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { + if (!(_PyFrame_GetCode(frame)->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { /* and it is used in a 'yield from' expression of a regular generator. */ _PyErr_SetString(tstate, PyExc_TypeError, @@ -2367,8 +2365,8 @@ dummy_func( inst(KW_NAMES, (--)) { assert(kwnames == NULL); - assert(oparg < PyTuple_GET_SIZE(frame->f_code->co_consts)); - kwnames = GETITEM(frame->f_code->co_consts, oparg); + assert(oparg < PyTuple_GET_SIZE(CONSTS())); + kwnames = GETITEM(CONSTS(), oparg); } // Cache layout: counter/1, func_version/2, min_args/1 diff --git a/Python/ceval.c b/Python/ceval.c index f4f23e88565820..0552c8105e64a4 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -127,6 +127,9 @@ lltrace_instruction(_PyInterpreterFrame *frame, PyObject **stack_pointer, _Py_CODEUNIT *next_instr) { + if (frame->owner == FRAME_OWNED_BY_CSTACK) { + return; + } /* This dump_stack() operation is risky, since the repr() of some objects enters the interpreter recursively. It is also slow. So you might want to comment it out. */ @@ -135,7 +138,7 @@ lltrace_instruction(_PyInterpreterFrame *frame, int opcode = next_instr->op.code; const char *opname = _PyOpcode_OpName[opcode]; assert(opname != NULL); - int offset = (int)(next_instr - _PyCode_CODE(frame->f_code)); + int offset = (int)(next_instr - _PyCode_CODE(_PyFrame_GetCode(frame))); if (HAS_ARG((int)_PyOpcode_Deopt[opcode])) { printf("%d: %s %d\n", offset * 2, opname, oparg); } @@ -743,7 +746,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int entry_frame.f_globals = (PyObject*)0xaaa3; entry_frame.f_builtins = (PyObject*)0xaaa4; #endif - entry_frame.f_code = tstate->interp->interpreter_trampoline; + entry_frame.f_executable = Py_None; entry_frame.prev_instr = _PyCode_CODE(tstate->interp->interpreter_trampoline); entry_frame.stacktop = 0; @@ -778,7 +781,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int /* Sets the above local variables from the frame */ #define SET_LOCALS_FROM_FRAME() \ - assert(_PyInterpreterFrame_LASTI(frame) >= -1); \ /* Jump back to the last instruction executed... */ \ next_instr = frame->prev_instr + 1; \ stack_pointer = _PyFrame_GetStackPointer(frame); \ @@ -853,7 +855,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int { assert(cframe.use_tracing); assert(tstate->tracing == 0); - if (INSTR_OFFSET() >= frame->f_code->_co_firsttraceable) { + if (frame->owner != FRAME_OWNED_BY_CSTACK && INSTR_OFFSET() >= _PyFrame_GetCode(frame)->_co_firsttraceable) { int instr_prev = _PyInterpreterFrame_LASTI(frame); frame->prev_instr = next_instr; NEXTOPARG(); @@ -940,7 +942,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int opcode = next_instr->op.code; _PyErr_Format(tstate, PyExc_SystemError, "%U:%d: unknown opcode %d", - frame->f_code->co_filename, + _PyFrame_GetCode(frame)->co_filename, _PyInterpreterFrame_GetLine(frame), opcode); goto error; @@ -955,7 +957,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int { format_exc_check_arg(tstate, PyExc_UnboundLocalError, UNBOUNDLOCAL_ERROR_MSG, - PyTuple_GetItem(frame->f_code->co_localsplusnames, oparg) + PyTuple_GetItem(_PyFrame_GetCode(frame)->co_localsplusnames, oparg) ); goto error; } @@ -1000,7 +1002,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int /* We can't use frame->f_lasti here, as RERAISE may have set it */ int offset = INSTR_OFFSET()-1; int level, handler, lasti; - if (get_exception_handler(frame->f_code, offset, &level, &handler, &lasti) == 0) { + if (get_exception_handler(_PyFrame_GetCode(frame), offset, &level, &handler, &lasti) == 0) { // No handlers, so exit. assert(_PyErr_Occurred(tstate)); @@ -1593,12 +1595,12 @@ clear_thread_frame(PyThreadState *tstate, _PyInterpreterFrame * frame) assert(frame->owner == FRAME_OWNED_BY_THREAD); // Make sure that this is, indeed, the top frame. We can't check this in // _PyThreadState_PopFrame, since f_code is already cleared at that point: - assert((PyObject **)frame + frame->f_code->co_framesize == + assert((PyObject **)frame + _PyFrame_GetCode(frame)->co_framesize == tstate->datastack_top); tstate->c_recursion_remaining--; assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame); _PyFrame_ClearExceptCode(frame); - Py_DECREF(frame->f_code); + Py_DECREF(frame->f_executable); tstate->c_recursion_remaining++; _PyThreadState_PopFrame(tstate, frame); } @@ -2074,7 +2076,7 @@ call_trace_protected(Py_tracefunc func, PyObject *obj, static void initialize_trace_info(PyTraceInfo *trace_info, _PyInterpreterFrame *frame) { - PyCodeObject *code = frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame); if (trace_info->code != code) { trace_info->code = code; _PyCode_InitAddressRange(code, &trace_info->bounds); @@ -2113,10 +2115,10 @@ call_trace(Py_tracefunc func, PyObject *obj, tstate->tracing_what = what; PyThreadState_EnterTracing(tstate); assert(_PyInterpreterFrame_LASTI(frame) >= 0); - if (_PyCode_InitLineArray(frame->f_code)) { + if (_PyCode_InitLineArray(_PyFrame_GetCode(frame))) { return -1; } - f->f_lineno = _PyCode_LineNumberFromArray(frame->f_code, _PyInterpreterFrame_LASTI(frame)); + f->f_lineno = _PyCode_LineNumberFromArray(_PyFrame_GetCode(frame), _PyInterpreterFrame_LASTI(frame)); result = func(obj, f, what, arg); f->f_lineno = 0; PyThreadState_LeaveTracing(tstate); @@ -2153,17 +2155,17 @@ maybe_call_line_trace(Py_tracefunc func, PyObject *obj, represents a jump backwards, update the frame's line number and then call the trace function if we're tracing source lines. */ - if (_PyCode_InitLineArray(frame->f_code)) { + if (_PyCode_InitLineArray(_PyFrame_GetCode(frame))) { return -1; } int lastline; - if (instr_prev <= frame->f_code->_co_firsttraceable) { + if (instr_prev <= _PyFrame_GetCode(frame)->_co_firsttraceable) { lastline = -1; } else { - lastline = _PyCode_LineNumberFromArray(frame->f_code, instr_prev); + lastline = _PyCode_LineNumberFromArray(_PyFrame_GetCode(frame), instr_prev); } - int line = _PyCode_LineNumberFromArray(frame->f_code, _PyInterpreterFrame_LASTI(frame)); + int line = _PyCode_LineNumberFromArray(_PyFrame_GetCode(frame), _PyInterpreterFrame_LASTI(frame)); PyFrameObject *f = _PyFrame_GetFrameObject(frame); if (f == NULL) { return -1; @@ -2459,7 +2461,7 @@ PyEval_MergeCompilerFlags(PyCompilerFlags *cf) int result = cf->cf_flags != 0; if (current_frame != NULL) { - const int codeflags = current_frame->f_code->co_flags; + const int codeflags = _PyFrame_GetCode(current_frame)->co_flags; const int compilerflags = codeflags & PyCF_MASK; if (compilerflags) { result = 1; @@ -2984,7 +2986,7 @@ dtrace_function_entry(_PyInterpreterFrame *frame) const char *funcname; int lineno; - PyCodeObject *code = frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame); filename = PyUnicode_AsUTF8(code->co_filename); funcname = PyUnicode_AsUTF8(code->co_name); lineno = _PyInterpreterFrame_GetLine(frame); @@ -2999,7 +3001,7 @@ dtrace_function_return(_PyInterpreterFrame *frame) const char *funcname; int lineno; - PyCodeObject *code = frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame); filename = PyUnicode_AsUTF8(code->co_filename); funcname = PyUnicode_AsUTF8(code->co_name); lineno = _PyInterpreterFrame_GetLine(frame); @@ -3027,11 +3029,11 @@ maybe_dtrace_line(_PyInterpreterFrame *frame, if (_PyInterpreterFrame_LASTI(frame) < instr_prev || (line != lastline && addr == trace_info->bounds.ar_start)) { - co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename); + co_filename = PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_filename); if (!co_filename) { co_filename = "?"; } - co_name = PyUnicode_AsUTF8(frame->f_code->co_name); + co_name = PyUnicode_AsUTF8(_PyFrame_GetCode(frame)->co_name); if (!co_name) { co_name = "?"; } diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index ac1fec77daca8a..d551eaf6b27af1 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -140,13 +140,13 @@ GETITEM(PyObject *v, Py_ssize_t i) { /* Code access macros */ /* The integer overflow is checked by an assertion below. */ -#define INSTR_OFFSET() ((int)(next_instr - _PyCode_CODE(frame->f_code))) +#define INSTR_OFFSET() ((int)(next_instr - _PyCode_CODE(_PyFrame_GetCode(frame)))) #define NEXTOPARG() do { \ _Py_CODEUNIT word = *next_instr; \ opcode = word.op.code; \ oparg = word.op.arg; \ } while (0) -#define JUMPTO(x) (next_instr = _PyCode_CODE(frame->f_code) + (x)) +#define JUMPTO(x) (next_instr = _PyCode_CODE(_PyFrame_GetCode(frame)) + (x)) #define JUMPBY(x) (next_instr += (x)) /* OpCode prediction macros @@ -199,7 +199,7 @@ GETITEM(PyObject *v, Py_ssize_t i) { /* The stack can grow at most MAXINT deep, as co_nlocals and co_stacksize are ints. */ #define STACK_LEVEL() ((int)(stack_pointer - _PyFrame_Stackbase(frame))) -#define STACK_SIZE() (frame->f_code->co_stacksize) +#define STACK_SIZE() (_PyFrame_GetCode(frame)->co_stacksize) #define EMPTY() (STACK_LEVEL() == 0) #define TOP() (stack_pointer[-1]) #define SECOND() (stack_pointer[-2]) @@ -282,6 +282,8 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define GLOBALS() frame->f_globals #define BUILTINS() frame->f_builtins #define LOCALS() frame->f_locals +#define CONSTS() ((PyCodeObject *)frame->f_executable)->co_consts +#define NAMES() ((PyCodeObject *)frame->f_executable)->co_names /* Shared opcode macros */ diff --git a/Python/frame.c b/Python/frame.c index c2c0be30113912..bffd705d1298f7 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -14,7 +14,7 @@ _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg) Py_VISIT(frame->frame_obj); Py_VISIT(frame->f_locals); Py_VISIT(frame->f_funcobj); - Py_VISIT(frame->f_code); + Py_VISIT(_PyFrame_GetCode(frame)); /* locals */ PyObject **locals = _PyFrame_GetLocalsArray(frame); int i = 0; @@ -31,7 +31,7 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame) assert(frame->frame_obj == NULL); PyObject *exc = PyErr_GetRaisedException(); - PyFrameObject *f = _PyFrame_New_NoTrack(frame->f_code); + PyFrameObject *f = _PyFrame_New_NoTrack(_PyFrame_GetCode(frame)); if (f == NULL) { Py_XDECREF(exc); return NULL; @@ -65,7 +65,7 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame) void _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest) { - assert(src->stacktop >= src->f_code->co_nlocalsplus); + assert(src->stacktop >= _PyFrame_GetCode(src)->co_nlocalsplus); Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src; memcpy(dest, src, size); // Don't leave a dangling pointer to the old frame when creating generators @@ -81,7 +81,7 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); assert(frame->owner != FRAME_CLEARED); Py_ssize_t size = ((char*)&frame->localsplus[frame->stacktop]) - (char *)frame; - Py_INCREF(frame->f_code); + Py_INCREF(_PyFrame_GetCode(frame)); memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size); frame = (_PyInterpreterFrame *)f->_f_frame_data; f->f_frame = frame; @@ -89,7 +89,7 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) if (_PyFrame_IsIncomplete(frame)) { // This may be a newly-created generator or coroutine frame. Since it's // dead anyways, just pretend that the first RESUME ran: - PyCodeObject *code = frame->f_code; + PyCodeObject *code = _PyFrame_GetCode(frame); frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable; } assert(!_PyFrame_IsIncomplete(frame)); @@ -148,5 +148,5 @@ int _PyInterpreterFrame_GetLine(_PyInterpreterFrame *frame) { int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT); - return PyCode_Addr2Line(frame->f_code, addr); + return PyCode_Addr2Line(_PyFrame_GetCode(frame), addr); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 8d35bc3aee7bad..1d2364f4ce3166 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -50,7 +50,7 @@ TARGET(LOAD_CONST) { PREDICTED(LOAD_CONST); PyObject *value; - value = GETITEM(frame->f_code->co_consts, oparg); + value = GETITEM(CONSTS(), oparg); Py_INCREF(value); STACK_GROW(1); stack_pointer[-1] = value; @@ -101,7 +101,7 @@ oparg = (next_instr++)->op.arg; { PyObject *value; - value = GETITEM(frame->f_code->co_consts, oparg); + value = GETITEM(CONSTS(), oparg); Py_INCREF(value); _tmp_1 = value; } @@ -150,7 +150,7 @@ PyObject *_tmp_2; { PyObject *value; - value = GETITEM(frame->f_code->co_consts, oparg); + value = GETITEM(CONSTS(), oparg); Py_INCREF(value); _tmp_2 = value; } @@ -724,8 +724,6 @@ PyObject *retval = stack_pointer[-1]; assert(frame == &entry_frame); assert(_PyFrame_IsIncomplete(frame)); - STACK_SHRINK(1); // Since we're not going to DISPATCH() - assert(EMPTY()); /* Restore previous cframe and return. */ tstate->cframe = cframe.previous; tstate->cframe->use_tracing = cframe.use_tracing; @@ -753,7 +751,7 @@ } TARGET(RETURN_CONST) { - PyObject *retval = GETITEM(frame->f_code->co_consts, oparg); + PyObject *retval = GETITEM(CONSTS(), oparg); Py_INCREF(retval); assert(EMPTY()); _PyFrame_SetStackPointer(frame, stack_pointer); @@ -993,7 +991,7 @@ if (oparg) { PyObject *lasti = values[0]; if (PyLong_Check(lasti)) { - frame->prev_instr = _PyCode_CODE(frame->f_code) + PyLong_AsLong(lasti); + frame->prev_instr = _PyCode_CODE(_PyFrame_GetCode(frame)) + PyLong_AsLong(lasti); assert(!_PyErr_Occurred(tstate)); } else { @@ -1092,7 +1090,7 @@ TARGET(STORE_NAME) { PyObject *v = stack_pointer[-1]; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -1112,7 +1110,7 @@ } TARGET(DELETE_NAME) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *ns = LOCALS(); int err; if (ns == NULL) { @@ -1226,7 +1224,7 @@ #if ENABLE_SPECIALIZATION if (ADAPTIVE_COUNTER_IS_ZERO(counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); next_instr--; _Py_Specialize_StoreAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1237,7 +1235,7 @@ #else (void)counter; // Unused. #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyObject_SetAttr(owner, name, v); Py_DECREF(v); Py_DECREF(owner); @@ -1249,7 +1247,7 @@ TARGET(DELETE_ATTR) { PyObject *owner = stack_pointer[-1]; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyObject_SetAttr(owner, name, (PyObject *)NULL); Py_DECREF(owner); if (err) goto pop_1_error; @@ -1259,7 +1257,7 @@ TARGET(STORE_GLOBAL) { PyObject *v = stack_pointer[-1]; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err = PyDict_SetItem(GLOBALS(), name, v); Py_DECREF(v); if (err) goto pop_1_error; @@ -1268,7 +1266,7 @@ } TARGET(DELETE_GLOBAL) { - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); int err; err = PyDict_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. @@ -1284,7 +1282,7 @@ TARGET(LOAD_NAME) { PyObject *v; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); PyObject *locals = LOCALS(); if (locals == NULL) { _PyErr_Format(tstate, PyExc_SystemError, @@ -1356,7 +1354,7 @@ _PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); next_instr--; _Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name); DISPATCH_SAME_OPARG(); @@ -1364,7 +1362,7 @@ STAT_INC(LOAD_GLOBAL, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); if (PyDict_CheckExact(GLOBALS()) && PyDict_CheckExact(BUILTINS())) { @@ -1489,7 +1487,7 @@ // Can't use ERROR_IF here. // Fortunately we don't need its superpower. if (oldobj == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); goto error; } PyCell_SET(cell, NULL); @@ -1501,8 +1499,8 @@ PyObject *value; PyObject *name, *locals = LOCALS(); assert(locals); - assert(oparg >= 0 && oparg < frame->f_code->co_nlocalsplus); - name = PyTuple_GET_ITEM(frame->f_code->co_localsplusnames, oparg); + assert(oparg >= 0 && oparg < _PyFrame_GetCode(frame)->co_nlocalsplus); + name = PyTuple_GET_ITEM(_PyFrame_GetCode(frame)->co_localsplusnames, oparg); if (PyDict_CheckExact(locals)) { value = PyDict_GetItemWithError(locals, name); if (value != NULL) { @@ -1525,7 +1523,7 @@ PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); if (value == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); goto error; } Py_INCREF(value); @@ -1540,7 +1538,7 @@ PyObject *cell = GETLOCAL(oparg); value = PyCell_GET(cell); if (value == NULL) { - format_exc_unbound(tstate, frame->f_code, oparg); + format_exc_unbound(tstate, _PyFrame_GetCode(frame), oparg); if (true) goto error; } Py_INCREF(value); @@ -1561,7 +1559,7 @@ TARGET(COPY_FREE_VARS) { /* Copy closure variables to free variables */ - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); assert(PyFunction_Check(frame->f_funcobj)); PyObject *closure = ((PyFunctionObject *)frame->f_funcobj)->func_closure; assert(oparg == co->co_nfreevars); @@ -1806,7 +1804,7 @@ _PyAttrCache *cache = (_PyAttrCache *)next_instr; if (ADAPTIVE_COUNTER_IS_ZERO(cache->counter)) { assert(cframe.use_tracing == 0); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); next_instr--; _Py_Specialize_LoadAttr(owner, next_instr, name); DISPATCH_SAME_OPARG(); @@ -1814,7 +1812,7 @@ STAT_INC(LOAD_ATTR, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); #endif /* ENABLE_SPECIALIZATION */ - PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); + PyObject *name = GETITEM(NAMES(), oparg >> 1); if (oparg & 1) { /* Designed to work in tandem with CALL, pushes two values. */ PyObject* meth = NULL; @@ -1925,7 +1923,7 @@ PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, LOAD_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(frame->f_code->co_names, oparg>>1); + PyObject *name = GETITEM(NAMES(), oparg>>1); uint16_t hint = index; DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, LOAD_ATTR); if (DK_IS_UNICODE(dict->ma_keys)) { @@ -2049,7 +2047,7 @@ DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize), LOAD_ATTR); STAT_INC(LOAD_ATTR, hit); - PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 1); + PyObject *name = GETITEM(NAMES(), oparg >> 1); Py_INCREF(f); _PyInterpreterFrame *new_frame = _PyFrame_PushUnchecked(tstate, f, 2); // Manipulate stack directly because we exit with DISPATCH_INLINED(). @@ -2105,7 +2103,7 @@ PyDictObject *dict = (PyDictObject *)_PyDictOrValues_GetDict(dorv); DEOPT_IF(dict == NULL, STORE_ATTR); assert(PyDict_CheckExact((PyObject *)dict)); - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); DEOPT_IF(hint >= (size_t)dict->ma_keys->dk_nentries, STORE_ATTR); PyObject *old_value; uint64_t new_version; @@ -2358,7 +2356,7 @@ PyObject *fromlist = stack_pointer[-1]; PyObject *level = stack_pointer[-2]; PyObject *res; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); res = import_name(tstate, frame, name, fromlist, level); Py_DECREF(level); Py_DECREF(fromlist); @@ -2371,7 +2369,7 @@ TARGET(IMPORT_FROM) { PyObject *from = stack_pointer[-1]; PyObject *res; - PyObject *name = GETITEM(frame->f_code->co_names, oparg); + PyObject *name = GETITEM(NAMES(), oparg); res = import_from(tstate, from, name); if (res == NULL) goto error; STACK_GROW(1); @@ -2621,7 +2619,7 @@ /* before: [obj]; after [getiter(obj)] */ if (PyCoro_CheckExact(iterable)) { /* `iterable` is a coroutine */ - if (!(frame->f_code->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { + if (!(_PyFrame_GetCode(frame)->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) { /* and it is used in a 'yield from' expression of a regular generator. */ _PyErr_SetString(tstate, PyExc_TypeError, @@ -3006,8 +3004,8 @@ TARGET(KW_NAMES) { assert(kwnames == NULL); - assert(oparg < PyTuple_GET_SIZE(frame->f_code->co_consts)); - kwnames = GETITEM(frame->f_code->co_consts, oparg); + assert(oparg < PyTuple_GET_SIZE(CONSTS())); + kwnames = GETITEM(CONSTS(), oparg); DISPATCH(); } diff --git a/Python/intrinsics.c b/Python/intrinsics.c index cca29d859902a4..a68f66555803c5 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -151,14 +151,14 @@ stopiteration_error(PyThreadState* tstate, PyObject *exc) const char *msg = NULL; if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) { msg = "generator raised StopIteration"; - if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) { + if (_PyFrame_GetCode(frame)->co_flags & CO_ASYNC_GENERATOR) { msg = "async generator raised StopIteration"; } - else if (frame->f_code->co_flags & CO_COROUTINE) { + else if (_PyFrame_GetCode(frame)->co_flags & CO_COROUTINE) { msg = "coroutine raised StopIteration"; } } - else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) && + else if ((_PyFrame_GetCode(frame)->co_flags & CO_ASYNC_GENERATOR) && PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) { /* code in `gen` raised a StopAsyncIteration error: diff --git a/Python/perf_trampoline.c b/Python/perf_trampoline.c index 1957ab82c33951..a4ab2e6e2a3f80 100644 --- a/Python/perf_trampoline.c +++ b/Python/perf_trampoline.c @@ -377,7 +377,7 @@ py_trampoline_evaluator(PyThreadState *ts, _PyInterpreterFrame *frame, perf_status == PERF_STATUS_NO_INIT) { goto default_eval; } - PyCodeObject *co = frame->f_code; + PyCodeObject *co = _PyFrame_GetCode(frame); py_trampoline f = NULL; assert(extra_code_index != -1); int ret = _PyCode_GetExtra((PyObject *)co, extra_code_index, (void **)&f); diff --git a/Python/traceback.c b/Python/traceback.c index 31b85e77575efa..a04703e22017a8 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -788,7 +788,7 @@ tb_displayline(PyTracebackObject* tb, PyObject *f, PyObject *filename, int linen } int code_offset = tb->tb_lasti; - PyCodeObject* code = frame->f_frame->f_code; + PyCodeObject* code = _PyFrame_GetCode(frame->f_frame); const Py_ssize_t source_line_len = PyUnicode_GET_LENGTH(source_line); int start_line; @@ -1167,7 +1167,7 @@ _Py_DumpASCII(int fd, PyObject *text) static void dump_frame(int fd, _PyInterpreterFrame *frame) { - PyCodeObject *code = frame->f_code; + PyCodeObject *code =_PyFrame_GetCode(frame); PUTS(fd, " File "); if (code->co_filename != NULL && PyUnicode_Check(code->co_filename)) diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py index 56d6970b29249c..a9eda8d5e8c8d0 100755 --- a/Tools/gdb/libpython.py +++ b/Tools/gdb/libpython.py @@ -1026,14 +1026,18 @@ def __init__(self, gdbval): self._gdbval = gdbval if not self.is_optimized_out(): - self.co = self._f_code() - self.co_name = self.co.pyop_field('co_name') - self.co_filename = self.co.pyop_field('co_filename') - - self.f_lasti = self._f_lasti() - self.co_nlocals = int_from_int(self.co.field('co_nlocals')) - pnames = self.co.field('co_localsplusnames') - self.co_localsplusnames = PyTupleObjectPtr.from_pyobject_ptr(pnames) + try: + self.co = self._f_code() + self.co_name = self.co.pyop_field('co_name') + self.co_filename = self.co.pyop_field('co_filename') + + self.f_lasti = self._f_lasti() + self.co_nlocals = int_from_int(self.co.field('co_nlocals')) + pnames = self.co.field('co_localsplusnames') + self.co_localsplusnames = PyTupleObjectPtr.from_pyobject_ptr(pnames) + self._is_code = True + except: + self._is_code = False def is_optimized_out(self): return self._gdbval.is_optimized_out @@ -1068,7 +1072,10 @@ def _f_builtins(self): return self._f_special("f_builtins") def _f_code(self): - return self._f_special("f_code", PyCodeObjectPtr.from_pyobject_ptr) + return self._f_special("f_executable", PyCodeObjectPtr.from_pyobject_ptr) + + def _f_executable(self): + return self._f_special("f_executable", PyObjectPtr.from_pyobject_ptr) def _f_nlocalsplus(self): return self._f_special("nlocalsplus", int_from_int) From 06e914b5af0c72b2862d37f1e02a04f0992894e0 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 15 Mar 2023 12:35:49 +0000 Subject: [PATCH 4/7] Allow minimal frames in frame stack. --- Include/cpython/ceval.h | 1 + Include/cpython/frameobject.h | 9 ------ Include/cpython/pystate.h | 4 ++- Include/internal/pycore_ceval.h | 2 ++ Include/internal/pycore_frame.h | 28 ++++++++++------ Lib/test/test_capi/test_misc.py | 1 + Modules/_tracemalloc.c | 2 +- Modules/_xxsubinterpretersmodule.c | 3 +- Objects/frameobject.c | 51 +++++++++++++----------------- Objects/genobject.c | 22 ++++++------- Python/bytecodes.c | 22 +++++++------ Python/ceval.c | 31 +++++++++--------- Python/ceval_macros.h | 9 +++--- Python/frame.c | 12 +++---- Python/generated_cases.c.h | 22 +++++++------ Python/intrinsics.c | 6 ++-- Python/pystate.c | 4 +-- Python/sysmodule.c | 19 ++++------- Python/traceback.c | 24 +++++--------- 19 files changed, 134 insertions(+), 138 deletions(-) diff --git a/Include/cpython/ceval.h b/Include/cpython/ceval.h index 0fbbee10c2edce..3dbc31af902aaf 100644 --- a/Include/cpython/ceval.h +++ b/Include/cpython/ceval.h @@ -2,6 +2,7 @@ # error "this header file must not be included directly" #endif + PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *); PyAPI_FUNC(void) PyEval_SetProfileAllThreads(Py_tracefunc, PyObject *); PyAPI_DATA(int) _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg); diff --git a/Include/cpython/frameobject.h b/Include/cpython/frameobject.h index 4e19535c656f2c..ddbb73887b325d 100644 --- a/Include/cpython/frameobject.h +++ b/Include/cpython/frameobject.h @@ -15,15 +15,6 @@ PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *, PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int); -/* -- Caveat emptor -- - * The concept of entry frames is an implementation detail of the CPython - * interpreter. This API is considered unstable and is provided for the - * convenience of debuggers, profilers and state-inspecting tools. Notice that - * this API can be changed in future minor versions if the underlying frame - * mechanism change or the concept of an 'entry frame' or its semantics becomes - * obsolete or outdated. */ - -PyAPI_FUNC(int) _PyFrame_IsEntryFrame(PyFrameObject *frame); PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f); PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *); diff --git a/Include/cpython/pystate.h b/Include/cpython/pystate.h index 3efb241e8237e7..2309d5e1b8ea6a 100644 --- a/Include/cpython/pystate.h +++ b/Include/cpython/pystate.h @@ -59,6 +59,8 @@ typedef int (*Py_tracefunc)(PyObject *, PyFrameObject *, int, PyObject *); #define PyTrace_OPCODE 7 +struct _PyInterpreterFrame; + typedef struct { PyCodeObject *code; // The code object for the bounds. May be NULL. PyCodeAddressRange bounds; // Only valid if code != NULL. @@ -79,7 +81,7 @@ typedef struct _PyCFrame { */ uint8_t use_tracing; // 0 or 255 (or'ed into opcode, hence 8-bit type) /* Pointer to the currently executing frame (it can be NULL) */ - struct _PyInterpreterFrame *current_frame; + struct _PyFrame *current_frame; struct _PyCFrame *previous; } _PyCFrame; diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index deda070a6dea79..b49c983b448d36 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -80,6 +80,8 @@ extern PyStatus _PyPerfTrampoline_AfterFork_Child(void); extern _PyPerf_Callbacks _Py_perfmap_callbacks; #endif +struct _PyInterpreterFrame; + static inline PyObject* _PyEval_EvalFrame(PyThreadState *tstate, struct _PyInterpreterFrame *frame, int throwflag) { diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h index 63f15aea854f9b..b072843ee3eca3 100644 --- a/Include/internal/pycore_frame.h +++ b/Include/internal/pycore_frame.h @@ -46,9 +46,13 @@ enum _frameowner { FRAME_OWNED_BY_CSTACK = 3, }; -typedef struct _PyInterpreterFrame { +typedef struct _PyFrame { PyObject *f_executable; /* Strong reference */ - struct _PyInterpreterFrame *previous; + struct _PyFrame *previous; +} _PyFrame; + +typedef struct _PyInterpreterFrame { + _PyFrame base; PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */ PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */ PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */ @@ -70,8 +74,8 @@ typedef struct _PyInterpreterFrame { ((int)((IF)->prev_instr - _PyCode_CODE(_PyFrame_GetCode(IF)))) static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) { - assert(PyCode_Check(f->f_executable)); - return (PyCodeObject *)f->f_executable; + assert(PyCode_Check(f->base.f_executable)); + return (PyCodeObject *)f->base.f_executable; } static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) { @@ -118,7 +122,7 @@ _PyFrame_Initialize( PyObject *locals, PyCodeObject *code, int null_locals_from) { frame->f_funcobj = (PyObject *)func; - frame->f_executable = Py_NewRef(code); + frame->base.f_executable = Py_NewRef(code); frame->f_builtins = func->func_builtins; frame->f_globals = func->func_globals; frame->f_locals = locals; @@ -163,8 +167,12 @@ _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer) * Frames owned by a generator are always complete. */ static inline bool -_PyFrame_IsIncomplete(_PyInterpreterFrame *frame) +_PyFrame_IsIncomplete(_PyFrame *frameptr) { + if (!PyCode_Check(frameptr->f_executable)) { + return true; + } + _PyInterpreterFrame *frame = (_PyInterpreterFrame *)frameptr; if (frame->owner == FRAME_OWNED_BY_CSTACK) { return true; } @@ -173,12 +181,12 @@ _PyFrame_IsIncomplete(_PyInterpreterFrame *frame) } static inline _PyInterpreterFrame * -_PyFrame_GetFirstComplete(_PyInterpreterFrame *frame) +_PyFrame_GetFirstComplete(_PyFrame *frame) { - while (frame && _PyFrame_IsIncomplete(frame)) { + while (frame != NULL && _PyFrame_IsIncomplete(frame)) { frame = frame->previous; } - return frame; + return (_PyInterpreterFrame *)frame; } static inline _PyInterpreterFrame * @@ -199,7 +207,7 @@ static inline PyFrameObject * _PyFrame_GetFrameObject(_PyInterpreterFrame *frame) { - assert(!_PyFrame_IsIncomplete(frame)); + assert(!_PyFrame_IsIncomplete(&frame->base)); PyFrameObject *res = frame->frame_obj; if (res != NULL) { return res; diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index c34ee578b5c83f..9ca563b6d4286d 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -262,6 +262,7 @@ def test_getitem_with_error(self): rc, out, err = assert_python_failure('-c', code) err = decode_stderr(err) if 'SystemError: ' not in err: + print(err) self.assertRegex(err, r'Fatal Python error: _Py_CheckSlotResult: ' r'Slot __getitem__ of type dict succeeded ' diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c index 6f36df2fb2e3f2..af38999c1dabca 100644 --- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -358,7 +358,7 @@ traceback_get_frames(traceback_t *traceback) if (traceback->total_nframe < UINT16_MAX) { traceback->total_nframe++; } - pyframe = _PyFrame_GetFirstComplete(pyframe->previous); + pyframe = _PyFrame_GetFirstComplete(pyframe->base.previous); } } diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c index 79dbe3474ba9e8..d38acf669ecfeb 100644 --- a/Modules/_xxsubinterpretersmodule.c +++ b/Modules/_xxsubinterpretersmodule.c @@ -407,8 +407,7 @@ _is_running(PyInterpreterState *interp) } assert(!PyErr_Occurred()); - _PyInterpreterFrame *frame = tstate->cframe->current_frame; - if (frame == NULL) { + if (tstate->cframe->current_frame == NULL) { return 0; } return 1; diff --git a/Objects/frameobject.c b/Objects/frameobject.c index d778efbcfad61a..d0b23d4949f64f 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -595,7 +595,7 @@ first_line_not_before(int *lines, int len, int line) static PyFrameState _PyFrame_GetState(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); if (frame->f_frame->stacktop == 0) { return FRAME_CLEARED; } @@ -881,8 +881,8 @@ frame_dealloc(PyFrameObject *f) assert(f->f_frame == (_PyInterpreterFrame *)f->_f_frame_data); _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f->_f_frame_data; /* Don't clear code object until the end */ - co = frame->f_executable; - frame->f_executable = NULL; + co = frame->base.f_executable; + frame->base.f_executable = NULL; Py_CLEAR(frame->f_funcobj); Py_CLEAR(frame->f_locals); PyObject **locals = _PyFrame_GetLocalsArray(frame); @@ -1024,7 +1024,7 @@ init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals) PyCodeObject *code = (PyCodeObject *)func->func_code; _PyFrame_Initialize(frame, (PyFunctionObject*)Py_NewRef(func), Py_XNewRef(locals), code, 0); - frame->previous = NULL; + frame->base.previous = NULL; } PyFrameObject* @@ -1078,7 +1078,7 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code, f->f_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT; // This frame needs to be "complete", so pretend that the first RESUME ran: f->f_frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable; - assert(!_PyFrame_IsIncomplete(f->f_frame)); + assert(!_PyFrame_IsIncomplete(&f->f_frame->base)); Py_DECREF(func); _PyObject_GC_TRACK(f); return f; @@ -1279,7 +1279,7 @@ PyFrame_GetVarString(PyFrameObject *frame, const char *name) int PyFrame_FastToLocalsWithError(PyFrameObject *f) { - assert(!_PyFrame_IsIncomplete(f->f_frame)); + assert(!_PyFrame_IsIncomplete(&f->f_frame->base)); if (f == NULL) { PyErr_BadInternalCall(); return -1; @@ -1295,7 +1295,7 @@ void PyFrame_FastToLocals(PyFrameObject *f) { int res; - assert(!_PyFrame_IsIncomplete(f->f_frame)); + assert(!_PyFrame_IsIncomplete(&f->f_frame->base)); assert(!PyErr_Occurred()); res = PyFrame_FastToLocalsWithError(f); @@ -1380,27 +1380,20 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear) void PyFrame_LocalsToFast(PyFrameObject *f, int clear) { - assert(!_PyFrame_IsIncomplete(f->f_frame)); + if (_PyFrame_IsIncomplete(&f->f_frame->base)) { + return; + } if (f && f->f_fast_as_locals && _PyFrame_GetState(f) != FRAME_CLEARED) { - _PyFrame_LocalsToFast(f->f_frame, clear); + _PyFrame_LocalsToFast((_PyInterpreterFrame *)f->f_frame, clear); f->f_fast_as_locals = 0; } } -int -_PyFrame_IsEntryFrame(PyFrameObject *frame) -{ - assert(frame != NULL); - _PyInterpreterFrame *f = frame->f_frame; - assert(!_PyFrame_IsIncomplete(f)); - return f->previous && f->previous->owner == FRAME_OWNED_BY_CSTACK; -} - PyCodeObject * PyFrame_GetCode(PyFrameObject *frame) { assert(frame != NULL); - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); PyCodeObject *code = _PyFrame_GetCode(frame->f_frame); assert(code != NULL); return (PyCodeObject*)Py_NewRef(code); @@ -1411,13 +1404,13 @@ PyFrameObject* PyFrame_GetBack(PyFrameObject *frame) { assert(frame != NULL); - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); PyFrameObject *back = frame->f_back; if (back == NULL) { - _PyInterpreterFrame *prev = frame->f_frame->previous; - prev = _PyFrame_GetFirstComplete(prev); - if (prev) { - back = _PyFrame_GetFrameObject(prev); + _PyFrame *prev = frame->f_frame->base.previous; + _PyInterpreterFrame *py_prev = _PyFrame_GetFirstComplete(prev); + if (py_prev) { + back = _PyFrame_GetFrameObject(py_prev); } } return (PyFrameObject*)Py_XNewRef(back); @@ -1426,28 +1419,28 @@ PyFrame_GetBack(PyFrameObject *frame) PyObject* PyFrame_GetLocals(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); return frame_getlocals(frame, NULL); } PyObject* PyFrame_GetGlobals(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); return frame_getglobals(frame, NULL); } PyObject* PyFrame_GetBuiltins(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); return frame_getbuiltins(frame, NULL); } int PyFrame_GetLasti(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); int lasti = _PyInterpreterFrame_LASTI(frame->f_frame); if (lasti < 0) { return -1; @@ -1458,7 +1451,7 @@ PyFrame_GetLasti(PyFrameObject *frame) PyObject * PyFrame_GetGenerator(PyFrameObject *frame) { - assert(!_PyFrame_IsIncomplete(frame->f_frame)); + assert(!_PyFrame_IsIncomplete(&frame->f_frame->base)); if (frame->f_frame->owner != FRAME_OWNED_BY_GENERATOR) { return NULL; } diff --git a/Objects/genobject.c b/Objects/genobject.c index 3d2777bf599110..264200daad81fe 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -146,7 +146,7 @@ gen_dealloc(PyGenObject *gen) if (gen->gi_frame_state < FRAME_CLEARED) { _PyInterpreterFrame *frame = (_PyInterpreterFrame *)gen->gi_iframe; gen->gi_frame_state = FRAME_CLEARED; - frame->previous = NULL; + frame->base.previous = NULL; _PyFrame_ClearExceptCode(frame); } if (_PyGen_GetCode(gen)->co_flags & CO_COROUTINE) { @@ -230,7 +230,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, assert(tstate->exc_info == prev_exc_info); assert(gen->gi_exc_state.previous_item == NULL); assert(gen->gi_frame_state != FRAME_EXECUTING); - assert(frame->previous == NULL); + assert(frame->base.previous == NULL); /* If the generator just returned (as opposed to yielding), signal * that the generator is exhausted. */ @@ -459,9 +459,9 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, will be reported correctly to the user. */ /* XXX We should probably be updating the current frame somewhere in ceval.c. */ - _PyInterpreterFrame *prev = tstate->cframe->current_frame; - frame->previous = prev; - tstate->cframe->current_frame = frame; + _PyFrame *prev = tstate->cframe->current_frame; + frame->base.previous = prev; + tstate->cframe->current_frame = &frame->base; /* Close the generator that we are currently iterating with 'yield from' or awaiting on with 'await'. */ PyFrameState state = gen->gi_frame_state; @@ -470,7 +470,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, typ, val, tb); gen->gi_frame_state = state; tstate->cframe->current_frame = prev; - frame->previous = NULL; + frame->base.previous = NULL; } else { /* `yf` is an iterator or a coroutine-like object. */ PyObject *meth; @@ -921,11 +921,11 @@ _Py_MakeCoro(PyFunctionObject *func) if (origin_depth == 0) { ((PyCoroObject *)coro)->cr_origin_or_finalizer = NULL; } else { - _PyInterpreterFrame *frame = tstate->cframe->current_frame; + _PyFrame *frame = tstate->cframe->current_frame; assert(frame); assert(_PyFrame_IsIncomplete(frame)); - frame = _PyFrame_GetFirstComplete(frame->previous); - PyObject *cr_origin = compute_cr_origin(origin_depth, frame); + _PyInterpreterFrame * pyframe = _PyFrame_GetFirstComplete(frame->previous); + PyObject *cr_origin = compute_cr_origin(origin_depth, pyframe); ((PyCoroObject *)coro)->cr_origin_or_finalizer = cr_origin; if (!cr_origin) { Py_DECREF(coro); @@ -1311,7 +1311,7 @@ compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame) /* First count how many frames we have */ int frame_count = 0; for (; frame && frame_count < origin_depth; ++frame_count) { - frame = _PyFrame_GetFirstComplete(frame->previous); + frame = _PyFrame_GetFirstComplete(frame->base.previous); } /* Now collect them */ @@ -1330,7 +1330,7 @@ compute_cr_origin(int origin_depth, _PyInterpreterFrame *current_frame) return NULL; } PyTuple_SET_ITEM(cr_origin, i, frameinfo); - frame = _PyFrame_GetFirstComplete(frame->previous); + frame = _PyFrame_GetFirstComplete(frame->base.previous); } return cr_origin; diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 45737a5d037297..e75f3d7988d816 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -87,7 +87,7 @@ dummy_func( inst(RESUME, (--)) { assert(tstate->cframe == &cframe); - assert(frame == cframe.current_frame); + assert(&frame->base == cframe.current_frame); if (_Py_atomic_load_relaxed_int32(eval_breaker) && oparg < 2) { goto handle_eval_breaker; } @@ -532,11 +532,11 @@ dummy_func( inst(INTERPRETER_EXIT, (retval --)) { assert(frame == &entry_frame); - assert(_PyFrame_IsIncomplete(frame)); + assert(_PyFrame_IsIncomplete(&frame->base)); /* Restore previous cframe and return. */ tstate->cframe = cframe.previous; tstate->cframe->use_tracing = cframe.use_tracing; - assert(tstate->cframe->current_frame == frame->previous); + assert(tstate->cframe->current_frame == frame->base.previous); assert(!_PyErr_Occurred(tstate)); _Py_LeaveRecursiveCallTstate(tstate); return retval; @@ -552,7 +552,8 @@ dummy_func( assert(frame != &entry_frame); // GH-99729: We need to unlink the frame *before* clearing it: _PyInterpreterFrame *dying = frame; - frame = cframe.current_frame = dying->previous; + frame = (_PyInterpreterFrame *)dying->base.previous; + cframe.current_frame = &frame->base; _PyEvalFrameClearAndPop(tstate, dying); _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -569,7 +570,8 @@ dummy_func( assert(frame != &entry_frame); // GH-99729: We need to unlink the frame *before* clearing it: _PyInterpreterFrame *dying = frame; - frame = cframe.current_frame = dying->previous; + frame = (_PyInterpreterFrame *)dying->base.previous; + cframe.current_frame = &frame->base; _PyEvalFrameClearAndPop(tstate, dying); _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -758,8 +760,9 @@ dummy_func( gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); _PyInterpreterFrame *gen_frame = frame; - frame = cframe.current_frame = frame->previous; - gen_frame->previous = NULL; + frame = (_PyInterpreterFrame *)frame->base.previous; + cframe.current_frame = &frame->base; + gen_frame->base.previous = NULL; frame->prev_instr -= frame->yield_offset; _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -3007,9 +3010,10 @@ dummy_func( gen_frame->owner = FRAME_OWNED_BY_GENERATOR; _Py_LeaveRecursiveCallPy(tstate); assert(frame != &entry_frame); - _PyInterpreterFrame *prev = frame->previous; + _PyInterpreterFrame *prev = (_PyInterpreterFrame *)frame->base.previous; _PyThreadState_PopFrame(tstate, frame); - frame = cframe.current_frame = prev; + frame = prev; + cframe.current_frame = &prev->base; _PyFrame_StackPush(frame, (PyObject *)gen); goto resume_frame; } diff --git a/Python/ceval.c b/Python/ceval.c index 0552c8105e64a4..734aadc625aba2 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -746,16 +746,16 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int entry_frame.f_globals = (PyObject*)0xaaa3; entry_frame.f_builtins = (PyObject*)0xaaa4; #endif - entry_frame.f_executable = Py_None; + entry_frame.base.f_executable = Py_None; entry_frame.prev_instr = _PyCode_CODE(tstate->interp->interpreter_trampoline); entry_frame.stacktop = 0; entry_frame.owner = FRAME_OWNED_BY_CSTACK; entry_frame.yield_offset = 0; /* Push frame */ - entry_frame.previous = prev_cframe->current_frame; - frame->previous = &entry_frame; - cframe.current_frame = frame; + entry_frame.base.previous = prev_cframe->current_frame; + frame->base.previous = &entry_frame.base; + cframe.current_frame = &frame->base; if (_Py_EnterRecursiveCallTstate(tstate, "")) { tstate->c_recursion_remaining--; @@ -984,7 +984,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int /* Log traceback info. */ assert(frame != &entry_frame); - if (!_PyFrame_IsIncomplete(frame)) { + if (!_PyFrame_IsIncomplete(&frame->base)) { PyFrameObject *f = _PyFrame_GetFrameObject(frame); if (f != NULL) { PyTraceBack_Here(f); @@ -1051,13 +1051,14 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int assert(frame != &entry_frame); // GH-99729: We need to unlink the frame *before* clearing it: _PyInterpreterFrame *dying = frame; - frame = cframe.current_frame = dying->previous; + cframe.current_frame = dying->base.previous; + frame = (_PyInterpreterFrame *)cframe.current_frame; _PyEvalFrameClearAndPop(tstate, dying); if (frame == &entry_frame) { /* Restore previous cframe and exit */ tstate->cframe = cframe.previous; tstate->cframe->use_tracing = cframe.use_tracing; - assert(tstate->cframe->current_frame == frame->previous); + assert(tstate->cframe->current_frame == frame->base.previous); _Py_LeaveRecursiveCallTstate(tstate); return NULL; } @@ -1600,7 +1601,7 @@ clear_thread_frame(PyThreadState *tstate, _PyInterpreterFrame * frame) tstate->c_recursion_remaining--; assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame); _PyFrame_ClearExceptCode(frame); - Py_DECREF(frame->f_executable); + Py_DECREF(frame->base.f_executable); tstate->c_recursion_remaining++; _PyThreadState_PopFrame(tstate, frame); } @@ -1618,7 +1619,7 @@ clear_gen_frame(PyThreadState *tstate, _PyInterpreterFrame * frame) assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame); _PyFrame_ClearExceptCode(frame); tstate->c_recursion_remaining++; - frame->previous = NULL; + frame->base.previous = NULL; } static void @@ -2457,11 +2458,11 @@ int PyEval_MergeCompilerFlags(PyCompilerFlags *cf) { PyThreadState *tstate = _PyThreadState_GET(); - _PyInterpreterFrame *current_frame = tstate->cframe->current_frame; + _PyFrame *current_frame = tstate->cframe->current_frame; int result = cf->cf_flags != 0; - if (current_frame != NULL) { - const int codeflags = _PyFrame_GetCode(current_frame)->co_flags; + if (current_frame != NULL && PyCode_Check(current_frame->f_executable)) { + const int codeflags = ((PyCodeObject *)current_frame->f_executable)->co_flags; const int compilerflags = codeflags & PyCF_MASK; if (compilerflags) { result = 1; @@ -2501,7 +2502,7 @@ PyEval_GetFuncDesc(PyObject *func) #define C_TRACE(x, call) \ if (use_tracing && tstate->c_profilefunc) { \ if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \ - tstate, tstate->cframe->current_frame, \ + tstate, (_PyInterpreterFrame *)tstate->cframe->current_frame, \ PyTrace_C_CALL, func)) { \ x = NULL; \ } \ @@ -2511,13 +2512,13 @@ if (use_tracing && tstate->c_profilefunc) { \ if (x == NULL) { \ call_trace_protected(tstate->c_profilefunc, \ tstate->c_profileobj, \ - tstate, tstate->cframe->current_frame, \ + tstate, (_PyInterpreterFrame *)tstate->cframe->current_frame, \ PyTrace_C_EXCEPTION, func); \ /* XXX should pass (type, value, tb) */ \ } else { \ if (call_trace(tstate->c_profilefunc, \ tstate->c_profileobj, \ - tstate, tstate->cframe->current_frame, \ + tstate, (_PyInterpreterFrame *)tstate->cframe->current_frame, \ PyTrace_C_RETURN, func)) { \ Py_DECREF(x); \ x = NULL; \ diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index d551eaf6b27af1..e25c764dacf94f 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -110,8 +110,9 @@ do { \ _PyFrame_SetStackPointer(frame, stack_pointer); \ frame->prev_instr = next_instr - 1; \ - (NEW_FRAME)->previous = frame; \ - frame = cframe.current_frame = (NEW_FRAME); \ + (NEW_FRAME)->base.previous = &frame->base; \ + frame = (NEW_FRAME); \ + cframe.current_frame = &frame->base; \ CALL_STAT_INC(inlined_py_calls); \ goto start_frame; \ } while (0) @@ -282,8 +283,8 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define GLOBALS() frame->f_globals #define BUILTINS() frame->f_builtins #define LOCALS() frame->f_locals -#define CONSTS() ((PyCodeObject *)frame->f_executable)->co_consts -#define NAMES() ((PyCodeObject *)frame->f_executable)->co_names +#define CONSTS() ((PyCodeObject *)frame->base.f_executable)->co_consts +#define NAMES() ((PyCodeObject *)frame->base.f_executable)->co_names /* Shared opcode macros */ diff --git a/Python/frame.c b/Python/frame.c index bffd705d1298f7..58e310dfb66728 100644 --- a/Python/frame.c +++ b/Python/frame.c @@ -70,7 +70,7 @@ _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest) memcpy(dest, src, size); // Don't leave a dangling pointer to the old frame when creating generators // and coroutines: - dest->previous = NULL; + dest->base.previous = NULL; } @@ -86,16 +86,16 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) frame = (_PyInterpreterFrame *)f->_f_frame_data; f->f_frame = frame; frame->owner = FRAME_OWNED_BY_FRAME_OBJECT; - if (_PyFrame_IsIncomplete(frame)) { + if (_PyFrame_IsIncomplete(&frame->base)) { // This may be a newly-created generator or coroutine frame. Since it's // dead anyways, just pretend that the first RESUME ran: PyCodeObject *code = _PyFrame_GetCode(frame); frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable; } - assert(!_PyFrame_IsIncomplete(frame)); + assert(!_PyFrame_IsIncomplete(&frame->base)); assert(f->f_back == NULL); - _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous); - frame->previous = NULL; + _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->base.previous); + frame->base.previous = NULL; if (prev) { assert(prev->owner != FRAME_OWNED_BY_CSTACK); /* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */ @@ -124,7 +124,7 @@ _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame) _PyFrame_GetGenerator(frame)->gi_frame_state == FRAME_CLEARED); // GH-99729: Clearing this frame can expose the stack (via finalizers). It's // crucial that this frame has been unlinked, and is no longer visible: - assert(_PyThreadState_GET()->cframe->current_frame != frame); + assert(_PyThreadState_GET()->cframe->current_frame != &frame->base); if (frame->frame_obj) { PyFrameObject *f = frame->frame_obj; frame->frame_obj = NULL; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 1d2364f4ce3166..47a1848a335981 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -9,7 +9,7 @@ TARGET(RESUME) { assert(tstate->cframe == &cframe); - assert(frame == cframe.current_frame); + assert(&frame->base == cframe.current_frame); if (_Py_atomic_load_relaxed_int32(eval_breaker) && oparg < 2) { goto handle_eval_breaker; } @@ -723,11 +723,11 @@ TARGET(INTERPRETER_EXIT) { PyObject *retval = stack_pointer[-1]; assert(frame == &entry_frame); - assert(_PyFrame_IsIncomplete(frame)); + assert(_PyFrame_IsIncomplete(&frame->base)); /* Restore previous cframe and return. */ tstate->cframe = cframe.previous; tstate->cframe->use_tracing = cframe.use_tracing; - assert(tstate->cframe->current_frame == frame->previous); + assert(tstate->cframe->current_frame == frame->base.previous); assert(!_PyErr_Occurred(tstate)); _Py_LeaveRecursiveCallTstate(tstate); return retval; @@ -744,7 +744,8 @@ assert(frame != &entry_frame); // GH-99729: We need to unlink the frame *before* clearing it: _PyInterpreterFrame *dying = frame; - frame = cframe.current_frame = dying->previous; + frame = (_PyInterpreterFrame *)dying->base.previous; + cframe.current_frame = &frame->base; _PyEvalFrameClearAndPop(tstate, dying); _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -761,7 +762,8 @@ assert(frame != &entry_frame); // GH-99729: We need to unlink the frame *before* clearing it: _PyInterpreterFrame *dying = frame; - frame = cframe.current_frame = dying->previous; + frame = (_PyInterpreterFrame *)dying->base.previous; + cframe.current_frame = &frame->base; _PyEvalFrameClearAndPop(tstate, dying); _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -969,8 +971,9 @@ gen->gi_exc_state.previous_item = NULL; _Py_LeaveRecursiveCallPy(tstate); _PyInterpreterFrame *gen_frame = frame; - frame = cframe.current_frame = frame->previous; - gen_frame->previous = NULL; + frame = (_PyInterpreterFrame *)frame->base.previous; + cframe.current_frame = &frame->base; + gen_frame->base.previous = NULL; frame->prev_instr -= frame->yield_offset; _PyFrame_StackPush(frame, retval); goto resume_frame; @@ -3766,9 +3769,10 @@ gen_frame->owner = FRAME_OWNED_BY_GENERATOR; _Py_LeaveRecursiveCallPy(tstate); assert(frame != &entry_frame); - _PyInterpreterFrame *prev = frame->previous; + _PyInterpreterFrame *prev = (_PyInterpreterFrame *)frame->base.previous; _PyThreadState_PopFrame(tstate, frame); - frame = cframe.current_frame = prev; + frame = prev; + cframe.current_frame = &prev->base; _PyFrame_StackPush(frame, (PyObject *)gen); goto resume_frame; } diff --git a/Python/intrinsics.c b/Python/intrinsics.c index a68f66555803c5..f730c174f65a86 100644 --- a/Python/intrinsics.c +++ b/Python/intrinsics.c @@ -123,7 +123,8 @@ import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v) static PyObject * import_star(PyThreadState* tstate, PyObject *from) { - _PyInterpreterFrame *frame = tstate->cframe->current_frame; + _PyInterpreterFrame *frame = (_PyInterpreterFrame *)tstate->cframe->current_frame; + assert(PyCode_Check(frame->base.f_executable)); if (_PyFrame_FastToLocalsWithError(frame) < 0) { return NULL; } @@ -145,7 +146,8 @@ import_star(PyThreadState* tstate, PyObject *from) static PyObject * stopiteration_error(PyThreadState* tstate, PyObject *exc) { - _PyInterpreterFrame *frame = tstate->cframe->current_frame; + _PyInterpreterFrame *frame = (_PyInterpreterFrame *)tstate->cframe->current_frame; + assert(PyCode_Check(frame->base.f_executable)); assert(frame->owner == FRAME_OWNED_BY_GENERATOR); assert(PyExceptionInstance_Check(exc)); const char *msg = NULL; diff --git a/Python/pystate.c b/Python/pystate.c index 28606e4f32f71c..4ce4d72fe74d21 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -1830,8 +1830,8 @@ _PyThread_CurrentFrames(void) for (i = runtime->interpreters.head; i != NULL; i = i->next) { PyThreadState *t; for (t = i->threads.head; t != NULL; t = t->next) { - _PyInterpreterFrame *frame = t->cframe->current_frame; - frame = _PyFrame_GetFirstComplete(frame); + _PyInterpreterFrame *frame = + _PyFrame_GetFirstComplete(t->cframe->current_frame); if (frame == NULL) { continue; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 207abb964bcac9..0b820f1600a629 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1891,16 +1891,11 @@ sys__getframe_impl(PyObject *module, int depth) /*[clinic end generated code: output=d438776c04d59804 input=c1be8a6464b11ee5]*/ { PyThreadState *tstate = _PyThreadState_GET(); - _PyInterpreterFrame *frame = tstate->cframe->current_frame; + _PyInterpreterFrame *frame = _PyFrame_GetFirstComplete(tstate->cframe->current_frame); - if (frame != NULL) { - while (depth > 0) { - frame = _PyFrame_GetFirstComplete(frame->previous); - if (frame == NULL) { - break; - } - --depth; - } + while (depth > 0 && frame != NULL) { + frame = _PyFrame_GetFirstComplete(frame->base.previous); + --depth; } if (frame == NULL) { _PyErr_SetString(tstate, PyExc_ValueError, @@ -2204,14 +2199,14 @@ sys__getframemodulename_impl(PyObject *module, int depth) if (PySys_Audit("sys._getframemodulename", "i", depth) < 0) { return NULL; } - _PyInterpreterFrame *f = _PyThreadState_GET()->cframe->current_frame; + _PyFrame *f = _PyThreadState_GET()->cframe->current_frame; while (f && (_PyFrame_IsIncomplete(f) || depth-- > 0)) { f = f->previous; } - if (f == NULL || f->f_funcobj == NULL) { + if (f == NULL || ((_PyInterpreterFrame *)f)->f_funcobj == NULL) { Py_RETURN_NONE; } - PyObject *r = PyFunction_GetModule(f->f_funcobj); + PyObject *r = PyFunction_GetModule(((_PyInterpreterFrame *)f)->f_funcobj); if (!r) { PyErr_Clear(); r = Py_None; diff --git a/Python/traceback.c b/Python/traceback.c index a04703e22017a8..fffe07c52d02c9 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -1165,8 +1165,12 @@ _Py_DumpASCII(int fd, PyObject *text) This function is signal safe. */ static void -dump_frame(int fd, _PyInterpreterFrame *frame) +dump_frame(int fd, _PyFrame *f) { + if (!PyCode_Check(f->f_executable)) { + return; + } + _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f; PyCodeObject *code =_PyFrame_GetCode(frame); PUTS(fd, " File "); if (code->co_filename != NULL @@ -1203,7 +1207,7 @@ dump_frame(int fd, _PyInterpreterFrame *frame) static void dump_traceback(int fd, PyThreadState *tstate, int write_header) { - _PyInterpreterFrame *frame; + _PyFrame *frame; unsigned int depth; if (write_header) { @@ -1217,26 +1221,14 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header) } depth = 0; - while (1) { + while (frame) { if (MAX_FRAME_DEPTH <= depth) { PUTS(fd, " ...\n"); break; } dump_frame(fd, frame); frame = frame->previous; - if (frame == NULL) { - break; - } - if (frame->owner == FRAME_OWNED_BY_CSTACK) { - /* Trampoline frame */ - frame = frame->previous; - } - if (frame == NULL) { - break; - } - /* Can't have more than one shim frame in a row */ - assert(frame->owner != FRAME_OWNED_BY_CSTACK); - depth++; + depth ++; } } From 3e2c3ab022aaa767bd9ab8530a12570f594dba2f Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 15 Mar 2023 19:10:51 +0000 Subject: [PATCH 5/7] Insert introspectable frames for each non-Python call from interpreter. --- Lib/test/test_capi/test_misc.py | 4 +++- Lib/test/test_faulthandler.py | 7 +++++++ Python/bytecodes.c | 23 +++++++++++--------- Python/ceval_macros.h | 12 +++++++++++ Python/generated_cases.c.h | 23 +++++++++++--------- Python/traceback.c | 37 +++++++++++++++++++++++++++------ 6 files changed, 79 insertions(+), 27 deletions(-) diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index 9ca563b6d4286d..eb9965d770dc46 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -206,6 +206,7 @@ def test_return_null_without_error(self): r'returned NULL without setting an exception\n' r'\n' r'Current thread.*:\n' + r' Builtin function return_null_without_error\n' r' File .*", line 6 in \n') else: with self.assertRaises(SystemError) as cm: @@ -240,6 +241,7 @@ def test_return_result_with_error(self): r'returned a result with an exception set\n' r'\n' r'Current thread.*:\n' + r' Builtin function return_result_with_error\n' r' File .*, line 6 in \n') else: with self.assertRaises(SystemError) as cm: @@ -262,7 +264,6 @@ def test_getitem_with_error(self): rc, out, err = assert_python_failure('-c', code) err = decode_stderr(err) if 'SystemError: ' not in err: - print(err) self.assertRegex(err, r'Fatal Python error: _Py_CheckSlotResult: ' r'Slot __getitem__ of type dict succeeded ' @@ -271,6 +272,7 @@ def test_getitem_with_error(self): r'ValueError: bug\n' r'\n' r'Current thread .* \(most recent call first\):\n' + r' Builtin function getitem_with_error\n' r' File .*, line 6 in \n' r'\n' r'Extension modules: _testcapi \(total: 1\)\n') diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 8d106daaf6520a..048ada77e59867 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -489,6 +489,13 @@ def funcA(): ' File "", line 19 in ' ] trace, exitcode = self.get_output(code, filename, fd) + if trace != expected: + print("Trace") + print("-----------------") + print(trace) + print("Expected") + print("-----------------") + print(expected) self.assertEqual(trace, expected) self.assertEqual(exitcode, 0) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index e75f3d7988d816..2e13a07050a934 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2455,6 +2455,7 @@ dummy_func( JUMPBY(INLINE_CACHE_ENTRIES_CALL); DISPATCH_INLINED(new_frame); } + /* Callable is not a normal Python function */ if (cframe.use_tracing) { res = trace_call_function( @@ -2462,10 +2463,10 @@ dummy_func( positional_args, kwnames); } else { - res = PyObject_Vectorcall( + CALL_WITH_NATIVE_FRAME(callable, res = PyObject_Vectorcall( callable, args, positional_args | PY_VECTORCALL_ARGUMENTS_OFFSET, - kwnames); + kwnames)); } kwnames = NULL; assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); @@ -2606,8 +2607,8 @@ dummy_func( PyTypeObject *tp = (PyTypeObject *)callable; DEOPT_IF(tp->tp_vectorcall == NULL, CALL); STAT_INC(CALL, hit); - res = tp->tp_vectorcall((PyObject *)tp, args, - total_args - kwnames_len, kwnames); + CALL_WITH_NATIVE_FRAME(callable, res = tp->tp_vectorcall((PyObject *)tp, args, + total_args - kwnames_len, kwnames)); kwnames = NULL; /* Free the arguments. */ for (int i = 0; i < total_args; i++) { @@ -2640,7 +2641,7 @@ dummy_func( goto error; } PyObject *arg = args[0]; - res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg); + CALL_WITH_NATIVE_FRAME(callable, res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg)); _Py_LeaveRecursiveCallTstate(tstate); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); @@ -2666,10 +2667,10 @@ dummy_func( STAT_INC(CALL, hit); PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable); /* res = func(self, args, nargs) */ - res = ((_PyCFunctionFast)(void(*)(void))cfunc)( + CALL_WITH_NATIVE_FRAME(callable, res = ((_PyCFunctionFast)(void(*)(void))cfunc)( PyCFunction_GET_SELF(callable), args, - total_args); + total_args)); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); /* Free the arguments. */ @@ -2704,12 +2705,12 @@ dummy_func( _PyCFunctionFastWithKeywords cfunc = (_PyCFunctionFastWithKeywords)(void(*)(void)) PyCFunction_GET_FUNCTION(callable); - res = cfunc( + CALL_WITH_NATIVE_FRAME(callable, res = cfunc( PyCFunction_GET_SELF(callable), args, total_args - KWNAMES_LEN(), kwnames - ); + )); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); kwnames = NULL; @@ -2738,7 +2739,8 @@ dummy_func( DEOPT_IF(callable != interp->callable_cache.len, CALL); STAT_INC(CALL, hit); PyObject *arg = args[0]; - Py_ssize_t len_i = PyObject_Length(arg); + Py_ssize_t len_i; + CALL_WITH_NATIVE_FRAME(callable, len_i = PyObject_Length(arg)); if (len_i < 0) { goto error; } @@ -2767,6 +2769,7 @@ dummy_func( STAT_INC(CALL, hit); PyObject *cls = args[1]; PyObject *inst = args[0]; + int retval = PyObject_IsInstance(inst, cls); if (retval < 0) { goto error; diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index e25c764dacf94f..e608a03b3f5672 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -353,3 +353,15 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define KWNAMES_LEN() \ (kwnames == NULL ? 0 : ((int)PyTuple_GET_SIZE(kwnames))) + + +#define CALL_WITH_NATIVE_FRAME(_callable, _call) \ + do { \ + assert(cframe.current_frame == &frame->base); \ + _PyFrame _bframe; \ + _bframe.f_executable = _callable; \ + _bframe.previous = &frame->base; \ + cframe.current_frame = &_bframe; \ + (_call); \ + cframe.current_frame = &frame->base; \ + } while (0) diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 47a1848a335981..f190c6c05574dc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -3071,6 +3071,7 @@ JUMPBY(INLINE_CACHE_ENTRIES_CALL); DISPATCH_INLINED(new_frame); } + /* Callable is not a normal Python function */ if (cframe.use_tracing) { res = trace_call_function( @@ -3078,10 +3079,10 @@ positional_args, kwnames); } else { - res = PyObject_Vectorcall( + CALL_WITH_NATIVE_FRAME(callable, res = PyObject_Vectorcall( callable, args, positional_args | PY_VECTORCALL_ARGUMENTS_OFFSET, - kwnames); + kwnames)); } kwnames = NULL; assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); @@ -3267,8 +3268,8 @@ PyTypeObject *tp = (PyTypeObject *)callable; DEOPT_IF(tp->tp_vectorcall == NULL, CALL); STAT_INC(CALL, hit); - res = tp->tp_vectorcall((PyObject *)tp, args, - total_args - kwnames_len, kwnames); + CALL_WITH_NATIVE_FRAME(callable, res = tp->tp_vectorcall((PyObject *)tp, args, + total_args - kwnames_len, kwnames)); kwnames = NULL; /* Free the arguments. */ for (int i = 0; i < total_args; i++) { @@ -3310,7 +3311,7 @@ goto error; } PyObject *arg = args[0]; - res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg); + CALL_WITH_NATIVE_FRAME(callable, res = _PyCFunction_TrampolineCall(cfunc, PyCFunction_GET_SELF(callable), arg)); _Py_LeaveRecursiveCallTstate(tstate); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); @@ -3345,10 +3346,10 @@ STAT_INC(CALL, hit); PyCFunction cfunc = PyCFunction_GET_FUNCTION(callable); /* res = func(self, args, nargs) */ - res = ((_PyCFunctionFast)(void(*)(void))cfunc)( + CALL_WITH_NATIVE_FRAME(callable, res = ((_PyCFunctionFast)(void(*)(void))cfunc)( PyCFunction_GET_SELF(callable), args, - total_args); + total_args)); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); /* Free the arguments. */ @@ -3392,12 +3393,12 @@ _PyCFunctionFastWithKeywords cfunc = (_PyCFunctionFastWithKeywords)(void(*)(void)) PyCFunction_GET_FUNCTION(callable); - res = cfunc( + CALL_WITH_NATIVE_FRAME(callable, res = cfunc( PyCFunction_GET_SELF(callable), args, total_args - KWNAMES_LEN(), kwnames - ); + )); assert((res != NULL) ^ (_PyErr_Occurred(tstate) != NULL)); kwnames = NULL; @@ -3435,7 +3436,8 @@ DEOPT_IF(callable != interp->callable_cache.len, CALL); STAT_INC(CALL, hit); PyObject *arg = args[0]; - Py_ssize_t len_i = PyObject_Length(arg); + Py_ssize_t len_i; + CALL_WITH_NATIVE_FRAME(callable, len_i = PyObject_Length(arg)); if (len_i < 0) { goto error; } @@ -3473,6 +3475,7 @@ STAT_INC(CALL, hit); PyObject *cls = args[1]; PyObject *inst = args[0]; + int retval = PyObject_IsInstance(inst, cls); if (retval < 0) { goto error; diff --git a/Python/traceback.c b/Python/traceback.c index fffe07c52d02c9..f6eb2212f6a2c4 100644 --- a/Python/traceback.c +++ b/Python/traceback.c @@ -1165,14 +1165,10 @@ _Py_DumpASCII(int fd, PyObject *text) This function is signal safe. */ static void -dump_frame(int fd, _PyFrame *f) +dump_pyframe(int fd, _PyInterpreterFrame *frame) { - if (!PyCode_Check(f->f_executable)) { - return; - } - _PyInterpreterFrame *frame = (_PyInterpreterFrame *)f; - PyCodeObject *code =_PyFrame_GetCode(frame); PUTS(fd, " File "); + PyCodeObject *code =_PyFrame_GetCode(frame); if (code->co_filename != NULL && PyUnicode_Check(code->co_filename)) { @@ -1204,6 +1200,35 @@ dump_frame(int fd, _PyFrame *f) PUTS(fd, "\n"); } +static void +dump_frame(int fd, _PyFrame *f) +{ + PyObject * func = f->f_executable; + if (PyCode_Check(func)) { + dump_pyframe(fd, (_PyInterpreterFrame *)f); + return; + } + if (func == Py_None) { + return; + } + if (PyType_Check(func)) { + PUTS(fd, " Class "); + PUTS(fd, ((PyTypeObject *)func)->tp_name); + } + else if (PyCFunction_Check(func)) { + PUTS(fd, " Builtin function "); + PUTS(fd, ((PyCFunctionObject *)func)->m_ml->ml_name); + } + else if (Py_TYPE(func) == &PyMethodDescr_Type) { + PUTS(fd, " Method descriptor "); + PUTS(fd, ((PyMethodDescrObject *)func)->d_method->ml_name); + } + else { + PUTS(fd, " Other"); + } + PUTS(fd, "\n"); +} + static void dump_traceback(int fd, PyThreadState *tstate, int write_header) { From 8e555ca460d5c21f46c0801bb09b072a3561e590 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 23 Mar 2023 07:16:42 +0000 Subject: [PATCH 6/7] Fixup faulthandler test. --- Lib/test/test_faulthandler.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 048ada77e59867..0d57510fad230b 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -118,7 +118,9 @@ def check_error(self, code, lineno, fatal_error, *, # Enable MULTILINE flag regex = f'(?m){regex}' output, exitcode = self.get_output(code, filename=filename, fd=fd) - output = '\n'.join(output) + output = '\n'.join([line for line in output if + "Builtin function" not in line and + "Method descriptor" not in line]) self.assertRegex(output, regex) self.assertNotEqual(exitcode, 0) @@ -484,6 +486,7 @@ def funcA(): lineno = 14 expected = [ 'Stack (most recent call first):', + ' Builtin function dump_traceback', ' File "", line %s in funcB' % lineno, ' File "", line 17 in funcA', ' File "", line 19 in ' @@ -529,6 +532,7 @@ def {func_name}(): ) expected = [ 'Stack (most recent call first):', + ' Builtin function dump_traceback', ' File "", line 4 in %s' % truncated, ' File "", line 6 in ' ] @@ -575,7 +579,9 @@ def run(self): """ code = code.format(filename=repr(filename)) output, exitcode = self.get_output(code, filename) - output = '\n'.join(output) + output = '\n'.join([line for line in output if + "Builtin function" not in line and + "Method descriptor" not in line]) if filename: lineno = 8 else: @@ -651,8 +657,9 @@ def func(timeout, repeat, cancel, file, loops): fd=fd, ) trace, exitcode = self.get_output(code, filename) - trace = '\n'.join(trace) - + trace = '\n'.join([line for line in trace if + "Builtin function" not in line and + "Method descriptor" not in line]) if not cancel: count = loops if repeat: @@ -754,7 +761,7 @@ def handler(signum, frame): fd=fd, ) trace, exitcode = self.get_output(code, filename) - trace = '\n'.join(trace) + trace = '\n'.join([line for line in trace if "Builtin function" not in line ]) if not unregister: if all_threads: regex = r'Current thread 0x[0-9a-f]+ \(most recent call first\):\n' From 44978c158699c6be944451d43321c12f2b574a05 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Thu, 23 Mar 2023 07:24:05 +0000 Subject: [PATCH 7/7] Restore _PyFrame_IsEntryFrame API. --- Include/cpython/frameobject.h | 9 +++++++++ Objects/frameobject.c | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/Include/cpython/frameobject.h b/Include/cpython/frameobject.h index ddbb73887b325d..4e19535c656f2c 100644 --- a/Include/cpython/frameobject.h +++ b/Include/cpython/frameobject.h @@ -15,6 +15,15 @@ PyAPI_FUNC(PyFrameObject *) PyFrame_New(PyThreadState *, PyCodeObject *, PyAPI_FUNC(void) PyFrame_LocalsToFast(PyFrameObject *, int); +/* -- Caveat emptor -- + * The concept of entry frames is an implementation detail of the CPython + * interpreter. This API is considered unstable and is provided for the + * convenience of debuggers, profilers and state-inspecting tools. Notice that + * this API can be changed in future minor versions if the underlying frame + * mechanism change or the concept of an 'entry frame' or its semantics becomes + * obsolete or outdated. */ + +PyAPI_FUNC(int) _PyFrame_IsEntryFrame(PyFrameObject *frame); PyAPI_FUNC(int) PyFrame_FastToLocalsWithError(PyFrameObject *f); PyAPI_FUNC(void) PyFrame_FastToLocals(PyFrameObject *); diff --git a/Objects/frameobject.c b/Objects/frameobject.c index e752cafc91f5e9..5558ade87dde4d 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -1371,6 +1371,15 @@ PyFrame_LocalsToFast(PyFrameObject *f, int clear) } } +int +_PyFrame_IsEntryFrame(PyFrameObject *frame) +{ + assert(frame != NULL); + _PyInterpreterFrame *f = frame->f_frame; + assert(!_PyFrame_IsIncomplete(&f->base)); + return f->base.previous && f->base.previous->f_executable == Py_None; +} + PyCodeObject * PyFrame_GetCode(PyFrameObject *frame) {