Skip to content

Commit 5668153

Browse files
committed
Clean things up a bit
1 parent d06324a commit 5668153

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

Include/internal/pycore_frame.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,10 @@ _PyThreadState_BumpFramePointerSlow(PyThreadState *tstate, size_t size);
173173
static inline InterpreterFrame *
174174
_PyThreadState_BumpFramePointer(PyThreadState *tstate, size_t size)
175175
{
176-
assert(!!tstate->datastack_top == !!tstate->datastack_limit);
177-
assert(!!tstate->datastack_top == !!tstate->datastack_chunk);
178176
PyObject **base = tstate->datastack_top;
179177
if (base) {
180178
PyObject **top = base + size;
179+
assert(tstate->datastack_limit);
181180
if (top < tstate->datastack_limit) {
182181
tstate->datastack_top = top;
183182
return (InterpreterFrame *)base;

Python/pystate.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,9 +2182,6 @@ _Py_GetConfig(void)
21822182
static PyObject **
21832183
push_chunk(PyThreadState *tstate, int size)
21842184
{
2185-
assert(tstate->datastack_chunk == NULL ||
2186-
tstate->datastack_top + size >= tstate->datastack_limit);
2187-
21882185
int allocate_size = DATA_STACK_CHUNK_SIZE;
21892186
while (allocate_size < (int)sizeof(PyObject*)*(size + MINIMUM_OVERHEAD)) {
21902187
allocate_size *= 2;
@@ -2199,6 +2196,9 @@ push_chunk(PyThreadState *tstate, int size)
21992196
}
22002197
tstate->datastack_chunk = new;
22012198
tstate->datastack_limit = (PyObject **)(((char *)new) + allocate_size);
2199+
// When new is the "root" chunk (new->previous == NULL), we keep
2200+
// _PyThreadState_PopFrame from freeing it later by "skipping" over the
2201+
// first element.
22022202
PyObject **res = &new->data[new->previous == NULL];
22032203
tstate->datastack_top = res + size;
22042204
return res;
@@ -2225,7 +2225,8 @@ _PyThreadState_PushFrame(PyThreadState *tstate, PyFunctionObject *func, PyObject
22252225
{
22262226
PyCodeObject *code = (PyCodeObject *)func->func_code;
22272227
int nlocalsplus = code->co_nlocalsplus;
2228-
size_t size = nlocalsplus + code->co_stacksize + FRAME_SPECIALS_SIZE;
2228+
size_t size = nlocalsplus + code->co_stacksize +
2229+
FRAME_SPECIALS_SIZE;
22292230
InterpreterFrame *frame = _PyThreadState_BumpFramePointer(tstate, size);
22302231
if (frame == NULL) {
22312232
return NULL;
@@ -2240,25 +2241,20 @@ _PyThreadState_PushFrame(PyThreadState *tstate, PyFunctionObject *func, PyObject
22402241
void
22412242
_PyThreadState_PopFrame(PyThreadState *tstate, InterpreterFrame * frame)
22422243
{
2243-
assert(tstate->datastack_top);
2244-
assert(tstate->datastack_limit);
22452244
assert(tstate->datastack_chunk);
22462245
PyObject **base = (PyObject **)frame;
22472246
if (base == &tstate->datastack_chunk->data[0]) {
22482247
_PyStackChunk *chunk = tstate->datastack_chunk;
22492248
_PyStackChunk *previous = chunk->previous;
2250-
if (previous) {
2251-
tstate->datastack_top = &previous->data[previous->top];
2252-
tstate->datastack_limit = (PyObject **)(((char *)previous) + previous->size);
2253-
}
2254-
else {
2255-
tstate->datastack_top = NULL;
2256-
tstate->datastack_limit = NULL;
2257-
}
2249+
// push_chunk ensures that the root chunk is never popped:
2250+
assert(previous);
2251+
tstate->datastack_top = &previous->data[previous->top];
22582252
tstate->datastack_chunk = previous;
22592253
_PyObject_VirtualFree(chunk, chunk->size);
2254+
tstate->datastack_limit = (PyObject **)(((char *)previous) + previous->size);
22602255
}
22612256
else {
2257+
assert(tstate->datastack_top);
22622258
assert(tstate->datastack_top >= base);
22632259
tstate->datastack_top = base;
22642260
}

0 commit comments

Comments
 (0)