[Python-checkins] cpython (merge 3.5 -> default): Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py
berker.peksag
python-checkins at python.org
Sun Mar 6 09:50:38 EST 2016
https://hg.python.org/cpython/rev/1b212e366b66
changeset: 100434:1b212e366b66
parent: 100431:046c75c9cd33
parent: 100433:a34428aff42d
user: Berker Peksag <berker.peksag at gmail.com>
date: Sun Mar 06 16:50:44 2016 +0200
summary:
Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py
Patch by Guo Ci Teo.
files:
Lib/test/test_tools/test_unparse.py | 5 +++++
Misc/NEWS | 3 +++
Tools/parser/unparse.py | 15 ++++++++++++---
3 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/Lib/test/test_tools/test_unparse.py b/Lib/test/test_tools/test_unparse.py
--- a/Lib/test/test_tools/test_unparse.py
+++ b/Lib/test/test_tools/test_unparse.py
@@ -259,6 +259,11 @@
def test_with_two_items(self):
self.check_roundtrip(with_two_items)
+ def test_dict_unpacking_in_dict(self):
+ # See issue 26489
+ self.check_roundtrip(r"""{**{'y': 2}, 'x': 1}""")
+ self.check_roundtrip(r"""{**{'y': 2}, **{'x': 1}}""")
+
class DirectoryTestCase(ASTTestCase):
"""Test roundtrip behaviour on all files in Lib and Lib/test."""
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -828,6 +828,9 @@
Tools/Demos
-----------
+- Issue #26489: Add dictionary unpacking support to Tools/parser/unparse.py.
+ Patch by Guo Ci Teo.
+
- Issue #26316: Fix variable name typo in Argument Clinic.
- Issue #25440: Fix output of python-config --extension-suffix.
diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py
--- a/Tools/parser/unparse.py
+++ b/Tools/parser/unparse.py
@@ -456,12 +456,21 @@
def _Dict(self, t):
self.write("{")
- def write_pair(pair):
- (k, v) = pair
+ def write_key_value_pair(k, v):
self.dispatch(k)
self.write(": ")
self.dispatch(v)
- interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
+
+ def write_item(item):
+ k, v = item
+ if k is None:
+ # for dictionary unpacking operator in dicts {**{'y': 2}}
+ # see PEP 448 for details
+ self.write("**")
+ self.dispatch(v)
+ else:
+ write_key_value_pair(k, v)
+ interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values))
self.write("}")
def _Tuple(self, t):
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list