summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_named_expressions.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2024-02-17 12:49:39 (GMT)
committerGitHub <noreply@github.com>2024-02-17 12:49:39 (GMT)
commitbf0e0729803c20298beda40af016e58510594a1d (patch)
tree171efabef5474e2c118939ee4194e95b070059b5 /Lib/test/test_named_expressions.py
parentebf8eb2740a46ca685c03660114eb047d054eb30 (diff)
downloadcpython-bf0e0729803c20298beda40af016e58510594a1d.zip
cpython-bf0e0729803c20298beda40af016e58510594a1d.tar.gz
cpython-bf0e0729803c20298beda40af016e58510594a1d.tar.bz2
[3.12] gh-96497: Mangle name before symtable lookup in 'symtable_extend_namedexpr_scope' (GH-96561) (GH-115603)
(cherry picked from commit 664965a1c141e8af5eb465d29099781a6a2fc3f3) Co-authored-by: wookie184 <wookie1840@gmail.com>
Diffstat (limited to 'Lib/test/test_named_expressions.py')
-rw-r--r--Lib/test/test_named_expressions.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_named_expressions.py b/Lib/test/test_named_expressions.py
index 7b2fa84..f2017bd 100644
--- a/Lib/test/test_named_expressions.py
+++ b/Lib/test/test_named_expressions.py
@@ -298,6 +298,16 @@ class NamedExpressionInvalidTest(unittest.TestCase):
with self.assertRaisesRegex(SyntaxError, msg):
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):
@@ -674,6 +684,18 @@ spam()"""
for idx, elem in enumerate(genexp):
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__":
unittest.main()