Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception handling #844

Open
wants to merge 10 commits into
base: master
from
@@ -883,3 +883,56 @@ def test_try_multiple_except_else_finally(self):
print("Do final cleanup")
print('Done.')
""", exits_early=True)

def test_except_an_expr(self):
self.assertCodeExecution("""
f = lambda: ValueError
try:
raise ValueError("YO")
except f():
print('Got a dynamic exception')
else:
print("Should do not print this")
finally:
print("Do final cleanup")
print('Done.')
""")

self.assertCodeExecution("""
f = lambda: ValueError
try:
raise ValueError
except f() as e:
print('Got a dynamic exception', e)
else:
print("Should do not print this")
finally:
print("Do final cleanup")
print('Done.')
""")

self.assertCodeExecution("""
f = lambda: ValueError
try:
raise TypeError("YO")
except f():
print("Should not print this")
else:
print("Do else handling")
finally:
print("Do final cleanup")
print('Done.')
""", exits_early=True)

self.assertCodeExecution("""
x = ValueError
try:
raise ValueError
except x as e:
print('Got a dynamic exception', e)
else:
print("Should do not print this")
finally:
print("Do final cleanup")
print('Done.')
""")
@@ -2300,7 +2300,120 @@ def visit_Index(self, node):

@node_visitor
def visit_ExceptHandler(self, node):
builtin_exceptions = [
"ArithmeticError",
"AssertionError",
"AttributeError",
"BaseException",
"BlockingIOError",
"BrokenPipeError",
"BufferError",
"BytesWarning",
"ChildProcessError",
"ConnectionAbortedError",
"ConnectionError",
"ConnectionRefusedError",
"ConnectionResetError",
"DeprecationWarning",
"EOFError",
"Exception",
"FileExistsError",
"FileNotFoundError",
"FloatingPointError",
"FutureWarning",
"GeneratorExit",
"ImportError",
"ImportWarning",
"IndentationError",
"IndexError",
"InterruptedError",
"IsADirectoryError",
"KeyError",
"KeyboardInterrupt",
"LookupError",
"MemoryError",
"NameError",
"NotADirectoryError",
"NotImplementedError",
"OSError",
"OverflowError",
"PendingDeprecationWarning",
"PermissionError",
"ProcessLookupError",
"ReferenceError",
"ResourceWarning",
"RuntimeError",
"RuntimeWarning",
"StandardError",
"StopIteration",
"SyntaxError",
"SyntaxWarning",
"SystemError",
"SystemExit",
"TabError",
"TimeoutError",
"TypeError",
"UnboundLocalError",
"UnicodeDecodeError",
"UnicodeEncodeError",
"UnicodeError",
"UnicodeTranslateError",
"UnicodeWarning",
"UserWarning",
"ValueError",
"Warning",
"ZeroDivisionError",
]
if node.type in builtin_exceptions:

This comment has been minimized.

Copy link
@BPYap

BPYap Jun 26, 2018

Contributor

@Heisenberg815 @eliasdorneles, so I was compiling asyncio modules using the codes from this PR and I encountered a minor bug where voc transpilation fails when node.type is None.

A potential fix would be adding or node.type is None condition on this line (line 2367) to redirect the program flow to self._visitCoreException(node), where node.type is None is being handled.

Cheers 😃

self._visitCoreException(node)

# expr? type, identifier? name, stmt* body):
else:
exception = self.full_classref('BaseException', default_prefix='org.python.exceptions')
self.context.add_opcodes(
CATCH(exception),
)
self.context.add_opcodes(
JavaOpcodes.DUP()
)
self.visit(node.type)
self.context.add_opcodes(
JavaOpcodes.INVOKESTATIC(
'org/Python',
'isinstance',
args=[
'Lorg/python/Object;',
'Lorg/python/Object;',
],
returns='Lorg/python/types/Bool;'
)
)
self.context.add_opcodes(
IF([python.Object.as_boolean()], JavaOpcodes.IFEQ),
)
if node.name:
# The exception has been named. Store it as that name.
self.context.store_name(node.name)
else:
self.context.add_opcodes(
JavaOpcodes.POP()
)

for child in node.body:
self.visit(child)
self.context.add_opcodes(
ELSE()
)
self.context.add_opcodes(
JavaOpcodes.CHECKCAST('java/lang/Throwable'),
JavaOpcodes.ATHROW(),
)
self.context.add_opcodes(
END_IF(),
)


def _visitCoreException(self, node):
if isinstance(node.type, ast.Tuple):
exception = [
self.full_classref(exc.id, default_prefix='org.python.exceptions')
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.