You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Constrain ifelsestmt on 2.6 more.
This commit is contained in:
Binary file not shown.
@@ -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, True, True, False, []) == 2
|
||||||
assert eval_print_amount(False, False, True, True, []) == 3
|
assert eval_print_amount(False, False, True, True, []) == 3
|
||||||
assert eval_print_amount(False, False, False, True, []) == 0
|
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
|
||||||
|
@@ -27,7 +27,7 @@ that a later phase can turn into a sequence of ASCII text.
|
|||||||
|
|
||||||
from __future__ import print_function
|
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.parser import PythonParser, PythonParserSingle, nop_func
|
||||||
from uncompyle6.parsers.treenode import SyntaxTree
|
from uncompyle6.parsers.treenode import SyntaxTree
|
||||||
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
|
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
|
||||||
@@ -643,6 +643,7 @@ class Python2Parser(PythonParser):
|
|||||||
self.reduce_check_table = {
|
self.reduce_check_table = {
|
||||||
# "and": and_check,
|
# "and": and_check,
|
||||||
"except_handler_else": except_handler_else,
|
"except_handler_else": except_handler_else,
|
||||||
|
"ifelsestmt": ifelsestmt,
|
||||||
# "or": or_check,
|
# "or": or_check,
|
||||||
"tryelsestmt": tryelsestmt,
|
"tryelsestmt": tryelsestmt,
|
||||||
"tryelsestmtl": tryelsestmt,
|
"tryelsestmtl": tryelsestmt,
|
||||||
|
@@ -345,6 +345,7 @@ class Python26Parser(Python2Parser):
|
|||||||
self.check_reduce['and'] = 'AST'
|
self.check_reduce['and'] = 'AST'
|
||||||
self.check_reduce['assert_expr_and'] = 'AST'
|
self.check_reduce['assert_expr_and'] = 'AST'
|
||||||
self.check_reduce["ifstmt"] = "tokens"
|
self.check_reduce["ifstmt"] = "tokens"
|
||||||
|
self.check_reduce["ifelsestmt"] = "AST"
|
||||||
self.check_reduce['list_for'] = 'AST'
|
self.check_reduce['list_for'] = 'AST'
|
||||||
self.check_reduce['try_except'] = 'tokens'
|
self.check_reduce['try_except'] = 'tokens'
|
||||||
self.check_reduce['tryelsestmt'] = 'AST'
|
self.check_reduce['tryelsestmt'] = 'AST'
|
||||||
|
@@ -53,6 +53,15 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
|
|||||||
"opt_come_from_except",
|
"opt_come_from_except",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"ifelsestmt",
|
||||||
|
(
|
||||||
|
"testexpr",
|
||||||
|
"c_stmts_opt",
|
||||||
|
"jf_cf_pop",
|
||||||
|
"else_suite",
|
||||||
|
),
|
||||||
|
),
|
||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -62,6 +71,7 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
|
|||||||
# offset COME_FROM is last, it is sufficient to test
|
# offset COME_FROM is last, it is sufficient to test
|
||||||
# just the last one.
|
# just the last one.
|
||||||
come_froms = ast[-1]
|
come_froms = ast[-1]
|
||||||
|
if come_froms.kind != "else_suite":
|
||||||
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
|
if come_froms == "opt_come_from_except" and len(come_froms) > 0:
|
||||||
come_froms = come_froms[0]
|
come_froms = come_froms[0]
|
||||||
if not isinstance(come_froms, Token):
|
if not isinstance(come_froms, Token):
|
||||||
@@ -94,10 +104,18 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
|
|||||||
if last == n:
|
if last == n:
|
||||||
last -= 1
|
last -= 1
|
||||||
jmp = test[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
|
# add this until we can find out what's wrong with
|
||||||
# not being able to parse:
|
# not being able to parse:
|
||||||
# if a and b or c:
|
# if a and b or c:
|
||||||
|
Reference in New Issue
Block a user