Skip to content

Commit 332e6b9

Browse files
authored
bpo-45256: Don't track the exact depth of each InterpreterFrame (GH-30372)
1 parent cae5554 commit 332e6b9

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

Include/internal/pycore_frame.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
extern "C" {
55
#endif
66

7+
#include <stdbool.h>
8+
79

810
/* runtime lifecycle */
911

@@ -44,7 +46,7 @@ typedef struct _interpreter_frame {
4446
int f_lasti; /* Last instruction if called */
4547
int stacktop; /* Offset of TOS from localsplus */
4648
PyFrameState f_state; /* What state the frame is in */
47-
int depth; /* Depth of the frame in a ceval loop */
49+
bool is_entry; // Whether this is the "root" frame for the current CFrame.
4850
PyObject *localsplus[1];
4951
} InterpreterFrame;
5052

@@ -101,7 +103,7 @@ _PyFrame_InitializeSpecials(
101103
frame->generator = NULL;
102104
frame->f_lasti = -1;
103105
frame->f_state = FRAME_CREATED;
104-
frame->depth = 0;
106+
frame->is_entry = false;
105107
}
106108

107109
/* Gets the pointer to the locals array

Lib/test/test_sys.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ class C(object): pass
13231323
def func():
13241324
return sys._getframe()
13251325
x = func()
1326-
check(x, size('3Pi3c8P2iciP'))
1326+
check(x, size('3Pi3c8P2ic?P'))
13271327
# function
13281328
def func(): pass
13291329
check(func, size('14Pi'))
@@ -1340,7 +1340,7 @@ def bar(cls):
13401340
check(bar, size('PP'))
13411341
# generator
13421342
def get_gen(): yield 1
1343-
check(get_gen(), size('P2P4P4c8P2iciP'))
1343+
check(get_gen(), size('P2P4P4c8P2ic?P'))
13441344
# iterator
13451345
check(iter('abc'), size('lP'))
13461346
# callable-iterator

Python/ceval.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
16831683
cframe.previous = prev_cframe;
16841684
tstate->cframe = &cframe;
16851685

1686-
assert(frame->depth == 0);
1686+
frame->is_entry = true;
16871687
/* Push frame */
16881688
frame->previous = prev_cframe->current_frame;
16891689
cframe.current_frame = frame;
@@ -2310,7 +2310,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
23102310
_PyFrame_SetStackPointer(frame, stack_pointer);
23112311
new_frame->previous = frame;
23122312
frame = cframe.current_frame = new_frame;
2313-
new_frame->depth = frame->depth + 1;
23142313
goto start_frame;
23152314
}
23162315

@@ -2475,7 +2474,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
24752474
TRACE_FUNCTION_EXIT();
24762475
DTRACE_FUNCTION_EXIT();
24772476
_Py_LeaveRecursiveCall(tstate);
2478-
if (frame->depth) {
2477+
if (!frame->is_entry) {
24792478
frame = cframe.current_frame = pop_frame(tstate, frame);
24802479
_PyFrame_StackPush(frame, retval);
24812480
goto resume_frame;
@@ -2625,7 +2624,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
26252624
}
26262625

26272626
TARGET(SEND) {
2628-
assert(frame->depth == 0);
2627+
assert(frame->is_entry);
26292628
assert(STACK_LEVEL() >= 2);
26302629
PyObject *v = POP();
26312630
PyObject *receiver = TOP();
@@ -2684,7 +2683,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
26842683
}
26852684

26862685
TARGET(YIELD_VALUE) {
2687-
assert(frame->depth == 0);
2686+
assert(frame->is_entry);
26882687
PyObject *retval = POP();
26892688

26902689
if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) {
@@ -4612,7 +4611,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
46124611
_PyFrame_SetStackPointer(frame, stack_pointer);
46134612
new_frame->previous = frame;
46144613
cframe.current_frame = frame = new_frame;
4615-
new_frame->depth = frame->depth + 1;
46164614
goto start_frame;
46174615
}
46184616
}
@@ -4706,7 +4704,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
47064704
_PyFrame_SetStackPointer(frame, stack_pointer);
47074705
new_frame->previous = frame;
47084706
frame = cframe.current_frame = new_frame;
4709-
new_frame->depth = frame->depth + 1;
47104707
goto start_frame;
47114708
}
47124709

@@ -5382,7 +5379,7 @@ MISS_WITH_OPARG_COUNTER(STORE_SUBSCR)
53825379
exit_unwind:
53835380
assert(_PyErr_Occurred(tstate));
53845381
_Py_LeaveRecursiveCall(tstate);
5385-
if (frame->depth == 0) {
5382+
if (frame->is_entry) {
53865383
/* Restore previous cframe and exit */
53875384
tstate->cframe = cframe.previous;
53885385
tstate->cframe->use_tracing = cframe.use_tracing;

Tools/gdb/libpython.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ def _f_nlocalsplus(self):
10441044
def _f_lasti(self):
10451045
return self._f_special("f_lasti", int_from_int)
10461046

1047-
def depth(self):
1048-
return self._f_special("depth", int_from_int)
1047+
def is_entry(self):
1048+
return self._f_special("is_entry", bool)
10491049

10501050
def previous(self):
10511051
return self._f_special("previous", PyFramePtr)
@@ -1860,7 +1860,7 @@ def print_summary(self):
18601860
line = interp_frame.current_line()
18611861
if line is not None:
18621862
sys.stdout.write(' %s\n' % line.strip())
1863-
if interp_frame.depth() == 0:
1863+
if interp_frame.is_entry():
18641864
break
18651865
else:
18661866
sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index())
@@ -1883,7 +1883,7 @@ def print_traceback(self):
18831883
line = interp_frame.current_line()
18841884
if line is not None:
18851885
sys.stdout.write(' %s\n' % line.strip())
1886-
if interp_frame.depth() == 0:
1886+
if interp_frame.is_entry():
18871887
break
18881888
else:
18891889
sys.stdout.write(' (unable to read python frame information)\n')
@@ -2147,7 +2147,7 @@ def invoke(self, args, from_tty):
21472147
% (pyop_name.proxyval(set()),
21482148
pyop_value.get_truncated_repr(MAX_OUTPUT_LEN)))
21492149

2150-
if pyop_frame.depth() == 0:
2150+
if pyop_frame.is_entry():
21512151
break
21522152

21532153
pyop_frame = pyop_frame.previous()

0 commit comments

Comments
 (0)