From 631940887f4406d476f500170ca369ac831e646f Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 25 Mar 2018 17:34:38 -0400 Subject: [PATCH] Additional Python 2.x assert vs raise testing --- test/simple_source/bug27+/02_assert.py | 16 +++++++++++++++- uncompyle6/parsers/parse2.py | 8 ++++++-- uncompyle6/scanners/scanner2.py | 2 +- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/test/simple_source/bug27+/02_assert.py b/test/simple_source/bug27+/02_assert.py index 86e9a139..dbaccb9e 100644 --- a/test/simple_source/bug27+/02_assert.py +++ b/test/simple_source/bug27+/02_assert.py @@ -1,5 +1,5 @@ # From 2.7 test_argparse.py -# Bug was turnning assert into an "or raise" statement +# Bug was turning assert into an "or raise" statement def __call__(arg, dest): try: assert arg == 'spam', 'dest: %s' % dest @@ -15,3 +15,17 @@ def refactor_doctest(clipped, new): if not new: new += u"\n" return + +# From 2.7.14 test_hashlib.py +# The bug was turning assert into an "if" +# statement which isn't wrong, but we got the +# range of the if incorrect. When we have +# better control flow analysis we can revisit. +def test_threaded_hashing(): + for threadnum in xrange(1): + result = 1 + assert result > 0 + result = 2 + return result + +assert test_threaded_hashing() == 2 diff --git a/uncompyle6/parsers/parse2.py b/uncompyle6/parsers/parse2.py index 07acb21d..be8cf997 100644 --- a/uncompyle6/parsers/parse2.py +++ b/uncompyle6/parsers/parse2.py @@ -520,7 +520,7 @@ class Python2Parser(PythonParser): self.addRule(rule, nop_func) pass - self.check_reduce['aug_assign1'] = 'AST' + self.check_reduce['raise_stmt1'] = 'tokens' self.check_reduce['aug_assign2'] = 'AST' self.check_reduce['or'] = 'AST' # self.check_reduce['_stmts'] = 'AST' @@ -540,7 +540,11 @@ class Python2Parser(PythonParser): if lhs in ('aug_assign1', 'aug_assign2') and ast[0] and ast[0][0] in ('and', 'or'): return True - if rule == ('or', ('expr', 'jmp_true', 'expr', '\\e_come_from_opt')): + elif lhs in ('raise_stmt1',): + # We will assme 'LOAD_ASSERT' will be handled by an assert grammar rule + return (tokens[first] == 'LOAD_ASSERT' and + (last >= len(tokens) or tokens[last] != 'JUMP_FORWARD')) + elif rule == ('or', ('expr', 'jmp_true', 'expr', '\\e_come_from_opt')): expr2 = ast[2] return expr2 == 'expr' and expr2[0] == 'LOAD_ASSERT' return False diff --git a/uncompyle6/scanners/scanner2.py b/uncompyle6/scanners/scanner2.py index c485cb55..ae1f4c13 100644 --- a/uncompyle6/scanners/scanner2.py +++ b/uncompyle6/scanners/scanner2.py @@ -148,7 +148,7 @@ class Scanner2(Scanner): # raise AssertionError # and # assert ... - # Below we use the heuristic that it is preceded by a POP_JUMP. + # Below we use the heuristic that an "sssert" is preceded by a POP_JUMP. # however we could also use followed by RAISE_VARARGS # or for PyPy there may be a JUMP_IF_NOT_DEBUG before. # FIXME: remove uses of PJIF, and PJIT