Additional Python 2.x assert vs raise testing

This commit is contained in:
rocky
2018-03-25 17:34:38 -04:00
parent 2ae9cd7d08
commit 631940887f
3 changed files with 22 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
# From 2.7 test_argparse.py # 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): def __call__(arg, dest):
try: try:
assert arg == 'spam', 'dest: %s' % dest assert arg == 'spam', 'dest: %s' % dest
@@ -15,3 +15,17 @@ def refactor_doctest(clipped, new):
if not new: if not new:
new += u"\n" new += u"\n"
return 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

View File

@@ -520,7 +520,7 @@ class Python2Parser(PythonParser):
self.addRule(rule, nop_func) self.addRule(rule, nop_func)
pass pass
self.check_reduce['aug_assign1'] = 'AST' self.check_reduce['raise_stmt1'] = 'tokens'
self.check_reduce['aug_assign2'] = 'AST' self.check_reduce['aug_assign2'] = 'AST'
self.check_reduce['or'] = 'AST' self.check_reduce['or'] = 'AST'
# self.check_reduce['_stmts'] = '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'): if lhs in ('aug_assign1', 'aug_assign2') and ast[0] and ast[0][0] in ('and', 'or'):
return True 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] expr2 = ast[2]
return expr2 == 'expr' and expr2[0] == 'LOAD_ASSERT' return expr2 == 'expr' and expr2[0] == 'LOAD_ASSERT'
return False return False

View File

@@ -148,7 +148,7 @@ class Scanner2(Scanner):
# raise AssertionError # raise AssertionError
# and # and
# assert ... # 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 # however we could also use followed by RAISE_VARARGS
# or for PyPy there may be a JUMP_IF_NOT_DEBUG before. # or for PyPy there may be a JUMP_IF_NOT_DEBUG before.
# FIXME: remove uses of PJIF, and PJIT # FIXME: remove uses of PJIF, and PJIT