Skip to content

Commit f5b6356

Browse files
authored
GH-128563: Add new frame owner type for interpreter entry frames (GH-129078)
Add new frame owner type for interpreter entry frames
1 parent d3b1bb2 commit f5b6356

13 files changed

+33
-48
lines changed

Include/internal/pycore_frame.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ enum _frameowner {
5656
FRAME_OWNED_BY_THREAD = 0,
5757
FRAME_OWNED_BY_GENERATOR = 1,
5858
FRAME_OWNED_BY_FRAME_OBJECT = 2,
59-
FRAME_OWNED_BY_CSTACK = 3,
59+
FRAME_OWNED_BY_INTERPRETER = 3,
60+
FRAME_OWNED_BY_CSTACK = 4,
6061
};
6162

6263
typedef struct _PyInterpreterFrame {
@@ -264,7 +265,7 @@ _PyFrame_SetStackPointer(_PyInterpreterFrame *frame, _PyStackRef *stack_pointer)
264265
static inline bool
265266
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
266267
{
267-
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
268+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
268269
return true;
269270
}
270271
return frame->owner != FRAME_OWNED_BY_GENERATOR &&

Modules/_testexternalinspection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ parse_frame_object(
508508
return -1;
509509
}
510510

511-
if (owner == FRAME_OWNED_BY_CSTACK) {
511+
if (owner >= FRAME_OWNED_BY_INTERPRETER) {
512512
return 0;
513513
}
514514

Objects/frameobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2133,7 +2133,7 @@ _PyFrame_IsEntryFrame(PyFrameObject *frame)
21332133
assert(frame != NULL);
21342134
_PyInterpreterFrame *f = frame->f_frame;
21352135
assert(!_PyFrame_IsIncomplete(f));
2136-
return f->previous && f->previous->owner == FRAME_OWNED_BY_CSTACK;
2136+
return f->previous && f->previous->owner == FRAME_OWNED_BY_INTERPRETER;
21372137
}
21382138

21392139
PyCodeObject *

Python/bytecodes.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ dummy_func(
10901090
}
10911091

10921092
tier1 inst(INTERPRETER_EXIT, (retval --)) {
1093-
assert(frame == &entry_frame);
1093+
assert(frame->owner == FRAME_OWNED_BY_INTERPRETER);
10941094
assert(_PyFrame_IsIncomplete(frame));
10951095
/* Restore previous frame and return. */
10961096
tstate->current_frame = frame->previous;
@@ -1105,9 +1105,7 @@ dummy_func(
11051105
// retval is popped from the stack, but res
11061106
// is pushed to a different frame, the callers' frame.
11071107
inst(RETURN_VALUE, (retval -- res)) {
1108-
#if TIER_ONE
1109-
assert(frame != &entry_frame);
1110-
#endif
1108+
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
11111109
_PyStackRef temp = retval;
11121110
DEAD(retval);
11131111
SAVE_STACK();
@@ -1205,7 +1203,7 @@ dummy_func(
12051203
PyObject *receiver_o = PyStackRef_AsPyObjectBorrow(receiver);
12061204

12071205
PyObject *retval_o;
1208-
assert(frame != &entry_frame);
1206+
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
12091207
if ((tstate->interp->eval_frame == NULL) &&
12101208
(Py_TYPE(receiver_o) == &PyGen_Type || Py_TYPE(receiver_o) == &PyCoro_Type) &&
12111209
((PyGenObject *)receiver_o)->gi_frame_state < FRAME_EXECUTING)
@@ -1278,9 +1276,7 @@ dummy_func(
12781276
// NOTE: It's important that YIELD_VALUE never raises an exception!
12791277
// The compiler treats any exception raised here as a failed close()
12801278
// or throw() call.
1281-
#if TIER_ONE
1282-
assert(frame != &entry_frame);
1283-
#endif
1279+
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
12841280
frame->instr_ptr++;
12851281
PyGenObject *gen = _PyGen_GetGeneratorFromFrame(frame);
12861282
assert(FRAME_SUSPENDED_YIELD_FROM == FRAME_SUSPENDED + 1);

Python/ceval.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ lltrace_instruction(_PyInterpreterFrame *frame,
178178
int opcode,
179179
int oparg)
180180
{
181-
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
181+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
182182
return;
183183
}
184184
dump_stack(frame, stack_pointer);
@@ -229,12 +229,12 @@ lltrace_resume_frame(_PyInterpreterFrame *frame)
229229
}
230230

231231
static int
232-
maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, _PyInterpreterFrame *skip_frame, PyObject *globals)
232+
maybe_lltrace_resume_frame(_PyInterpreterFrame *frame, PyObject *globals)
233233
{
234234
if (globals == NULL) {
235235
return 0;
236236
}
237-
if (frame == skip_frame) {
237+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
238238
return 0;
239239
}
240240
int r = PyDict_Contains(globals, &_Py_ID(__lltrace__));
@@ -818,7 +818,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
818818
entry_frame.f_executable = PyStackRef_None;
819819
entry_frame.instr_ptr = (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS + 1;
820820
entry_frame.stackpointer = entry_frame.localsplus;
821-
entry_frame.owner = FRAME_OWNED_BY_CSTACK;
821+
entry_frame.owner = FRAME_OWNED_BY_INTERPRETER;
822822
entry_frame.visited = 0;
823823
entry_frame.return_offset = 0;
824824
/* Push frame */
@@ -880,7 +880,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
880880
stack_pointer = _PyFrame_GetStackPointer(frame);
881881

882882
#ifdef LLTRACE
883-
lltrace = maybe_lltrace_resume_frame(frame, &entry_frame, GLOBALS());
883+
lltrace = maybe_lltrace_resume_frame(frame, GLOBALS());
884884
if (lltrace < 0) {
885885
goto exit_unwind;
886886
}

Python/ceval_macros.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#if LLTRACE
9090
#define LLTRACE_RESUME_FRAME() \
9191
do { \
92-
lltrace = maybe_lltrace_resume_frame(frame, &entry_frame, GLOBALS()); \
92+
lltrace = maybe_lltrace_resume_frame(frame, GLOBALS()); \
9393
if (lltrace < 0) { \
9494
goto exit_unwind; \
9595
} \
@@ -238,7 +238,7 @@ GETITEM(PyObject *v, Py_ssize_t i) {
238238
#endif
239239

240240
#define WITHIN_STACK_BOUNDS() \
241-
(frame == &entry_frame || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
241+
(frame->owner == FRAME_OWNED_BY_INTERPRETER || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
242242

243243
/* Data access macros */
244244
#define FRAME_CO_CONSTS (_PyFrame_GetCode(frame)->co_consts)

Python/executor_cases.c.h

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/frame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame)
4848
static void
4949
take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
5050
{
51-
assert(frame->owner != FRAME_OWNED_BY_CSTACK);
51+
assert(frame->owner < FRAME_OWNED_BY_INTERPRETER);
5252
assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT);
5353
Py_ssize_t size = ((char*)frame->stackpointer) - (char *)frame;
5454
memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size);
@@ -69,7 +69,7 @@ take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame)
6969
_PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous);
7070
frame->previous = NULL;
7171
if (prev) {
72-
assert(prev->owner != FRAME_OWNED_BY_CSTACK);
72+
assert(prev->owner < FRAME_OWNED_BY_INTERPRETER);
7373
/* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */
7474
PyFrameObject *back = _PyFrame_GetFrameObject(prev);
7575
if (back == NULL) {

Python/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ mark_stacks(PyInterpreterState *interp, PyGC_Head *visited, int visited_space, b
14761476
while (ts) {
14771477
_PyInterpreterFrame *frame = ts->current_frame;
14781478
while (frame) {
1479-
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
1479+
if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
14801480
frame = frame->previous;
14811481
continue;
14821482
}

Python/generated_cases.c.h

Lines changed: 6 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/instrumentation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ instrument_all_executing_code_objects(PyInterpreterState *interp) {
19461946
while (ts) {
19471947
_PyInterpreterFrame *frame = ts->current_frame;
19481948
while (frame) {
1949-
if (frame->owner != FRAME_OWNED_BY_CSTACK) {
1949+
if (frame->owner < FRAME_OWNED_BY_INTERPRETER) {
19501950
if (instrument_lock_held(_PyFrame_GetCode(frame), interp)) {
19511951
return -1;
19521952
}

Python/traceback.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ _Py_DumpASCII(int fd, PyObject *text)
890890
static void
891891
dump_frame(int fd, _PyInterpreterFrame *frame)
892892
{
893-
assert(frame->owner != FRAME_OWNED_BY_CSTACK);
893+
assert(frame->owner < FRAME_OWNED_BY_INTERPRETER);
894894

895895
PyCodeObject *code =_PyFrame_GetCode(frame);
896896
PUTS(fd, " File ");
@@ -965,15 +965,15 @@ dump_traceback(int fd, PyThreadState *tstate, int write_header)
965965

966966
unsigned int depth = 0;
967967
while (1) {
968-
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
968+
if (frame->owner == FRAME_OWNED_BY_INTERPRETER) {
969969
/* Trampoline frame */
970970
frame = frame->previous;
971971
if (frame == NULL) {
972972
break;
973973
}
974974

975975
/* Can't have more than one shim frame in a row */
976-
assert(frame->owner != FRAME_OWNED_BY_CSTACK);
976+
assert(frame->owner != FRAME_OWNED_BY_INTERPRETER);
977977
}
978978

979979
if (MAX_FRAME_DEPTH <= depth) {

Tools/gdb/libpython.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def interp_frame_has_tlbc_index():
9999
Py_TPFLAGS_TYPE_SUBCLASS = (1 << 31)
100100

101101
#From pycore_frame.h
102-
FRAME_OWNED_BY_CSTACK = 3
102+
FRAME_OWNED_BY_INTERPRETER = 3
103103

104104
MAX_OUTPUT_LEN=1024
105105

@@ -1113,7 +1113,7 @@ def _f_lasti(self):
11131113
return int(instr_ptr - first_instr)
11141114

11151115
def is_shim(self):
1116-
return self._f_special("owner", int) == FRAME_OWNED_BY_CSTACK
1116+
return self._f_special("owner", int) == FRAME_OWNED_BY_INTERPRETER
11171117

11181118
def previous(self):
11191119
return self._f_special("previous", PyFramePtr)

0 commit comments

Comments
 (0)