mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.11] gh-96497: Mangle name before symtable lookup in 'symtable_extend_namedexpr_scope' (GH-96561) (GH-115604)
(cherry picked from commit 664965a1c1)
Co-authored-by: wookie184 <wookie1840@gmail.com>
This commit is contained in:
parent
5b6e358913
commit
51b974b0ab
3 changed files with 34 additions and 4 deletions
|
|
@ -222,6 +222,16 @@ def test_named_expression_invalid_set_comprehension_iterable_expression(self):
|
||||||
with self.assertRaisesRegex(SyntaxError, msg):
|
with self.assertRaisesRegex(SyntaxError, msg):
|
||||||
exec(f"lambda: {code}", {}) # Function scope
|
exec(f"lambda: {code}", {}) # Function scope
|
||||||
|
|
||||||
|
def test_named_expression_invalid_mangled_class_variables(self):
|
||||||
|
code = """class Foo:
|
||||||
|
def bar(self):
|
||||||
|
[[(__x:=2) for _ in range(2)] for __x in range(2)]
|
||||||
|
"""
|
||||||
|
|
||||||
|
with self.assertRaisesRegex(SyntaxError,
|
||||||
|
"assignment expression cannot rebind comprehension iteration variable '__x'"):
|
||||||
|
exec(code, {}, {})
|
||||||
|
|
||||||
|
|
||||||
class NamedExpressionAssignmentTest(unittest.TestCase):
|
class NamedExpressionAssignmentTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
@ -598,6 +608,18 @@ def test_named_expression_scope_in_genexp(self):
|
||||||
for idx, elem in enumerate(genexp):
|
for idx, elem in enumerate(genexp):
|
||||||
self.assertEqual(elem, b[idx] + a)
|
self.assertEqual(elem, b[idx] + a)
|
||||||
|
|
||||||
|
def test_named_expression_scope_mangled_names(self):
|
||||||
|
class Foo:
|
||||||
|
def f(self_):
|
||||||
|
global __x1
|
||||||
|
__x1 = 0
|
||||||
|
[_Foo__x1 := 1 for a in [2]]
|
||||||
|
self.assertEqual(__x1, 1)
|
||||||
|
[__x1 := 2 for a in [3]]
|
||||||
|
self.assertEqual(__x1, 2)
|
||||||
|
|
||||||
|
Foo().f()
|
||||||
|
self.assertEqual(_Foo__x1, 2)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix incorrect resolution of mangled class variables used in assignment
|
||||||
|
expressions in comprehensions.
|
||||||
|
|
@ -1042,16 +1042,22 @@ symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
|
||||||
}
|
}
|
||||||
|
|
||||||
static long
|
static long
|
||||||
symtable_lookup(struct symtable *st, PyObject *name)
|
symtable_lookup_entry(struct symtable *st, PySTEntryObject *ste, PyObject *name)
|
||||||
{
|
{
|
||||||
PyObject *mangled = _Py_Mangle(st->st_private, name);
|
PyObject *mangled = _Py_Mangle(st->st_private, name);
|
||||||
if (!mangled)
|
if (!mangled)
|
||||||
return 0;
|
return 0;
|
||||||
long ret = _PyST_GetSymbol(st->st_cur, mangled);
|
long ret = _PyST_GetSymbol(ste, mangled);
|
||||||
Py_DECREF(mangled);
|
Py_DECREF(mangled);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static long
|
||||||
|
symtable_lookup(struct symtable *st, PyObject *name)
|
||||||
|
{
|
||||||
|
return symtable_lookup_entry(st, st->st_cur, name);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste,
|
symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste,
|
||||||
int lineno, int col_offset, int end_lineno, int end_col_offset)
|
int lineno, int col_offset, int end_lineno, int end_col_offset)
|
||||||
|
|
@ -1525,7 +1531,7 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
|
||||||
* binding conflict with iteration variables, otherwise skip it
|
* binding conflict with iteration variables, otherwise skip it
|
||||||
*/
|
*/
|
||||||
if (ste->ste_comprehension) {
|
if (ste->ste_comprehension) {
|
||||||
long target_in_scope = _PyST_GetSymbol(ste, target_name);
|
long target_in_scope = symtable_lookup_entry(st, ste, target_name);
|
||||||
if (target_in_scope & DEF_COMP_ITER) {
|
if (target_in_scope & DEF_COMP_ITER) {
|
||||||
PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
|
PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
|
||||||
PyErr_RangedSyntaxLocationObject(st->st_filename,
|
PyErr_RangedSyntaxLocationObject(st->st_filename,
|
||||||
|
|
@ -1540,7 +1546,7 @@ symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
|
||||||
|
|
||||||
/* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
|
/* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
|
||||||
if (ste->ste_type == FunctionBlock) {
|
if (ste->ste_type == FunctionBlock) {
|
||||||
long target_in_scope = _PyST_GetSymbol(ste, target_name);
|
long target_in_scope = symtable_lookup_entry(st, ste, target_name);
|
||||||
if (target_in_scope & DEF_GLOBAL) {
|
if (target_in_scope & DEF_GLOBAL) {
|
||||||
if (!symtable_add_def(st, target_name, DEF_GLOBAL, LOCATION(e)))
|
if (!symtable_add_def(st, target_name, DEF_GLOBAL, LOCATION(e)))
|
||||||
VISIT_QUIT(st, 0);
|
VISIT_QUIT(st, 0);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue