diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2014-02-18 17:49:41 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2014-02-18 17:49:41 (GMT) |
commit | 026019f89bf6669593d458dfa3ad9fc7b8d78bc2 (patch) | |
tree | a8d346de9c8006259fe7879d7c1b60aab62cca18 | |
parent | ee227ae7cf1f88e865c168a8f13743e92ade1938 (diff) | |
download | cpython-026019f89bf6669593d458dfa3ad9fc7b8d78bc2.zip cpython-026019f89bf6669593d458dfa3ad9fc7b8d78bc2.tar.gz cpython-026019f89bf6669593d458dfa3ad9fc7b8d78bc2.tar.bz2 |
Mangle __parameters in __annotations__ dict properly. Issue #20625.
-rw-r--r-- | Doc/whatsnew/3.4.rst | 4 | ||||
-rw-r--r-- | Lib/test/test_grammar.py | 7 | ||||
-rw-r--r-- | Lib/test/test_inspect.py | 16 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Python/compile.c | 8 |
5 files changed, 37 insertions, 1 deletions
diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst index faa35f0..1b71c57 100644 --- a/Doc/whatsnew/3.4.rst +++ b/Doc/whatsnew/3.4.rst @@ -1704,6 +1704,10 @@ Changes in the Python API informative :exc:`ValueError` rather than the previous more mysterious :exc:`AttributeError` (:issue:`9177`). +* Parameter names in ``__annotations__`` dict are now mangled properly, + similarly to ``__kwdefaults__``. (Contributed by Yury Selivanov in + :issue:`20625`). + Changes in the C API -------------------- diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py index 6b326bd..9da6338 100644 --- a/Lib/test/test_grammar.py +++ b/Lib/test/test_grammar.py @@ -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 diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 3f20419..a34a418 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -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): @@ -10,6 +10,9 @@ Release date: 2014-02-23 Core and Builtins ----------------- +- Issue #20625: Parameter names in __annotations__ were not mangled properly. + Discovered by Jonas Wielicki, patch by Yury Selivanov. + - Issue #20261: In pickle, lookup __getnewargs__ and __getnewargs_ex__ on the type of the object. diff --git a/Python/compile.c b/Python/compile.c index a7ddc5a..57a2329 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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; } |