Skip to content

Commit dcaf329

Browse files
committed
instead of hacking __locals__ in during bytecode generation, put it in the symtable
1 parent 231b7f1 commit dcaf329

2 files changed

Lines changed: 6 additions & 22 deletions

File tree

Python/compile.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,23 +1524,14 @@ compiler_function(struct compiler *c, stmt_ty s)
15241524
static int
15251525
compiler_class(struct compiler *c, stmt_ty s)
15261526
{
1527-
static PyObject *locals = NULL;
15281527
PyCodeObject *co;
15291528
PyObject *str;
1530-
PySTEntryObject *ste;
1531-
int err, i;
1529+
int i;
15321530
asdl_seq* decos = s->v.ClassDef.decorator_list;
15331531

15341532
if (!compiler_decorators(c, decos))
15351533
return 0;
15361534

1537-
/* initialize statics */
1538-
if (locals == NULL) {
1539-
locals = PyUnicode_InternFromString("__locals__");
1540-
if (locals == NULL)
1541-
return 0;
1542-
}
1543-
15441535
/* ultimately generate code for:
15451536
<name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
15461537
where:
@@ -1553,16 +1544,6 @@ compiler_class(struct compiler *c, stmt_ty s)
15531544
This borrows from compiler_call.
15541545
*/
15551546

1556-
/* 0. Create a fake argument named __locals__ */
1557-
ste = PySymtable_Lookup(c->c_st, s);
1558-
if (ste == NULL)
1559-
return 0;
1560-
assert(PyList_Check(ste->ste_varnames));
1561-
err = PyList_Append(ste->ste_varnames, locals);
1562-
Py_DECREF(ste);
1563-
if (err < 0)
1564-
return 0;
1565-
15661547
/* 1. compile the class body into a code object */
15671548
if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
15681549
return 0;

Python/symtable.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
186186

187187

188188
static identifier top = NULL, lambda = NULL, genexpr = NULL,
189-
listcomp = NULL, setcomp = NULL, dictcomp = NULL, __class__ = NULL;
189+
listcomp = NULL, setcomp = NULL, dictcomp = NULL,
190+
__class__ = NULL, __locals__ = NULL;
190191

191192
#define GET_IDENTIFIER(VAR) \
192193
((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
@@ -1050,7 +1051,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)
10501051
(void *)s, s->lineno))
10511052
return 0;
10521053
if (!GET_IDENTIFIER(__class__) ||
1053-
!symtable_add_def(st, __class__, DEF_LOCAL)) {
1054+
!symtable_add_def(st, __class__, DEF_LOCAL) ||
1055+
!GET_IDENTIFIER(__locals__) ||
1056+
!symtable_add_def(st, __locals__, DEF_PARAM)) {
10541057
symtable_exit_block(st, s);
10551058
return 0;
10561059
}

0 commit comments

Comments
 (0)