Constrain ifelsestmt on 2.6 more.

This commit is contained in:
rocky
2020-01-15 21:32:39 -05:00
parent 3fbe0b90e3
commit 0677ddc8fb
5 changed files with 49 additions and 11 deletions

View File

@@ -32,3 +32,21 @@ assert eval_print_amount(True, False, False, False, []) == 0
assert eval_print_amount(False, True, True, False, []) == 2
assert eval_print_amount(False, False, True, True, []) == 3
assert eval_print_amount(False, False, False, True, []) == 0
# Bug in 2.6 was in including the part at x = value
# at the end asa part of the "else"
def eval_directive(a):
if a:
value = 2
else:
try:
value = 3
except:
pass
x = value
return x
assert eval_directive(True) == 2
assert eval_directive(False) == 3

View File

@@ -27,7 +27,7 @@ that a later phase can turn into a sequence of ASCII text.
from __future__ import print_function
from uncompyle6.parsers.reducecheck import (except_handler_else, tryelsestmt)
from uncompyle6.parsers.reducecheck import (except_handler_else, ifelsestmt, tryelsestmt)
from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func
from uncompyle6.parsers.treenode import SyntaxTree
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
@@ -643,6 +643,7 @@ class Python2Parser(PythonParser):
self.reduce_check_table = {
# "and": and_check,
"except_handler_else": except_handler_else,
"ifelsestmt": ifelsestmt,
# "or": or_check,
"tryelsestmt": tryelsestmt,
"tryelsestmtl": tryelsestmt,

View File

@@ -345,6 +345,7 @@ class Python26Parser(Python2Parser):
self.check_reduce['and'] = 'AST'
self.check_reduce['assert_expr_and'] = 'AST'
self.check_reduce["ifstmt"] = "tokens"
self.check_reduce["ifelsestmt"] = "AST"
self.check_reduce['list_for'] = 'AST'
self.check_reduce['try_except'] = 'tokens'
self.check_reduce['tryelsestmt'] = 'AST'

View File

@@ -53,6 +53,15 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
"opt_come_from_except",
),
),
(
"ifelsestmt",
(
"testexpr",
"c_stmts_opt",
"jf_cf_pop",
"else_suite",
),
),
):
return False
@@ -62,13 +71,14 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
# offset COME_FROM is last, it is sufficient to test
# just the last one.
come_froms = ast[-1]
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
come_froms = come_froms[0]
if not isinstance(come_froms, Token):
if len(come_froms):
return tokens[first].offset > come_froms[-1].attr
elif tokens[first].offset > come_froms.attr:
return True
if come_froms.kind != "else_suite":
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
come_froms = come_froms[0]
if not isinstance(come_froms, Token):
if len(come_froms):
return tokens[first].offset > come_froms[-1].attr
elif tokens[first].offset > come_froms.attr:
return True
# For mysterious reasons a COME_FROM in tokens[last+1] might be part of the grammar rule
# even though it is not found in come_froms.
@@ -94,10 +104,18 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
if last == n:
last -= 1
jmp = test[1]
jmp_target = jmp[0].attr
jmp_target = int(jmp[0].pattr)
# FIXME: the jump inside "else" check below should be added.
#
# Make sure we don't jump at the end of the "then" inside the "else"
# (jf_cf_pop may be a 2.6ish specific thing.)
jf_cf_pop = ast[2]
if jf_cf_pop == "jf_cf_pop" and jf_cf_pop[0] == "JUMP_FORWARD":
if int(jf_cf_pop[0].pattr) < tokens[last-1].off2int():
return True
# The jump inside "else" check below should be added.
# add this until we can find out what's wrong with
# not being able to parse:
# if a and b or c: