GH-120024: Use pointer for stack pointer by markshannon · Pull Request #121923 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions Include/internal/pycore_frame.h
5 changes: 3 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,8 @@ class C(object): pass
def func():
return sys._getframe()
x = func()
check(x, size('3Pi2c2P7P2ic??2P'))
INTERPRETER_FRAME = '9PhcP'
check(x, size('3PiccPP' + INTERPRETER_FRAME + 'P'))
# function
def func(): pass
check(func, size('16Pi'))
Expand All @@ -1620,7 +1621,7 @@ def bar(cls):
check(bar, size('PP'))
# generator
def get_gen(): yield 1
check(get_gen(), size('PP4P4c7P2ic??2P'))
check(get_gen(), size('6P4c' + INTERPRETER_FRAME + 'P'))
# iterator
check(iter('abc'), size('lP'))
# callable-iterator
Expand Down
24 changes: 13 additions & 11 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,10 @@ frame_dealloc(PyFrameObject *f)
Py_CLEAR(frame->f_funcobj);
Py_CLEAR(frame->f_locals);
_PyStackRef *locals = _PyFrame_GetLocalsArray(frame);
for (int i = 0; i < frame->stacktop; i++) {
PyStackRef_CLEAR(locals[i]);
_PyStackRef *sp = frame->stackpointer;
while (sp > locals) {
sp--;
PyStackRef_CLEAR(*sp);
}
}
Py_CLEAR(f->f_back);
Expand Down Expand Up @@ -1656,11 +1658,13 @@ frame_tp_clear(PyFrameObject *f)

/* locals and stack */
_PyStackRef *locals = _PyFrame_GetLocalsArray(f->f_frame);
assert(f->f_frame->stacktop >= 0);
for (int i = 0; i < f->f_frame->stacktop; i++) {
PyStackRef_CLEAR(locals[i]);
_PyStackRef *sp = f->f_frame->stackpointer;
assert(sp >= locals);
while (sp > locals) {
sp--;
PyStackRef_CLEAR(*sp);
}
f->f_frame->stacktop = 0;
f->f_frame->stackpointer = locals;
Py_CLEAR(f->f_frame->f_locals);
return 0;
}
Expand Down Expand Up @@ -1878,8 +1882,9 @@ frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
return 0;
}

PyObject *value = PyStackRef_AsPyObjectBorrow(frame->localsplus[i]);
if (frame->stacktop) {
PyObject *value = NULL;
if (frame->stackpointer == NULL || frame->stackpointer > frame->localsplus + i) {
value = PyStackRef_AsPyObjectBorrow(frame->localsplus[i]);
if (kind & CO_FAST_FREE) {
// The cell was set by COPY_FREE_VARS.
assert(value != NULL && PyCell_Check(value));
Expand All @@ -1897,9 +1902,6 @@ frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
}
}
}
else {
assert(value == NULL);
}
*pvalue = value;
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
#endif
entry_frame.f_executable = Py_None;
entry_frame.instr_ptr = (_Py_CODEUNIT *)_Py_INTERPRETER_TRAMPOLINE_INSTRUCTIONS + 1;
entry_frame.stacktop = 0;
entry_frame.stackpointer = entry_frame.localsplus;
entry_frame.owner = FRAME_OWNED_BY_CSTACK;
entry_frame.return_offset = 0;
/* Push frame */
Expand Down
22 changes: 13 additions & 9 deletions Python/frame.c