From d443295df674c072faf34c596622fc0241cb0211 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 31 Dec 2018 08:39:08 -0500 Subject: [PATCH] Check range of _come_froms on ifelsestmt reduction Fixes #200 --- test/bytecode_3.7_run/00_if_elif.pyc | Bin 0 -> 406 bytes test/simple_source/bug36/00_if_elif.py | 15 +++++++++++++++ uncompyle6/parsers/parse3.py | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 test/bytecode_3.7_run/00_if_elif.pyc create mode 100644 test/simple_source/bug36/00_if_elif.py 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 0000000000000000000000000000000000000000..b87ddc0a390eaa74c5791baf623f7c9613e1ac73 GIT binary patch literal 406 zcmYLFO-sW-5Z&3t8iNhuQ9MKtX^Yr`;7N*~*IarKic-?<)P|WjB=lg6Qp}Gm?2K!cjcKP@&0Hh9Zo^ zS)As08fMajk<5^sHlToc5nOAH$|N$oS4yh?6Mv^|yXIX^q{vfjZ*H+p&-dw)J@^6R C5L1-^ literal 0 HcmV?d00001 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):