JuliaPy / pyjulia

python interface to julia
MIT License
885 stars 101 forks source link

Improve exception handling in %julia magic #266

Open tkf opened 5 years ago

tkf commented 5 years ago

Current behavior of %julia magic is to print exception from Julia to stderr and continue executing. This is not ideal because

Reading PR #14 (where this behavior was introduced), it seems that the aim was for making the error clear. This is better be done using _render_traceback_. It's actually pretty easy to use it. Just adding

class JuliaError(Exception):
    def _render_traceback_(self):
        return [str(self)]

yields

In [22]: Main.eval("""error("some error")""")
Exception 'some error' occurred while calling julia code:
error("some error")

Note that the error message is succinct even without %julia magic (as long as you are in IPython). However, this is probably too succinct to recognize it as an exception. We need to include Julia traceback to make it more informative and look like an exception. As it takes a bit more coding, let's just stop catching exception in %julia magic (already done in #265) and then worry about traceback later. This way, we don't have to change the programmable interface later.

tkf commented 5 years ago

@stevengj @marius311 Let me know if you have different opinions.