Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/build/unreleased/368.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, codegen
:tickets: 368

Fixed issue where unpacking nested tuples in a for loop using would raise a
"couldn't apply loop context" error if the loop context was used. The regex
used to match the for loop expression now allows the list of loop variables
to contain parenthesized sub-tuples.
9 changes: 7 additions & 2 deletions mako/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,13 @@ def visitCallTag(self, node):


_FOR_LOOP = re.compile(
r"^for\s+((?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z0-9_]*),??)*\s*(?:\)?))\s+in\s+(.*):"
r"^for\s+((?:\(?)\s*"
r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
r"(?:\s*,\s*(?:"
r"(?:\(?)\s*[A-Za-z_][A-Za-z_0-9]*"
r"(?:\s*,\s*(?:[A-Za-z_][A-Za-z_0-9]*),??)*\s*(?:\)?)"
r"),??)*\s*(?:\)?))\s+in\s+(.*):"
)


Expand Down
10 changes: 10 additions & 0 deletions test/test_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def test__FOR_LOOP(self):
"x",
"[y+1 for y in [1, 2, 3]]",
),
(
"for ((key1, val1), (key2, val2)) in pairwise(dict.items()):",
"((key1, val1), (key2, val2))",
"pairwise(dict.items())",
),
(
"for (key1, val1), (key2, val2) in pairwise(dict.items()):",
"(key1, val1), (key2, val2)",
"pairwise(dict.items())",
),
):
match = _FOR_LOOP.match(statement)
assert match and match.groups() == (target_list, expression_list)
Expand Down