diff --git a/test/bytecode_3.7_run/00_if_elif.pyc b/test/bytecode_3.7_run/00_if_elif.pyc new file mode 100644 index 00000000..b87ddc0a Binary files /dev/null and b/test/bytecode_3.7_run/00_if_elif.pyc differ diff --git a/test/simple_source/bug36/00_if_elif.py b/test/simple_source/bug36/00_if_elif.py new file mode 100644 index 00000000..0867a01f --- /dev/null +++ b/test/simple_source/bug36/00_if_elif.py @@ -0,0 +1,15 @@ +# Python 3 bug in not detecting the end bounds of if elif. +def testit(b): + if b == 1: + a = 1 + elif b == 2: + a = 2 + else: + a = 4 + + return a + +for x in (1, 2, 4): + x = testit(x) + assert x is not None, "Should have returned a value, not None" + assert x == x diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index 437a9e3f..c716e069 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -26,6 +26,7 @@ If we succeed in creating a parse tree, then we have a Python program that a later phase can turn into a sequence of ASCII text. """ +from uncompyle6.scanners.tok import Token from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func from uncompyle6.parsers.treenode import SyntaxTree from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG @@ -1136,6 +1137,7 @@ class Python3Parser(PythonParser): self.check_reduce['aug_assign2'] = 'AST' self.check_reduce['while1stmt'] = 'noAST' self.check_reduce['while1elsestmt'] = 'noAST' + self.check_reduce['ifelsestmt'] = 'AST' self.check_reduce['annotate_tuple'] = 'noAST' self.check_reduce['kwarg'] = 'noAST' # FIXME: remove parser errors caused by the below @@ -1212,6 +1214,14 @@ class Python3Parser(PythonParser): if offset != tokens[first].attr: return True return False + elif rule == ('ifelsestmt', + ('testexpr', 'c_stmts_opt', 'jump_forward_else', 'else_suite', '_come_froms')): + # Make sure the highest/smallest "come from" offset comes inside the "if". + come_froms = ast[-1] + if not isinstance(come_froms, Token): + return tokens[first].offset > come_froms[-1].attr + return False + return False class Python30Parser(Python3Parser):