Skip to content
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
20 changes: 15 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,22 @@ 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 ex
sprint(showerror, ex, catch_backtrace())
end""")
return char_p(msg).value.encode("utf-8")
Copy link
Contributor

Choose a reason for hiding this comment

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

From my understanding of julia's capi, jl_eval_string()'s return value must be unboxed to get to the actual char* returned by sprint(). This is why I originally used return self.api.jl_bytestring_ptr(msg), which doesn't segfault for me.


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