Commit 591199d1 authored by Stefan Behnel's avatar Stefan Behnel

optimise iteration over dict(...kwargs...).items() etc. and not only dict(*args).items() etc.

parent 37566326
......@@ -27,6 +27,9 @@ Bugs fixed
* ``isinf()`` declarations in ``libc/math.pxd`` and ``numpy/math.pxd`` now
reflect the actual tristate ``int`` return value instead of using ``bint``.
* Iteration over ``dict(...).items()`` failed to get optimised when dict
arguments included keyword arguments.
Other changes
-------------
......
......@@ -211,7 +211,7 @@ class IterationTransform(Visitor.EnvTransform):
if not is_safe_iter and method in ('keys', 'values', 'items'):
# try to reduce this to the corresponding .iter*() methods
if isinstance(base_obj, ExprNodes.SimpleCallNode):
if isinstance(base_obj, ExprNodes.CallNode):
inner_function = base_obj.function
if (inner_function.is_name and inner_function.name == 'dict'
and inner_function.entry
......
......@@ -505,3 +505,25 @@ def values_of_expression(**kwargs):
"""
# this can be optimised even in Py2
return [ arg for arg in dict(kwargs.items()).values() ]
def items_of_expression(*args, **kwargs):
"""
>>> sorted(items_of_expression(a=3, b=4))
[('a', 3), ('b', 4)]
>>> sorted(items_of_expression([('a', 3)], b=4))
[('a', 3), ('b', 4)]
"""
return [item for item in dict(*args, **kwargs).items()]
def iteritems_of_expression(*args, **kwargs):
"""
>>> sorted(iteritems_of_expression(a=3, b=4))
[('a', 3), ('b', 4)]
>>> sorted(iteritems_of_expression([('a', 3)], b=4))
[('a', 3), ('b', 4)]
"""
return [item for item in dict(*args, **kwargs).iteritems()]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment