Skip to content

Commit 609da58

Browse files
committed
store the current scope on the stack right away
1 parent 0aafa4f commit 609da58

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

‎Python/symtable.c‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -899,17 +899,14 @@ symtable_warn(struct symtable *st, char *msg, int lineno)
899899
static int
900900
symtable_exit_block(struct symtable *st, void *ast)
901901
{
902-
Py_ssize_t end;
902+
Py_ssize_t size;
903903

904-
Py_CLEAR(st->st_cur);
905-
end = PyList_GET_SIZE(st->st_stack) - 1;
906-
if (end >= 0) {
904+
st->st_cur = NULL;
905+
size = PyList_GET_SIZE(st->st_stack);
906+
if (size) {
907907
st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
908-
end);
909-
if (st->st_cur == NULL)
910-
return 0;
911-
Py_INCREF(st->st_cur);
912-
if (PySequence_DelItem(st->st_stack, end) < 0)
908+
size - 2);
909+
if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
913910
return 0;
914911
}
915912
return 1;
@@ -919,23 +916,23 @@ static int
919916
symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
920917
void *ast, int lineno, int col_offset)
921918
{
922-
PySTEntryObject *prev = NULL;
919+
PySTEntryObject *prev = NULL, *ste;
923920

924-
if (st->st_cur) {
925-
prev = st->st_cur;
926-
if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
927-
return 0;
928-
}
929-
Py_DECREF(st->st_cur);
930-
}
931-
st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
932-
if (st->st_cur == NULL)
921+
ste = ste_new(st, name, block, ast, lineno, col_offset);
922+
if (ste == NULL)
933923
return 0;
924+
if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
925+
Py_DECREF(ste);
926+
return 0;
927+
}
928+
prev = st->st_cur;
929+
/* The entry is owned by the stack. Borrow it for st_cur. */
930+
Py_DECREF(ste);
931+
st->st_cur = ste;
934932
if (block == ModuleBlock)
935933
st->st_global = st->st_cur->ste_symbols;
936934
if (prev) {
937-
if (PyList_Append(prev->ste_children,
938-
(PyObject *)st->st_cur) < 0) {
935+
if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
939936
return 0;
940937
}
941938
}

0 commit comments

Comments
 (0)