[Python-checkins] cpython: Issue #28076: Variable annotations should be mangled for private names.

guido.van.rossum python-checkins at python.org
Sun Sep 11 12:45:32 EDT 2016


https://hg.python.org/cpython/rev/1c2a4f29e1ab
changeset: 103642:1c2a4f29e1ab
user: Guido van Rossum <guido at dropbox.com>
date: Sun Sep 11 09:45:24 2016 -0700
summary:
 Issue #28076: Variable annotations should be mangled for private names.
By Ivan Levkivskyi.
files:
 Doc/reference/simple_stmts.rst | 7 ++++---
 Lib/test/test_grammar.py | 4 ++--
 Python/compile.c | 8 +++++++-
 3 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -334,9 +334,10 @@
 For simple names as assignment targets, if in class or module scope,
 the annotations are evaluated and stored in a special class or module
 attribute :attr:`__annotations__`
-that is a dictionary mapping from variable names to evaluated annotations.
-This attribute is writable and is automatically created at the start
-of class or module body execution, if annotations are found statically.
+that is a dictionary mapping from variable names (mangled if private) to
+evaluated annotations. This attribute is writable and is automatically
+created at the start of class or module body execution, if annotations
+are found statically.
 
 For expressions as assignment targets, the annotations are evaluated if
 in class or module scope, but not stored.
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -328,12 +328,12 @@
 
 # class semantics
 class C:
- x: int
+ __foo: int
 s: str = "attr"
 z = 2
 def __init__(self, x):
 self.x: int = x
- self.assertEqual(C.__annotations__, {'x': int, 's': str})
+ self.assertEqual(C.__annotations__, {'_C__foo': int, 's': str})
 with self.assertRaises(NameError):
 class CBad:
 no_such_name_defined.attr: int = 0
diff --git a/Python/compile.c b/Python/compile.c
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -4562,6 +4562,7 @@
 compiler_annassign(struct compiler *c, stmt_ty s)
 {
 expr_ty targ = s->v.AnnAssign.target;
+ PyObject* mangled;
 
 assert(s->kind == AnnAssign_kind);
 
@@ -4576,8 +4577,13 @@
 if (s->v.AnnAssign.simple &&
 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
+ mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
+ if (!mangled) {
+ return 0;
+ }
 VISIT(c, expr, s->v.AnnAssign.annotation);
- ADDOP_O(c, STORE_ANNOTATION, targ->v.Name.id, names)
+ /* ADDOP_N decrefs its argument */
+ ADDOP_N(c, STORE_ANNOTATION, mangled, names);
 }
 break;
 case Attribute_kind:
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /