summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2015-05-05 20:16:41 -0400
committerBenjamin Peterson <benjamin@python.org>2015-05-05 20:16:41 -0400
commit025e9ebd0a0a19f50ca83af6ada0ac65be1fa2a1 (patch)
treed769adcb6d4a557a00923f18ed2b0ca8b515a473 /Lib
parent4ccc1514d070cabe80e8cfa0469dc03c12d08be2 (diff)
downloadcpython-git-025e9ebd0a0a19f50ca83af6ada0ac65be1fa2a1.tar.gz
PEP 448: additional unpacking generalizations (closes #2292)
Patch by Neil Girdhar.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/_bootstrap_external.py3
-rw-r--r--Lib/opcode.py6
-rw-r--r--Lib/test/test_ast.py56
-rw-r--r--Lib/test/test_extcall.py22
-rw-r--r--Lib/test/test_grammar.py6
-rw-r--r--Lib/test/test_parser.py12
-rw-r--r--Lib/test/test_syntax.py16
-rw-r--r--Lib/test/test_unpack_ex.py183
8 files changed, 263 insertions, 41 deletions
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index 9385d112d2..b76c550e9d 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -220,12 +220,13 @@ _code_type = type(_write_atomic.__code__)
# Python 3.4a4 3300 (more changes to __qualname__ computation)
# Python 3.4rc2 3310 (alter __qualname__ computation)
# Python 3.5a0 3320 (matrix multiplication operator)
+# Python 3.5b1 3330 (PEP 448: Additional Unpacking Generalizations)
#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
# due to the addition of new opcodes).
-MAGIC_NUMBER = (3320).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = (3330).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c
_PYCACHE = '__pycache__'
diff --git a/Lib/opcode.py b/Lib/opcode.py
index bfd3c4d8dd..c25afd44a7 100644
--- a/Lib/opcode.py
+++ b/Lib/opcode.py
@@ -200,4 +200,10 @@ hasfree.append(148)
def_op('EXTENDED_ARG', 144)
EXTENDED_ARG = 144
+def_op('BUILD_LIST_UNPACK', 149)
+def_op('BUILD_MAP_UNPACK', 150)
+def_op('BUILD_MAP_UNPACK_WITH_CALL', 151)
+def_op('BUILD_TUPLE_UNPACK', 152)
+def_op('BUILD_SET_UNPACK', 153)
+
del def_op, name_op, jrel_op, jabs_op
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index a533f8693b..1d9de2c4b5 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -427,17 +427,17 @@ class ASTHelpers_Test(unittest.TestCase):
self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
- "keywords=[], starargs=None, kwargs=None))])"
+ "keywords=[]))])"
)
self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
- "Str('and cheese')], [], None, None))])"
+ "Str('and cheese')], []))])"
)
self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
- "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
+ "col_offset=11)], keywords=[], "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
)
@@ -453,16 +453,16 @@ class ASTHelpers_Test(unittest.TestCase):
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
- [ast.Str('eggs')], [], None, None)))
+ [ast.Str('eggs')], [])))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
- "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
+ "col_offset=6)], keywords=[], "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
- "keywords=[], starargs=None, kwargs=None, lineno=1, "
+ "keywords=[], lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
@@ -487,8 +487,7 @@ class ASTHelpers_Test(unittest.TestCase):
node = ast.parse('foo()', mode='eval')
d = dict(ast.iter_fields(node.body))
self.assertEqual(d.pop('func').id, 'foo')
- self.assertEqual(d, {'keywords': [], 'kwargs': None,
- 'args': [], 'starargs': None})
+ self.assertEqual(d, {'keywords': [], 'args': []})
def test_iter_child_nodes(self):
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
@@ -604,8 +603,7 @@ class ASTValidatorTests(unittest.TestCase):
self._check_arguments(fac, self.stmt)
def test_classdef(self):
- def cls(bases=None, keywords=None, starargs=None, kwargs=None,
- body=None, decorator_list=None):
+ def cls(bases=None, keywords=None, body=None, decorator_list=None):
if bases is None:
bases = []
if keywords is None:
@@ -614,16 +612,12 @@ class ASTValidatorTests(unittest.TestCase):
body = [ast.Pass()]
if decorator_list is None:
decorator_list = []
- return ast.ClassDef("myclass", bases, keywords, starargs,
- kwargs, body, decorator_list)
+ return ast.ClassDef("myclass", bases, keywords,
+ body, decorator_list)
self.stmt(cls(bases=[ast.Name("x", ast.Store())]),
"must have Load context")
self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]),
"must have Load context")
- self.stmt(cls(starargs=ast.Name("x", ast.Store())),
- "must have Load context")
- self.stmt(cls(kwargs=ast.Name("x", ast.Store())),
- "must have Load context")
self.stmt(cls(body=[]), "empty body on ClassDef")
self.stmt(cls(body=[None]), "None disallowed")
self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]),
@@ -854,20 +848,12 @@ class ASTValidatorTests(unittest.TestCase):
func = ast.Name("x", ast.Load())
args = [ast.Name("y", ast.Load())]
keywords = [ast.keyword("w", ast.Name("z", ast.Load()))]
- stararg = ast.Name("p", ast.Load())
- kwarg = ast.Name("q", ast.Load())
- call = ast.Call(ast.Name("x", ast.Store()), args, keywords, stararg,
- kwarg)
+ call = ast.Call(ast.Name("x", ast.Store()), args, keywords)
self.expr(call, "must have Load context")
- call = ast.Call(func, [None], keywords, stararg, kwarg)
+ call = ast.Call(func, [None], keywords)
self.expr(call, "None disallowed")
bad_keywords = [ast.keyword("w", ast.Name("z", ast.Store()))]
- call = ast.Call(func, args, bad_keywords, stararg, kwarg)
- self.expr(call, "must have Load context")
- call = ast.Call(func, args, keywords, ast.Name("z", ast.Store()), kwarg)
- self.expr(call, "must have Load context")
- call = ast.Call(func, args, keywords, stararg,
- ast.Name("w", ast.Store()))
+ call = ast.Call(func, args, bad_keywords)
self.expr(call, "must have Load context")
def test_num(self):
@@ -957,8 +943,8 @@ exec_results = [
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], ('arg', (1, 7), 'args', None), [], [], None, []), [('Pass', (1, 14))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], ('arg', (1, 8), 'kwargs', None), []), [('Pass', (1, 17))], [], None)]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [('arg', (1, 6), 'a', None), ('arg', (1, 9), 'b', None), ('arg', (1, 14), 'c', None), ('arg', (1, 22), 'd', None), ('arg', (1, 28), 'e', None)], ('arg', (1, 35), 'args', None), [('arg', (1, 41), 'f', None)], [('Num', (1, 43), 42)], ('arg', (1, 49), 'kwargs', None), [('Num', (1, 11), 1), ('NameConstant', (1, 16), None), ('List', (1, 24), [], ('Load',)), ('Dict', (1, 30), [], [])]), [('Pass', (1, 58))], [], None)]),
-('Module', [('ClassDef', (1, 0), 'C', [], [], None, None, [('Pass', (1, 8))], [])]),
-('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], None, None, [('Pass', (1, 17))], [])]),
+('Module', [('ClassDef', (1, 0), 'C', [], [], [('Pass', (1, 8))], [])]),
+('Module', [('ClassDef', (1, 0), 'C', [('Name', (1, 8), 'object', ('Load',))], [], [('Pass', (1, 17))], [])]),
('Module', [('FunctionDef', (1, 0), 'f', ('arguments', [], None, [], [], None, []), [('Return', (1, 8), ('Num', (1, 15), 1))], [], None)]),
('Module', [('Delete', (1, 0), [('Name', (1, 4), 'v', ('Del',))])]),
('Module', [('Assign', (1, 0), [('Name', (1, 0), 'v', ('Store',))], ('Num', (1, 4), 1))]),
@@ -968,7 +954,7 @@ exec_results = [
('Module', [('If', (1, 0), ('Name', (1, 3), 'v', ('Load',)), [('Pass', (1, 5))], [])]),
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',)))], [('Pass', (1, 13))])]),
('Module', [('With', (1, 0), [('withitem', ('Name', (1, 5), 'x', ('Load',)), ('Name', (1, 10), 'y', ('Store',))), ('withitem', ('Name', (1, 13), 'z', ('Load',)), ('Name', (1, 18), 'q', ('Store',)))], [('Pass', (1, 21))])]),
-('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], [], None, None), None)]),
+('Module', [('Raise', (1, 0), ('Call', (1, 6), ('Name', (1, 6), 'Exception', ('Load',)), [('Str', (1, 16), 'string')], []), None)]),
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [('ExceptHandler', (3, 0), ('Name', (3, 7), 'Exception', ('Load',)), None, [('Pass', (4, 2))])], [], [])]),
('Module', [('Try', (1, 0), [('Pass', (2, 2))], [], [], [('Pass', (4, 2))])]),
('Module', [('Assert', (1, 0), ('Name', (1, 7), 'v', ('Load',)), None)]),
@@ -998,14 +984,14 @@ eval_results = [
('Expression', ('BinOp', (1, 0), ('Name', (1, 0), 'a', ('Load',)), ('Add',), ('Name', (1, 4), 'b', ('Load',)))),
('Expression', ('UnaryOp', (1, 0), ('Not',), ('Name', (1, 4), 'v', ('Load',)))),
('Expression', ('Lambda', (1, 0), ('arguments', [], None, [], [], None, []), ('NameConstant', (1, 7), None))),
-('Expression', ('Dict', (1, 0), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
+('Expression', ('Dict', (1, 2), [('Num', (1, 2), 1)], [('Num', (1, 4), 2)])),
('Expression', ('Dict', (1, 0), [], [])),
-('Expression', ('Set', (1, 0), [('NameConstant', (1, 1), None)])),
-('Expression', ('Dict', (1, 0), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
+('Expression', ('Set', (1, 1), [('NameConstant', (1, 1), None)])),
+('Expression', ('Dict', (2, 6), [('Num', (2, 6), 1)], [('Num', (4, 10), 2)])),
('Expression', ('ListComp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
('Expression', ('GeneratorExp', (1, 1), ('Name', (1, 1), 'a', ('Load',)), [('comprehension', ('Name', (1, 7), 'b', ('Store',)), ('Name', (1, 12), 'c', ('Load',)), [('Name', (1, 17), 'd', ('Load',))])])),
('Expression', ('Compare', (1, 0), ('Num', (1, 0), 1), [('Lt',), ('Lt',)], [('Num', (1, 4), 2), ('Num', (1, 8), 3)])),
-('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2)], [('keyword', 'c', ('Num', (1, 8), 3))], ('Name', (1, 11), 'd', ('Load',)), ('Name', (1, 15), 'e', ('Load',)))),
+('Expression', ('Call', (1, 0), ('Name', (1, 0), 'f', ('Load',)), [('Num', (1, 2), 1), ('Num', (1, 4), 2), ('Starred', (1, 10), ('Name', (1, 11), 'd', ('Load',)), ('Load',))], [('keyword', 'c', ('Num', (1, 8), 3)), ('keyword', None, ('Name', (1, 15), 'e', ('Load',)))])),
('Expression', ('Num', (1, 0), 10)),
('Expression', ('Str', (1, 0), 'string')),
('Expression', ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',))),
@@ -1016,6 +1002,6 @@ eval_results = [
('Expression', ('Tuple', (1, 0), [('Num', (1, 0), 1), ('Num', (1, 2), 2), ('Num', (1, 4), 3)], ('Load',))),
('Expression', ('Tuple', (1, 1), [('Num', (1, 1), 1), ('Num', (1, 3), 2), ('Num', (1, 5), 3)], ('Load',))),
('Expression', ('Tuple', (1, 0), [], ('Load',))),
-('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [], None, None)),
+('Expression', ('Call', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Attribute', (1, 0), ('Name', (1, 0), 'a', ('Load',)), 'b', ('Load',)), 'c', ('Load',)), 'd', ('Load',)), [('Subscript', (1, 8), ('Attribute', (1, 8), ('Name', (1, 8), 'a', ('Load',)), 'b', ('Load',)), ('Slice', ('Num', (1, 12), 1), ('Num', (1, 14), 2), None), ('Load',))], [])),
]
main()
diff --git a/Lib/test/test_extcall.py b/Lib/test/test_extcall.py
index 6b6c12de2e..654258eb24 100644
--- a/Lib/test/test_extcall.py
+++ b/Lib/test/test_extcall.py
@@ -34,17 +34,37 @@ Argument list examples
(1, 2, 3, 4, 5) {}
>>> f(1, 2, 3, *[4, 5])
(1, 2, 3, 4, 5) {}
+ >>> f(*[1, 2, 3], 4, 5)
+ (1, 2, 3, 4, 5) {}
>>> f(1, 2, 3, *UserList([4, 5]))
(1, 2, 3, 4, 5) {}
+ >>> f(1, 2, 3, *[4, 5], *[6, 7])
+ (1, 2, 3, 4, 5, 6, 7) {}
+ >>> f(1, *[2, 3], 4, *[5, 6], 7)
+ (1, 2, 3, 4, 5, 6, 7) {}
+ >>> f(*UserList([1, 2]), *UserList([3, 4]), 5, *UserList([6, 7]))
+ (1, 2, 3, 4, 5, 6, 7) {}
Here we add keyword arguments
>>> f(1, 2, 3, **{'a':4, 'b':5})
(1, 2, 3) {'a': 4, 'b': 5}
+ >>> f(1, 2, **{'a': -1, 'b': 5}, **{'a': 4, 'c': 6})
+ Traceback (most recent call last):
+ ...
+ TypeError: f() got multiple values for keyword argument 'a'
+ >>> f(1, 2, **{'a': -1, 'b': 5}, a=4, c=6)
+ Traceback (most recent call last):
+ ...
+ TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, 2, 3, *[4, 5], **{'a':6, 'b':7})
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b': 9})
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
+ >>> f(1, 2, 3, *[4, 5], **{'c': 8}, **{'a':6, 'b':7})
+ (1, 2, 3, 4, 5) {'a': 6, 'b': 7, 'c': 8}
+ >>> f(1, 2, 3, *(4, 5), x=6, y=7, **{'a':8, 'b': 9})
+ (1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7}
>>> f(1, 2, 3, **UserDict(a=4, b=5))
(1, 2, 3) {'a': 4, 'b': 5}
@@ -52,6 +72,8 @@ Here we add keyword arguments
(1, 2, 3, 4, 5) {'a': 6, 'b': 7}
>>> f(1, 2, 3, x=4, y=5, *(6, 7), **UserDict(a=8, b=9))
(1, 2, 3, 6, 7) {'a': 8, 'b': 9, 'x': 4, 'y': 5}
+ >>> f(1, 2, 3, *(4, 5), x=6, y=7, **UserDict(a=8, b=9))
+ (1, 2, 3, 4, 5) {'a': 8, 'b': 9, 'x': 6, 'y': 7}
Examples with invalid arguments (TypeErrors). We're also testing the function
names in the exception messages.
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 0c658587e9..28b1f044f2 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -296,8 +296,12 @@ class GrammarTests(unittest.TestCase):
return args, kwargs
self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
{'x':2, 'y':5}))
- self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
+ self.assertEqual(f(1, *(2,3), 4), ((1, 2, 3, 4), {}))
self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
+ self.assertEqual(f(**{'eggs':'scrambled', 'spam':'fried'}),
+ ((), {'eggs':'scrambled', 'spam':'fried'}))
+ self.assertEqual(f(spam='fried', **{'eggs':'scrambled'}),
+ ((), {'eggs':'scrambled', 'spam':'fried'}))
# argument annotation tests
def f(x) -> list: pass
diff --git a/Lib/test/test_parser.py b/Lib/test/test_parser.py
index 2112821237..18f16e3f52 100644
--- a/Lib/test/test_parser.py
+++ b/Lib/test/test_parser.py
@@ -313,7 +313,12 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
"except Exception as e:\n"
" raise ValueError from e\n")
+ def test_list_displays(self):
+ self.check_expr('[]')
+ self.check_expr('[*{2}, 3, *[4]]')
+
def test_set_displays(self):
+ self.check_expr('{*{2}, 3, *[4]}')
self.check_expr('{2}')
self.check_expr('{2,}')
self.check_expr('{2, 3}')
@@ -325,6 +330,13 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
self.check_expr('{a:b,}')
self.check_expr('{a:b, c:d}')
self.check_expr('{a:b, c:d,}')
+ self.check_expr('{**{}}')
+ self.check_expr('{**{}, 3:4, **{5:6, 7:8}}')
+
+ def test_argument_unpacking(self):
+ self.check_expr('f(a, *b, *c, *d)')
+ self.check_expr('f(**a, **b)')
+ self.check_expr('f(2, *a, *b, **b, **c, **d)')
def test_set_comprehensions(self):
self.check_expr('{x for x in seq}')
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index a9d3628819..a22cebb828 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -141,6 +141,9 @@ From ast_for_call():
>>> f(x for x in L, 1)
Traceback (most recent call last):
SyntaxError: Generator expression must be parenthesized if not sole argument
+>>> f(x for x in L, y for y in L)
+Traceback (most recent call last):
+SyntaxError: Generator expression must be parenthesized if not sole argument
>>> f((x for x in L), 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -582,7 +585,18 @@ class SyntaxTestCase(unittest.TestCase):
subclass=IndentationError)
def test_kwargs_last(self):
- self._check_error("int(base=10, '2')", "non-keyword arg")
+ self._check_error("int(base=10, '2')",
+ "positional argument follows keyword argument")
+
+ def test_kwargs_last2(self):
+ self._check_error("int(**{base: 10}, '2')",
+ "positional argument follows "
+ "keyword argument unpacking")
+
+ def test_kwargs_last3(self):
+ self._check_error("int(**{base: 10}, *['2'])",
+ "iterable argument unpacking follows "
+ "keyword argument unpacking")
def test_main():
support.run_unittest(SyntaxTestCase)
diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py
index 54666b0cf0..01f57b95e7 100644
--- a/Lib/test/test_unpack_ex.py
+++ b/Lib/test/test_unpack_ex.py
@@ -71,8 +71,185 @@ Multiple targets
>>> a == 0 and b == [1, 2, 3] and c == 4 and d == [0, 1, 2, 3] and e == 4
True
+Assignment unpacking
+
+ >>> a, b, *c = range(5)
+ >>> a, b, c
+ (0, 1, [2, 3, 4])
+ >>> *a, b, c = a, b, *c
+ >>> a, b, c
+ ([0, 1, 2], 3, 4)
+
+Set display element unpacking
+
+ >>> a = [1, 2, 3]
+ >>> sorted({1, *a, 0, 4})
+ [0, 1, 2, 3, 4]
+
+ >>> {1, *1, 0, 4}
+ Traceback (most recent call last):
+ ...
+ TypeError: 'int' object is not iterable
+
+Dict display element unpacking
+
+ >>> kwds = {'z': 0, 'w': 12}
+ >>> sorted({'x': 1, 'y': 2, **kwds}.items())
+ [('w', 12), ('x', 1), ('y', 2), ('z', 0)]
+
+ >>> sorted({**{'x': 1}, 'y': 2, **{'z': 3}}.items())
+ [('x', 1), ('y', 2), ('z', 3)]
+
+ >>> sorted({**{'x': 1}, 'y': 2, **{'x': 3}}.items())
+ [('x', 3), ('y', 2)]
+
+ >>> sorted({**{'x': 1}, **{'x': 3}, 'x': 4}.items())
+ [('x', 4)]
+
+ >>> {**{}}
+ {}
+
+ >>> a = {}
+ >>> {**a}[0] = 1
+ >>> a
+ {}
+
+ >>> {**1}
+ Traceback (most recent call last):
+ ...
+ TypeError: 'int' object is not a mapping
+
+ >>> {**[]}
+ Traceback (most recent call last):
+ ...
+ TypeError: 'list' object is not a mapping
+
+ >>> len(eval("{" + ", ".join("**{{{}: {}}}".format(i, i)
+ ... for i in range(1000)) + "}"))
+ 1000
+
+List comprehension element unpacking
+
+ >>> a, b, c = [0, 1, 2], 3, 4
+ >>> [*a, b, c]
+ [0, 1, 2, 3, 4]
+
+ >>> l = [a, (3, 4), {5}, {6: None}, (i for i in range(7, 10))]
+ >>> [*item for item in l]
+ Traceback (most recent call last):
+ ...
+ SyntaxError: iterable unpacking cannot be used in comprehension
+
+ >>> [*[0, 1] for i in range(10)]
+ Traceback (most recent call last):
+ ...
+ SyntaxError: iterable unpacking cannot be used in comprehension
+
+ >>> [*'a' for i in range(10)]
+ Traceback (most recent call last):
+ ...
+ SyntaxError: iterable unpacking cannot be used in comprehension
+
+ >>> [*[] for i in range(10)]
+ Traceback (most recent call last):
+ ...
+ SyntaxError: iterable unpacking cannot be used in comprehension
+
+Generator expression in function arguments
+
+ >>> list(*x for x in (range(5) for i in range(3)))
+ Traceback (most recent call last):
+ ...
+ list(*x for x in (range(5) for i in range(3)))
+ ^
+ SyntaxError: invalid syntax
+
+ >>> dict(**x for x in [{1:2}])
+ Traceback (most recent call last):
+ ...
+ dict(**x for x in [{1:2}])
+ ^
+ SyntaxError: invalid syntax
+
+Iterable argument unpacking
+
+ >>> print(*[1], *[2], 3)
+ 1 2 3
+
+Make sure that they don't corrupt the passed-in dicts.
+
+ >>> def f(x, y):
+ ... print(x, y)
+ ...
+ >>> original_dict = {'x': 1}
+ >>> f(**original_dict, y=2)
+ 1 2
+ >>> original_dict
+ {'x': 1}
+
Now for some failures
+Make sure the raised errors are right for keyword argument unpackings
+
+ >>> from collections.abc import MutableMapping
+ >>> class CrazyDict(MutableMapping):
+ ... def __init__(self):
+ ... self.d = {}
+ ...
+ ... def __iter__(self):
+ ... for x in self.d.__iter__():
+ ... if x == 'c':
+ ... self.d['z'] = 10
+ ... yield x
+ ...
+ ... def __getitem__(self, k):
+ ... return self.d[k]
+ ...
+ ... def __len__(self):
+ ... return len(self.d)
+ ...
+ ... def __setitem__(self, k, v):
+ ... self.d[k] = v
+ ...
+ ... def __delitem__(self, k):
+ ... del self.d[k]
+ ...
+ >>> d = CrazyDict()
+ >>> d.d = {chr(ord('a') + x): x for x in range(5)}
+ >>> e = {**d}
+ Traceback (most recent call last):
+ ...
+ RuntimeError: dictionary changed size during iteration
+
+ >>> d.d = {chr(ord('a') + x): x for x in range(5)}
+ >>> def f(**kwargs): print(kwargs)
+ >>> f(**d)
+ Traceback (most recent call last):
+ ...
+ RuntimeError: dictionary changed size during iteration
+
+Overridden parameters
+
+ >>> f(x=5, **{'x': 3}, y=2)
+ Traceback (most recent call last):
+ ...
+ TypeError: f() got multiple values for keyword argument 'x'
+
+ >>> f(**{'x': 3}, x=5, y=2)
+ Traceback (most recent call last):
+ ...
+ TypeError: f() got multiple values for keyword argument 'x'
+
+ >>> f(**{'x': 3}, **{'x': 5}, y=2)
+ Traceback (most recent call last):
+ ...
+ TypeError: f() got multiple values for keyword argument 'x'
+
+ >>> f(**{1: 3}, **{1: 5})
+ Traceback (most recent call last):
+ ...
+ TypeError: f() keywords must be strings
+
Unpacking non-sequence
>>> a, *b = 7
@@ -138,17 +315,17 @@ Now some general starred expressions (all fail).
>>> *a # doctest:+ELLIPSIS
Traceback (most recent call last):
...
- SyntaxError: can use starred expression only as assignment target
+ SyntaxError: can't use starred expression here
>>> *1 # doctest:+ELLIPSIS
Traceback (most recent call last):
...
- SyntaxError: can use starred expression only as assignment target
+ SyntaxError: can't use starred expression here
>>> x = *a # doctest:+ELLIPSIS
Traceback (most recent call last):
...
- SyntaxError: can use starred expression only as assignment target
+ SyntaxError: can't use starred expression here
Some size constraints (all fail.)