Correct some Python 2.6 chain compare bugs

This commit is contained in:
rocky
2022-03-05 04:03:27 -05:00
parent d366248b47
commit 3490389a66
2 changed files with 19 additions and 4 deletions

View File

@@ -6,7 +6,7 @@ spark grammar differences over Python2 for Python 2.6.
from uncompyle6.parser import PythonParserSingle
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6.parsers.parse2 import Python2Parser
from uncompyle6.parsers.reducecheck import (except_handler, tryelsestmt)
from uncompyle6.parsers.reducecheck import (except_handler, tryexcept, tryelsestmt)
class Python26Parser(Python2Parser):
@@ -318,6 +318,8 @@ class Python26Parser(Python2Parser):
return_lambda ::= RETURN_VALUE_LAMBDA
compare_chained2 ::= expr COMPARE_OP return_expr_lambda
compare_chained2 ::= expr COMPARE_OP RETURN_END_IF_LAMBDA
compare_chained2 ::= expr COMPARE_OP RETURN_END_IF COME_FROM
return_if_lambda ::= RETURN_END_IF_LAMBDA POP_TOP
stmt ::= if_exp_lambda
@@ -354,6 +356,7 @@ class Python26Parser(Python2Parser):
self.reduce_check_table = {
"except_handler": except_handler,
"tryelsestmt": tryelsestmt,
"try_except": tryexcept,
"tryelsestmtl": tryelsestmt,
}
@@ -366,7 +369,7 @@ class Python26Parser(Python2Parser):
self.check_reduce["forelselaststmtl"] = "tokens"
self.check_reduce["forelsestmt"] = "tokens"
self.check_reduce['list_for'] = 'AST'
self.check_reduce['try_except'] = 'tokens'
self.check_reduce['try_except'] = 'AST'
self.check_reduce['tryelsestmt'] = 'AST'
self.check_reduce['tryelsestmtl'] = 'AST'

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2020 Rocky Bernstein
# Copyright (c) 2020, 2022 Rocky Bernstein
def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
come_from_except = ast[-1]
@@ -13,12 +13,24 @@ def tryexcept(self, lhs, n, rule, ast, tokens, first, last):
),
):
if come_from_except[0] == "COME_FROM":
# There should be at last two COME_FROMs, one from an
# There should be at least two COME_FROMs, one from an
# exception handler and one from the try. Otherwise
# we have a try/else.
return True
pass
elif rule == (
"try_except",
(
"SETUP_EXCEPT",
"suite_stmts_opt",
"POP_BLOCK",
"except_handler",
"COME_FROM",
),
):
return come_from_except.attr < tokens[first].offset
elif rule == (
'try_except',
(