Skip to content

Commit fb870cc

Browse files
committed
3.x ""raise AssertionError" vs "assert"
Not sure this is totally correct yet.
1 parent 7b7a9fa commit fb870cc

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

test/simple_source/stmts/15_assert.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
# 2.6
1313
# assert2 ::= assert_expr jmp_true LOAD_ASSERT expr RAISE_VARARGS_2 come_from_pop
1414

15-
for method_name in self:
16-
if method_name in self:
15+
for method_name in ['a']:
16+
if method_name in ('b',):
1717
method = 'a'
1818
else:
1919
assert 0, "instance installed"
2020

2121
methods = 'b'
22-
#
22+
23+
# _Bug in 3.x is not recognizing the assert
24+
# producing:
25+
# if not not do_setlocal:
26+
# raise AssertError
27+
28+
def getpreferredencoding(do_setlocale=True):
29+
assert not do_setlocale

uncompyle6/scanners/scanner3.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def disassemble(self, co, classname=None, code_objects={}, show_asm=None):
133133

134134
bytecode = Bytecode(co, self.opc)
135135

136+
# FIXME: put as its own method?
136137
# Scan for assertions. Later we will
137138
# turn 'LOAD_GLOBAL' to 'LOAD_ASSERT'.
138139
# 'LOAD_ASSERT' is used in assert statements.
@@ -143,13 +144,23 @@ def disassemble(self, co, classname=None, code_objects={}, show_asm=None):
143144
inst = bs[i]
144145

145146
# We need to detect the difference between
146-
# "raise AssertionError" and
147-
# "assert"
148-
if inst.opname == 'POP_JUMP_IF_TRUE' and i+3 < n:
149-
next_inst = bs[i+3]
147+
# "raise AssertionError" and "assert"
148+
# If we have a JUMP_FORWARD after the
149+
# RAISE_VARARGS then we have a "raise" statement
150+
# else we have an "assert" statement.
151+
if inst.opname == 'POP_JUMP_IF_TRUE' and i+1 < n:
152+
next_inst = bs[i+1]
150153
if (next_inst.opname == 'LOAD_GLOBAL' and
151154
next_inst.argval == 'AssertionError'):
152-
self.load_asserts.add(next_inst.offset)
155+
for j in range(i+2, n):
156+
raise_inst = bs[j]
157+
if raise_inst.opname.startswith('RAISE_VARARGS'):
158+
if j+1 >= n or bs[j+1].opname != 'JUMP_FORWARD':
159+
self.load_asserts.add(next_inst.offset)
160+
pass
161+
break
162+
pass
163+
pass
153164

154165
# Get jump targets
155166
# Format: {target offset: [jump offsets]}

0 commit comments

Comments
 (0)