From f2f17740ee204fa73895fa61278498c04770532f Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 5 May 2019 16:09:10 -0400 Subject: [PATCH] 2.7 if_expr_true restriction ... condition_true -> if_expr_true condition_lambda -> if_expr_lambda These correspond to the Python AST names better. --- test/bytecode_2.7_run/04_ifelse_parens.pyc | Bin 0 -> 447 bytes .../branching/02_ifelse_lambda.py | 2 +- test/simple_source/bug26/03_weird26.py | 2 +- test/simple_source/bug27+/03_not_dead_code.py | 4 ++-- uncompyle6/parsers/parse25.py | 4 ++-- uncompyle6/parsers/parse26.py | 12 +++++----- uncompyle6/parsers/parse27.py | 21 ++++++++---------- uncompyle6/parsers/parse3.py | 14 ++++++------ uncompyle6/semantics/consts.py | 16 ++++++------- uncompyle6/semantics/helper.py | 2 +- 10 files changed, 37 insertions(+), 40 deletions(-) create mode 100644 test/bytecode_2.7_run/04_ifelse_parens.pyc diff --git a/test/bytecode_2.7_run/04_ifelse_parens.pyc b/test/bytecode_2.7_run/04_ifelse_parens.pyc new file mode 100644 index 0000000000000000000000000000000000000000..17be18a0e31120e27b1d4173a9de1ef40bb6f890 GIT binary patch literal 447 zcmb7A%TB{E5F96gwn*>`T#(3#0)ieYgg`uwJ+zz<5QW4^t)@xvItQfm0{kC8!i^tb zH&o(`;JD(TQ-&O9f$PUTtQ1;44lMexyJB4WqS&6`lZTfcRvuapv(V$4 z6C*~UxN;@tRjIo|Zj)JNbdi#i-K^Wrn1;;I0qt`}W?2_HqN=fp9zHku@o0E>5}8aF zq@y~vx}<~p)eZP;G>>T>#c3*ELQ)UvNEvz0Bou8b+LYtBn>*WRC;Dy7W^RYGOCoKZ dsmdE`EBn{W{~PMF+u5>8n?j#={m6d|f*-lrRO$c# literal 0 HcmV?d00001 diff --git a/test/simple_source/branching/02_ifelse_lambda.py b/test/simple_source/branching/02_ifelse_lambda.py index b94f7dcd..6b68ee52 100644 --- a/test/simple_source/branching/02_ifelse_lambda.py +++ b/test/simple_source/branching/02_ifelse_lambda.py @@ -22,7 +22,7 @@ assert i[0]('a') == True assert i[0]('A') == False # Issue #170. Bug is needing an "conditional_not_lambda" grammar rule -# in addition the the "conditional_lambda" rule +# in addition the the "if_expr_lambda" rule j = lambda a: False if not a else True assert j(True) == True assert j(False) == False diff --git a/test/simple_source/bug26/03_weird26.py b/test/simple_source/bug26/03_weird26.py index 9ff54cfc..34e176b0 100644 --- a/test/simple_source/bug26/03_weird26.py +++ b/test/simple_source/bug26/03_weird26.py @@ -8,7 +8,7 @@ list(x for x in range(10) if x % 2 if x % 3) # expresion which evaluates True unconditionally, # but leave dead code or junk around that we have to match on. -# Tests "conditional_true" rule +# Tests "if_expr_true" rule 5 if 1 else 2 0 or max(5, 3) if 0 else 3 diff --git a/test/simple_source/bug27+/03_not_dead_code.py b/test/simple_source/bug27+/03_not_dead_code.py index f5d167d3..f4698761 100644 --- a/test/simple_source/bug27+/03_not_dead_code.py +++ b/test/simple_source/bug27+/03_not_dead_code.py @@ -1,6 +1,6 @@ # Bug found in 2.7 test_itertools.py -# Bug was erroneously using reduction to unconditional_true -# A proper fix would be to use unconditional_true only when we +# Bug was erroneously using reduction to if_expr_true +# A proper fix would be to use if_expr_true only when we # can determine there is or was dead code. from itertools import izip_longest for args in [['abc', range(6)]]: diff --git a/uncompyle6/parsers/parse25.py b/uncompyle6/parsers/parse25.py index a54cd904..ef2447a2 100644 --- a/uncompyle6/parsers/parse25.py +++ b/uncompyle6/parsers/parse25.py @@ -74,9 +74,9 @@ class Python25Parser(Python26Parser): return_stmt_lambda ::= ret_expr RETURN_VALUE_LAMBDA setupwithas ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 setup_finally stmt ::= classdefdeco - stmt ::= conditional_lambda + stmt ::= if_expr_lambda stmt ::= conditional_not_lambda - conditional_lambda ::= expr jmp_false_then expr return_if_lambda + if_expr_lambda ::= expr jmp_false_then expr return_if_lambda return_stmt_lambda LAMBDA_MARKER conditional_not_lambda ::= expr jmp_true_then expr return_if_lambda diff --git a/uncompyle6/parsers/parse26.py b/uncompyle6/parsers/parse26.py index 172f4ea3..525a84d6 100644 --- a/uncompyle6/parsers/parse26.py +++ b/uncompyle6/parsers/parse26.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2018 Rocky Bernstein +# Copyright (c) 2017-2019 Rocky Bernstein """ spark grammar differences over Python2 for Python 2.6. """ @@ -293,19 +293,19 @@ class Python26Parser(Python2Parser): compare_chained2 ::= expr COMPARE_OP return_lambda return_if_lambda ::= RETURN_END_IF_LAMBDA POP_TOP - stmt ::= conditional_lambda + stmt ::= if_expr_lambda stmt ::= conditional_not_lambda - conditional_lambda ::= expr jmp_false_then expr return_if_lambda + if_expr_lambda ::= expr jmp_false_then expr return_if_lambda return_stmt_lambda LAMBDA_MARKER conditional_not_lambda ::= expr jmp_true_then expr return_if_lambda return_stmt_lambda LAMBDA_MARKER - # conditional_true are for conditions which always evaluate true + # if_expr_true are for conditions which always evaluate true # There is dead or non-optional remnants of the condition code though, # and we use that to match on to reconstruct the source more accurately - expr ::= conditional_true - conditional_true ::= expr jf_pop expr COME_FROM + expr ::= if_expr_true + if_expr_true ::= expr jf_pop expr COME_FROM # This comes from # 0 or max(5, 3) if 0 else 3 diff --git a/uncompyle6/parsers/parse27.py b/uncompyle6/parsers/parse27.py index 3704eecd..e9c176d3 100644 --- a/uncompyle6/parsers/parse27.py +++ b/uncompyle6/parsers/parse27.py @@ -112,14 +112,14 @@ class Python27Parser(Python2Parser): compare_chained2 ::= expr COMPARE_OP return_lambda compare_chained2 ::= expr COMPARE_OP return_lambda - # conditional_true are for conditions which always evaluate true + # if_expr_true are for conditions which always evaluate true # There is dead or non-optional remnants of the condition code though, # and we use that to match on to reconstruct the source more accurately. # FIXME: we should do analysis and reduce *only* if there is dead code? # right now we check that expr is "or". Any other nodes types? - expr ::= conditional_true - conditional_true ::= expr JUMP_FORWARD expr COME_FROM + expr ::= if_expr_true + if_expr_true ::= expr JUMP_FORWARD expr COME_FROM conditional ::= expr jmp_false expr JUMP_FORWARD expr COME_FROM conditional ::= expr jmp_false expr JUMP_ABSOLUTE expr @@ -181,9 +181,9 @@ class Python27Parser(Python2Parser): # Common with 2.6 return_if_lambda ::= RETURN_END_IF_LAMBDA COME_FROM - stmt ::= conditional_lambda - stmt ::= conditional_not_lambda - conditional_lambda ::= expr jmp_false expr return_if_lambda + stmt ::= if_expr_lambda + stmt ::= conditional_not_lambda + if_expr_lambda ::= expr jmp_false expr return_if_lambda return_stmt_lambda LAMBDA_MARKER conditional_not_lambda ::= expr jmp_true expr return_if_lambda @@ -216,7 +216,7 @@ class Python27Parser(Python2Parser): self.check_reduce['raise_stmt1'] = 'AST' self.check_reduce['list_if_not'] = 'AST' self.check_reduce['list_if'] = 'AST' - self.check_reduce['conditional_true'] = 'AST' + self.check_reduce['if_expr_true'] = 'tokens' self.check_reduce['whilestmt'] = 'tokens' return @@ -268,11 +268,8 @@ class Python27Parser(Python2Parser): while (tokens[i] != 'JUMP_BACK'): i -= 1 return tokens[i].attr != tokens[i-1].attr - # elif rule[0] == ('conditional_true'): - # # FIXME: the below is a hack: we check expr for - # # nodes that could have possibly been a been a Boolean. - # # We should also look for the presence of dead code. - # return ast[0] == 'expr' and ast[0] == 'or' + elif rule[0] == 'if_expr_true': + return (first) > 0 and tokens[first-1] == 'POP_JUMP_IF_FALSE' return False diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index 44b5b3aa..c7679e73 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -324,9 +324,9 @@ class Python3Parser(PythonParser): def p_stmt3(self, args): """ - stmt ::= conditional_lambda + stmt ::= if_expr_lambda stmt ::= conditional_not_lambda - conditional_lambda ::= expr jmp_false expr return_if_lambda + if_expr_lambda ::= expr jmp_false expr return_if_lambda return_stmt_lambda LAMBDA_MARKER conditional_not_lambda ::= expr jmp_true expr return_if_lambda @@ -406,11 +406,11 @@ class Python3Parser(PythonParser): # a JUMP_ABSOLUTE with no COME_FROM conditional ::= expr jmp_false expr jump_absolute_else expr - # conditional_true are for conditions which always evaluate true + # if_expr_true are for conditions which always evaluate true # There is dead or non-optional remnants of the condition code though, # and we use that to match on to reconstruct the source more accurately - expr ::= conditional_true - conditional_true ::= expr JUMP_FORWARD expr COME_FROM + expr ::= if_expr_true + if_expr_true ::= expr JUMP_FORWARD expr COME_FROM """ @staticmethod @@ -584,9 +584,9 @@ class Python3Parser(PythonParser): stmt ::= assign2_pypy assign3_pypy ::= expr expr expr store store store assign2_pypy ::= expr expr store store - stmt ::= conditional_lambda + stmt ::= if_expr_lambda stmt ::= conditional_not_lambda - conditional_lambda ::= expr jmp_false expr return_if_lambda + if_expr_lambda ::= expr jmp_false expr return_if_lambda return_lambda LAMBDA_MARKER conditional_not_lambda ::= expr jmp_true expr return_if_lambda diff --git a/uncompyle6/semantics/consts.py b/uncompyle6/semantics/consts.py index b90ad958..a2a98097 100644 --- a/uncompyle6/semantics/consts.py +++ b/uncompyle6/semantics/consts.py @@ -95,7 +95,7 @@ PRECEDENCE = { 'conditional_lamdba': 28, 'conditional_not_lamdba': 28, 'conditionalnot': 28, - 'conditional_true': 28, + 'if_expr_true': 28, 'ret_cond': 28, '_mklambda': 30, @@ -288,19 +288,19 @@ TABLE_DIRECT = { 'and2': ( '%c', 3 ), 'or': ( '%c or %c', 0, 2 ), 'ret_or': ( '%c or %c', 0, 2 ), - 'conditional': ( '%p if %p else %p', (2, 27), (0, 27), (4, 27) ), - 'conditional_true': ( '%p if 1 else %p', (0, 27), (2, 27) ), + 'conditional': ( '%p if %c else %c', + (2, 'expr', 27), 0, 4 ), + 'if_expr_lambda': ( '%p if %c else %c', + (2, 'expr', 27), (0, 'expr'), 4 ), + 'if_expr_true': ( '%p if 1 else %c', (0, 'expr', 27), 2 ), 'ret_cond': ( '%p if %p else %p', (2, 27), (0, 27), (-1, 27) ), 'conditional_not': ( '%p if not %p else %p', (2, 27), (0, "expr", PRECEDENCE['unary_not']), (4, 27) ), - 'conditional_lambda': - ( '%c if %c else %c', - (2, 'expr'), 0, 4 ), 'conditional_not_lambda': - ( '%c if not %c else %c', - (2, 'expr'), 0, 4 ), + ( '%p if not %c else %c', + (2, 'expr', 27), 0, 4 ), 'compare_single': ( '%p %[-1]{pattr.replace("-", " ")} %p', (0, 19), (1, 19) ), 'compare_chained': ( '%p %p', (0, 29), (1, 30)), diff --git a/uncompyle6/semantics/helper.py b/uncompyle6/semantics/helper.py index 19b89770..78708b6c 100644 --- a/uncompyle6/semantics/helper.py +++ b/uncompyle6/semantics/helper.py @@ -50,7 +50,7 @@ def find_globals_and_nonlocals(node, globs, nonlocals, code, version): # # print("XXX", n.kind, global_ops) # if isinstance(n, SyntaxTree): # # FIXME: do I need a caser for n.kind="mkfunc"? -# if n.kind in ("conditional_lambda", "return_lambda"): +# if n.kind in ("if_expr_lambda", "return_lambda"): # globs = find_globals(n, globs, mklambda_globals) # else: # globs = find_globals(n, globs, global_ops)