3.x ""raise AssertionError" vs "assert"

Not sure this is totally correct yet.
This commit is contained in:
rocky 2016-07-14 10:31:12 -04:00
parent 7b7a9fa4cf
commit fb870ccd8d
2 changed files with 26 additions and 8 deletions

View File

@ -12,11 +12,18 @@ assert isinstance(1, int)
# 2.6
# assert2 ::= assert_expr jmp_true LOAD_ASSERT expr RAISE_VARARGS_2 come_from_pop
for method_name in self:
if method_name in self:
for method_name in ['a']:
if method_name in ('b',):
method = 'a'
else:
assert 0, "instance installed"
methods = 'b'
#
# _Bug in 3.x is not recognizing the assert
# producing:
# if not not do_setlocal:
# raise AssertError
def getpreferredencoding(do_setlocale=True):
assert not do_setlocale

View File

@ -133,6 +133,7 @@ class Scanner3(scan.Scanner):
bytecode = Bytecode(co, self.opc)
# FIXME: put as its own method?
# Scan for assertions. Later we will
# turn 'LOAD_GLOBAL' to 'LOAD_ASSERT'.
# 'LOAD_ASSERT' is used in assert statements.
@ -143,13 +144,23 @@ class Scanner3(scan.Scanner):
inst = bs[i]
# We need to detect the difference between
# "raise AssertionError" and
# "assert"
if inst.opname == 'POP_JUMP_IF_TRUE' and i+3 < n:
next_inst = bs[i+3]
# "raise AssertionError" and "assert"
# If we have a JUMP_FORWARD after the
# RAISE_VARARGS then we have a "raise" statement
# else we have an "assert" statement.
if inst.opname == 'POP_JUMP_IF_TRUE' and i+1 < n:
next_inst = bs[i+1]
if (next_inst.opname == 'LOAD_GLOBAL' and
next_inst.argval == 'AssertionError'):
self.load_asserts.add(next_inst.offset)
for j in range(i+2, n):
raise_inst = bs[j]
if raise_inst.opname.startswith('RAISE_VARARGS'):
if j+1 >= n or bs[j+1].opname != 'JUMP_FORWARD':
self.load_asserts.add(next_inst.offset)
pass
break
pass
pass
# Get jump targets
# Format: {target offset: [jump offsets]}