Skip to content

Improve exception handling in %julia magic #266

Open
@tkf

Description

@tkf

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

  • A language interface library should not hide exception. If there is an exception, let users handle it. This is particularly important for line magics which can be surrounded by normal Python code.

  • It was inconsistent in that Python errors thrown in Julia are actually properly raised. (This was something I noticed while reviewing add variable interpolation #262 and found test failures like this)

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions