]> granicus.if.org Git - python/commitdiff
Mangle __parameters in __annotations__ dict properly. Issue #20625.
authorYury Selivanov <yselivanov@sprymix.com>
Tue, 18 Feb 2014 17:49:41 +0000 (12:49 -0500)
committerYury Selivanov <yselivanov@sprymix.com>
Tue, 18 Feb 2014 17:49:41 +0000 (12:49 -0500)
Doc/whatsnew/3.4.rst
Lib/test/test_grammar.py
Lib/test/test_inspect.py
Misc/NEWS
Python/compile.c

index f88866d7b8b713316f74374aee4a7bb5386b907d..0ca4472188a88d65844664490454ceba3291b990 100644 (file)
@@ -1781,6 +1781,10 @@ Changes in the Python API
   perpetually alive (for example, database connections kept in thread-local
   storage).  (:issue:`17094`.)
 
+* Parameter names in ``__annotations__`` dict are now mangled properly,
+  similarly to ``__kwdefaults__``. (Contributed by Yury Selivanov in
+  :issue:`20625`).
+
 
 Changes in the C API
 --------------------
index 6b326bdaa112e89a5adf3037b020b512508bad19..9da63380993761392911c9fe5e9c6420fad28689 100644 (file)
@@ -314,6 +314,13 @@ class GrammarTests(unittest.TestCase):
         self.assertEqual(f.__annotations__,
                           {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
                            'k': 11, 'return': 12})
+        # Check for issue #20625 -- annotations mangling
+        class Spam:
+            def f(self, *, __kw:1):
+                pass
+        class Ham(Spam): pass
+        self.assertEquals(Spam.f.__annotations__, {'_Spam__kw': 1})
+        self.assertEquals(Ham.f.__annotations__, {'_Spam__kw': 1})
         # Check for SF Bug #1697248 - mixing decorators and a return annotation
         def null(x): return x
         @null
index 3f204191bf79b7647c60629d339c3b3653726bc9..a34a418ccab41706a9c92642bb90e73652ff6a16 100644 (file)
@@ -2390,6 +2390,22 @@ class TestSignatureObject(unittest.TestCase):
         self.assertEqual(sig.return_annotation, 42)
         self.assertEqual(sig, inspect.signature(test))
 
+    def test_signature_on_mangled_parameters(self):
+        class Spam:
+            def foo(self, __p1:1=2, *, __p2:2=3):
+                pass
+        class Ham(Spam):
+            pass
+
+        self.assertEqual(self.signature(Spam.foo),
+                         ((('self', ..., ..., "positional_or_keyword"),
+                           ('_Spam__p1', 2, 1, "positional_or_keyword"),
+                           ('_Spam__p2', 3, 2, "keyword_only")),
+                          ...))
+
+        self.assertEqual(self.signature(Spam.foo),
+                         self.signature(Ham.foo))
+
 
 class TestParameterObject(unittest.TestCase):
     def test_signature_parameter_kinds(self):
index 67a6f8d9dad83ef6db142ed72284d161f816b394..caad0969fe5c0f32af9f93f3f669f7c574dabb7e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
 
 - Issue #20595: Make getargs.c C89 compliant.
 
+- Issue #20625: Parameter names in __annotations__ were not mangled properly.
+  Discovered by Jonas Wielicki, patch by Yury Selivanov.
+
 Library
 -------
 
index a7ddc5a11ac220e0072835eeb78ff3b9249dba3a..57a232919d37b2041dac0a430139a81a5a56aba0 100644 (file)
@@ -1533,8 +1533,14 @@ compiler_visit_argannotation(struct compiler *c, identifier id,
 {
     if (annotation) {
         VISIT(c, expr, annotation);
-        if (PyList_Append(names, id))
+        PyObject *mangled = _Py_Mangle(c->u->u_private, id);
+        if (!mangled)
             return -1;
+        if (PyList_Append(names, mangled) < 0) {
+            Py_DECREF(mangled);
+            return -1;
+        }
+        Py_DECREF(mangled);
     }
     return 0;
 }