3.7+ "if not" in list comprehension and other bug fixes

This commit is contained in:
rocky
2020-01-03 09:01:48 -05:00
parent 1a901bde8f
commit 825add1af7
5 changed files with 22 additions and 16 deletions

View File

@@ -23,12 +23,10 @@ def columnize(l):
if not isinstance(l[i], str)]
assert [0, 2] == columnize([1, 'a', 2])
# FIXME:
if False:
# From 3.7 test_generators
# Bug was in handling the way list_if is optimized in 3.7+;
# We need list_if37 and compare_chained37.
def init_board(c):
return [io for io in c if 3 <= io < 5]
# From 3.7 test_generators
# Bug was in handling the way list_if is optimized in 3.7+;
# We need list_if37 and compare_chained37.
def init_board(c):
return [io for io in c if 3 <= io < 5]
assert init_board(list(range(6))) == [3, 4]
assert init_board(list(range(6))) == [3, 4]

View File

@@ -626,8 +626,9 @@ class Python37Parser(Python37BaseParser):
jmp_false37 ::= POP_JUMP_IF_FALSE COME_FROM
list_if ::= expr jmp_false37 list_iter
list_iter ::= list_if37
list_if37 ::= compare_chained37 list_iter
list_iter ::= list_if37_not
list_if37 ::= compare_chained37_false list_iter
list_if37_not ::= compare_chained37 list_iter
_ifstmts_jump ::= c_stmts_opt come_froms

View File

@@ -136,6 +136,7 @@ def customize_for_version37(self, version):
"importattr37": ("%c", (0, "IMPORT_NAME_ATTR")),
'list_if37': ( " if %p%c", (0, 27), 1 ),
'list_if37_not': ( " if not %p%c", (0, 27), 1 ),
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
"testfalse_not_and": ("not (%c)", 0),
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),

View File

@@ -1236,6 +1236,7 @@ class SourceWalker(GenericASTTraversal, object):
# Iterate to find the innermost store
# We'll come back to the list iteration below.
while n in ("list_iter", "comp_iter"):
# iterate one nesting deeper
if self.version == 3.0 and len(n) == 3:
@@ -1248,13 +1249,18 @@ class SourceWalker(GenericASTTraversal, object):
if n[2] == "store" and not store:
store = n[2]
n = n[3]
elif n in ("list_if", "list_if_not", "comp_if", "comp_if_not"):
have_not = n in ("list_if_not", "comp_if_not")
if_node = n[0]
if n[1] == "store":
store = n[1]
n = n[2]
pass
elif n in ("list_if", "list_if_not",
"list_if37", "list_if37_not",
"comp_if", "comp_if_not"):
have_not = n in ("list_if_not", "comp_if_not", "list_if37_not")
if n in ("list_if37", "list_if37_not"):
n = n[1]
else:
if_node = n[0]
if n[1] == "store":
store = n[1]
n = n[2]
pass
pass
# Python 2.7+ starts including set_comp_body