Python 2.6 generator rule with 'and' conditional

Fixes #283.
This commit is contained in:
rocky
2019-08-05 10:36:08 -04:00
parent a781006ff1
commit fd580f3c60
4 changed files with 14 additions and 1 deletions

Binary file not shown.

View File

@@ -0,0 +1,7 @@
# Issue #283 in Python 2.6
# See https://github.com/rocky/python-uncompyle6/issues/283
# This code is RUNNABLE!
G = ( c for c in "spam, Spam, SPAM!" if c > 'A' and c < 'S')
assert list(G) == ["P", "M"]

View File

@@ -241,6 +241,9 @@ class Python26Parser(Python2Parser):
genexpr_func ::= setup_loop_lf FOR_ITER store comp_iter JUMP_ABSOLUTE come_froms
POP_TOP jb_pop jb_pb_come_from
genexpr_func ::= setup_loop_lf FOR_ITER store comp_iter JUMP_BACK come_froms
POP_TOP jb_pb_come_from
generator_exp ::= LOAD_GENEXPR MAKE_FUNCTION_0 expr GET_ITER CALL_FUNCTION_1 COME_FROM
list_if ::= list_if ::= expr jmp_false_then list_iter
'''

View File

@@ -234,7 +234,7 @@ class Python27Parser(Python2Parser):
return invalid
if rule == ('and', ('expr', 'jmp_false', 'expr', '\\e_come_from_opt')):
# If the instruction after the instructions formin "and" is an "YIELD_VALUE"
# If the instruction after the instructions forming "and" is a "YIELD_VALUE"
# then this is probably an "if" inside a comprehension.
if tokens[last] == 'YIELD_VALUE':
# Note: We might also consider testing last+1 being "POP_TOP"
@@ -243,6 +243,9 @@ class Python27Parser(Python2Parser):
# Test that jmp_false jumps to the end of "and"
# or that it jumps to the same place as the end of "and"
jmp_false = ast[1][0]
# FIXME: if the jmp_false is POP_JUMP_IF_FALSE is the address
# is *absoulte* and the calulation below is wrong!
jmp_target = jmp_false.offset + jmp_false.attr + 3
return not (jmp_target == tokens[last].offset or
tokens[last].pattr == jmp_false.pattr)