8000 [3.12] gh-96497: Mangle name before symtable lookup in 'symtable_exte… · python/cpython@bf0e072 · GitHub
[go: up one dir, main page]

Skip to content

Commit bf0e072

Browse files
[3.12] gh-96497: Mangle name before symtable lookup in 'symtable_extend_namedexpr_scope' (GH-96561) (GH-115603)
(cherry picked from commit 664965a) Co-authored-by: wookie184 <wookie1840@gmail.com>
1 parent ebf8eb2 commit bf0e072

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Lib/test/test_named_expressions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,16 @@ def test_named_expression_invalid_set_comprehension_iterable_expression(self):
298298
with self.assertRaisesRegex(SyntaxError, msg):
299299
exec(f"lambda: {code}", {}) # Function scope
300300

301+
def test_named_expression_invalid_mangled_class_variables(self):
302+
code = """class Foo:
303+
def bar(self):
304+
[[(__x:=2) for _ in range(2)] for __x in range(2)]
305+
"""
306+
307+
with self.assertRaisesRegex(SyntaxError,
308+
"assignment expression cannot rebind comprehension iteration variable '__x'"):
309+
exec(code, {}, {})
310+
301311

302312
class NamedExpressionAssignmentTest(unittest.TestCase):
303313

@@ -674,6 +684,18 @@ def test_named_expression_scope_in_genexp(self):
674684
for idx, elem in enumerate(genexp):
675685
self.assertEqual(elem, b[idx] + a)
676686

687+
def test_named_expression_scope_mangled_names(self):
688+
class Foo:
689+
def f(self_):
690+
global __x1
691+
__x1 = 0
692+
[_Foo__x1 := 1 for a in [2]]
693+
self.assertEqual(__x1, 1)
694+
[__x1 := 2 for a in [3]]
695+
self.assertEqual(__x1, 2)
696+
697+
Foo().f()
698+
self.assertEqual(_Foo__x1, 2)
677699

678700
if __name__ == "__main__":
679701
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incorrect resolution of mangled class variables used in assignment
2+
expressions in comprehensions.

Python/symtable.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,22 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
12541254
}
12551255

12561256
static long
1257-
symtable_lookup(struct symtable *st, PyObject *name)
1257+
symtable_lookup_entry(struct symtable *st, PySTEntryObject *ste, PyObject *name)
12581258
{
12591259
PyObject *mangled = _Py_Mangle(st->st_private, name);
12601260
if (!mangled)
12611261
return 0;
1262-
long ret = _PyST_GetSymbol(st->st_cur, mangled);
1262+
long ret = _PyST_GetSymbol(ste, mangled);
12631263
Py_DECREF(mangled);
12641264
return ret;
12651265
}
12661266

1267+
static long
1268+
symtable_lookup(struct symtable *st, PyObject *name)
1269+
{
1270+
return symtable_lookup_entry(st, st->st_cur, name);
1271+
}
1272+
12671273
static int
12681274
symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste,
12691275
int lineno, int col_offset, int end_lineno, int end_col_offset)
@@ -1904,7 +1910,7 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
19041910
* binding conflict with iteration variables, otherwise skip it
19051911
*/
19061912
if (ste->ste_comprehension) {
1907-
long target_in_scope = _PyST_GetSymbol(ste, target_name);
1913+
long target_in_scope = symtable_lookup_entry(st, ste, target_name);
19081914
if ((target_in_scope & DEF_COMP_ITER) &&
19091915
(target_in_scope & DEF_LOCAL)) {
19101916
PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
@@ -1920,7 +1926,7 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
19201926

19211927
/* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
19221928
if (ste->ste_type == FunctionBlock) {
1923-
long target_in_scope = _PyST_GetSymbol(ste, target_name);
1929+
long target_in_scope = symtable_lookup_entry(st, ste, target_name);
19241930
if (target_in_scope & DEF_GLOBAL) {
19251931
if (!symtable_add_def(st, target_name, DEF_GLOBAL, LOCATION(e)))
19261932
VISIT_QUIT(st, 0);

0 commit comments

Comments
 (0)
0