Skip to content

Commit 90ed5b6

Browse files
committed
Various small clarifications as suggested by Pablo.
1 parent 666b618 commit 90ed5b6

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

Include/internal/pycore_frame.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static inline int _PyFrameHasCompleted(InterpreterFrame *f) {
4949

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

52-
void _PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *locals);
52+
void _PyFrame_TakeInterpreterFrame(PyFrameObject *f, InterpreterFrame *locals);
5353

5454
static inline void
5555
_PyFrame_InitializeSpecials(
@@ -107,13 +107,13 @@ _PyFrame_GetFrameObject(InterpreterFrame *frame)
107107
}
108108

109109
/* Clears all references in the frame.
110-
* If take is non-zero, then the frame
111-
* may be transfered to the frame object
110+
* If take is non-zero, then the InterpreterFrame frame
111+
* may be transfered to the frame object it references
112112
* instead of being cleared. Either way
113113
* the caller no longer owns the references
114114
* in the frame.
115115
* take should be set to 1 for heap allocated
116-
* frames.
116+
* frames like the ones in generators and coroutines.
117117
*/
118118
int
119119
_PyFrame_Clear(InterpreterFrame * frame, int take);
@@ -124,6 +124,11 @@ _PyFrame_FastToLocalsWithError(InterpreterFrame *frame);
124124
void
125125
_PyFrame_LocalsToFast(InterpreterFrame *frame, int clear);
126126

127+
InterpreterFrame *_PyThreadState_PushFrame(
128+
PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);
129+
130+
void _PyThreadState_PopFrame(PyThreadState *tstate, InterpreterFrame *frame);
131+
127132
#ifdef __cplusplus
128133
}
129134
#endif

Include/internal/pycore_pystate.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,6 @@ PyAPI_FUNC(int) _PyState_AddModule(
147147

148148
PyAPI_FUNC(int) _PyOS_InterruptOccurred(PyThreadState *tstate);
149149

150-
struct _interpreter_frame *_PyThreadState_PushFrame(
151-
PyThreadState *tstate, PyFrameConstructor *con, PyObject *locals);
152-
153-
void _PyThreadState_PopFrame(PyThreadState *tstate, struct _interpreter_frame *frame);
154-
155150
#ifdef __cplusplus
156151
}
157152
#endif

Objects/frameobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ frame_alloc(InterpreterFrame *frame, int owns)
857857
}
858858

859859
void
860-
_PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *frame)
860+
_PyFrame_TakeInterpreterFrame(PyFrameObject *f, InterpreterFrame *frame)
861861
{
862862
assert(f->f_own_locals_memory == 0);
863863
assert(frame->frame_obj == NULL);
@@ -866,17 +866,20 @@ _PyFrame_TakeLocals(PyFrameObject *f, InterpreterFrame *frame)
866866
f->f_frame = frame;
867867
assert(f->f_back == NULL);
868868
if (frame->previous != NULL) {
869+
/* Link PyFrameObjects.f_back and remove link through InterpreterFrame.previous */
869870
PyFrameObject *back = _PyFrame_GetFrameObject(frame->previous);
870871
if (back == NULL) {
871-
/* Memory error here. Nothing we can do about it */
872+
/* Memory error here. */
873+
assert(_PyErr_GetTopmostException(_PyThreadState_GET())->exc_type == PyExc_MemoryError);
874+
/* Nothing we can do about it */
872875
PyErr_Clear();
873876
_PyErr_WriteUnraisableMsg("Out of memory lazily allocating frame->f_back", NULL);
874877
}
875878
else {
876879
f->f_back = (PyFrameObject *)Py_NewRef(back);
877880
}
881+
frame->previous = NULL;
878882
}
879-
frame->previous = NULL;
880883
if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) {
881884
_PyObject_GC_TRACK((PyObject *)f);
882885
}

Objects/typeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8887,6 +8887,7 @@ super_init_without_args(PyFrameObject *f, PyCodeObject *co,
88878887
return -1;
88888888
}
88898889

8890+
assert(f->f_frame->nlocalsplus > 0);
88908891
PyObject *firstarg = _PyFrame_GetLocalsArray(f->f_frame)[0];
88918892
// The first argument might be a cell.
88928893
if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) {

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5162,7 +5162,7 @@ _PyFrame_Clear(InterpreterFrame * frame, int take)
51625162
return -1;
51635163
}
51645164
}
5165-
_PyFrame_TakeLocals(f, frame);
5165+
_PyFrame_TakeInterpreterFrame(f, frame);
51665166
Py_DECREF(f);
51675167
return 0;
51685168
}

Python/pystate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ _PyThreadState_PushFrame(PyThreadState *tstate, PyFrameConstructor *con, PyObjec
20422042
{
20432043
PyCodeObject *code = (PyCodeObject *)con->fc_code;
20442044
int nlocalsplus = code->co_nlocalsplus;
2045-
int size = nlocalsplus + code->co_stacksize +
2045+
size_t size = nlocalsplus + code->co_stacksize +
20462046
FRAME_SPECIALS_SIZE;
20472047
PyObject **localsarray = tstate->datastack_top;
20482048
PyObject **top = localsarray + size;

0 commit comments

Comments
 (0)