Skip to content

Descriptive error messages for julia exceptions #14

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

Merged
merged 2 commits into from
Aug 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions julia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def __init__(self, init_julia=True, jl_init_path=None):
self.api.jl_typename_str.restype = char_p
self.api.jl_typeof_str.restype = char_p
self.api.jl_unbox_voidpointer.restype = py_object
self.api.jl_bytestring_ptr.restype = char_p

if init_julia:
try:
Expand Down Expand Up @@ -295,13 +296,26 @@ def call(self, src):
# return null ptr if error
ans = self.api.jl_eval_string(src.encode('utf-8'))
if not ans:
jexp = self.api.jl_exception_occurred()
exception_str = self._unwrap_exception(jexp).decode('utf-8')
raise JuliaError(u'Exception calling julia src: {}\n{}'
.format(exception_str, src))
exception_type = self._typeof_julia_exception_in_transit().decode('utf-8')
exception_msg = self._capture_showerror_for_last_julia_exception().decode('utf-8')
raise JuliaError(u'Exception \'{}\' ocurred while calling julia code:\n{}\n\nCode:\n{}'
.format(exception_type, exception_msg, src))
return ans

def _unwrap_exception(self, jl_exc):
def _capture_showerror_for_last_julia_exception(self):
msg = self.api.jl_eval_string(u"""
try
rethrow()
catch e
b = IOBuffer()
showerror(b, e, catch_backtrace())
seekstart(b)
return readall(b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be simpler to just do sprint(showerror, e, catch_backtrace())

end
""")
return self.api.jl_bytestring_ptr(msg)

def _typeof_julia_exception_in_transit(self):
exception = void_p.in_dll(self.api, 'jl_exception_in_transit')
msg = self.api.jl_typeof_str(exception)
return char_p(msg).value
Expand Down
11 changes: 9 additions & 2 deletions julia/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import sys

from IPython.core.magic import Magics, magics_class, line_cell_magic
from julia import Julia
from julia import Julia, JuliaError

#-----------------------------------------------------------------------------
# Main classes
Expand Down Expand Up @@ -55,7 +55,14 @@ def julia(self, line, cell=None):
Python namespace.
"""
src = unicode(line if cell is None else cell)
return self.julia.eval(src)

try:
ans = self.julia.eval(src)
except JuliaError as e:
print(e.message, file=sys.stderr)
ans = None

return ans


# Add to the global docstring the class information.
Expand Down