Fix private name mangling. The symtable also must do mangles so that · pythoncapi/cpython@8b528b2 · GitHub
Skip to content

Commit 8b528b2

Browse files
committed
Fix private name mangling. The symtable also must do mangles so that
the scope of names can be correctly determined.
1 parent 3a44aaa commit 8b528b2

3 files changed

Lines changed: 47 additions & 19 deletions

File tree

Include/symtable.h

Lines changed: 1 addition & 1 deletion

Python/compile.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2701,10 +2701,11 @@ inplace_binop(struct compiler *c, operator_ty op)
27012701
static int
27022702
compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
27032703
{
2704-
int op, scope;
2704+
int op, scope, r, arg;
27052705
enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
27062706

27072707
PyObject *dict = c->u->u_names;
2708+
PyObject *mangled;
27082709
/* XXX AugStore isn't used anywhere! */
27092710

27102711
/* First check for assignment to __debug__. Param? */
@@ -2713,9 +2714,13 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
27132714
return compiler_error(c, "can not assign to __debug__");
27142715
}
27152716

2717+
mangled = _Py_Mangle(c->u->u_private, name);
2718+
if (!mangled)
2719+
return 0;
2720+
27162721
op = 0;
27172722
optype = OP_NAME;
2718-
scope = PyST_GetScope(c->u->u_ste, name);
2723+
scope = PyST_GetScope(c->u->u_ste, mangled);
27192724
switch (scope) {
27202725
case FREE:
27212726
dict = c->u->u_freevars;
@@ -2755,6 +2760,7 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
27552760
"can not delete variable '%s' referenced "
27562761
"in nested scope",
27572762
PyString_AS_STRING(name));
2763+
Py_DECREF(mangled);
27582764
return 0;
27592765
break;
27602766
case Param:
@@ -2772,7 +2778,8 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
27722778
case Param:
27732779
assert(0); /* impossible */
27742780
}
2775-
ADDOP_O(c, op, name, varnames);
2781+
ADDOP_O(c, op, mangled, varnames);
2782+
Py_DECREF(mangled);
27762783
return 1;
27772784
case OP_GLOBAL:
27782785
switch (ctx) {
@@ -2801,7 +2808,12 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
28012808
}
28022809

28032810
assert(op);
2804-
return compiler_addop_name(c, op, dict, name);
2811+
arg = compiler_add_o(c, dict, mangled);
2812+
if (arg < 0)
2813+
return 0;
2814+
r = compiler_addop_i(c, op, arg);
2815+
Py_DECREF(mangled);
2816+
return r;
28052817
}
28062818

28072819
static int

Python/symtable.c

Lines changed: 30 additions & 14 deletions

0 commit comments

Comments
 (0)