Correct some 2.6 bugs in chained compare ...

and other bugs

* main.py: was botching a version triple test
* parse2{5,6}.py: Add 2.6 try/except end position checking via COME_FROM offsets
* parse26.py: adjust grammar rule for chained-compare2

such as in try-except
This commit is contained in:
rocky
2022-03-05 03:53:44 -05:00
parent 2efe2b5b47
commit c5bc21bf6a
4 changed files with 21 additions and 6 deletions

View File

@@ -315,7 +315,7 @@ def main(
else:
buffering = 0
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', buffering)
if PYTHON_VERSION_TRIPLE > (2, 6):
if PYTHON_VERSION_TRIPLE >= (2, 7):
tee = subprocess.Popen(["tee", current_outfile],
stdin=subprocess.PIPE)
os.dup2(tee.stdin.fileno(), sys.stdout.fileno())

View File

@@ -98,7 +98,7 @@ class Python25Parser(Python26Parser):
""")
super(Python25Parser, self).customize_grammar_rules(tokens, customize)
if self.version[:2] == (2, 5):
self.check_reduce["try_except"] = "tokens"
self.check_reduce["try_except"] = "AST"
self.check_reduce["aug_assign1"] = "AST"
self.check_reduce["ifelsestmt"] = "AST"

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',
(