Skip to content

Commit cca05cd

Browse files
committed
Merge pull request #15 from JuliaLang/fix_err
Descriptive error messages for julia exceptions
2 parents b8430c9 + 87b90ec commit cca05cd

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

julia/core.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def __init__(self, init_julia=True, jl_init_path=None):
257257
self.api.jl_typename_str.restype = char_p
258258
self.api.jl_typeof_str.restype = char_p
259259
self.api.jl_unbox_voidpointer.restype = py_object
260+
self.api.jl_bytestring_ptr.restype = char_p
260261

261262
if init_julia:
262263
try:
@@ -295,13 +296,22 @@ def call(self, src):
295296
# return null ptr if error
296297
ans = self.api.jl_eval_string(src.encode('utf-8'))
297298
if not ans:
298-
jexp = self.api.jl_exception_occurred()
299-
exception_str = self._unwrap_exception(jexp).decode('utf-8')
300-
raise JuliaError(u'Exception calling julia src: {}\n{}'
301-
.format(exception_str, src))
299+
exception_type = self._typeof_julia_exception_in_transit().decode('utf-8')
300+
exception_msg = self._capture_showerror_for_last_julia_exception().decode('utf-8')
301+
raise JuliaError(u'Exception \'{}\' ocurred while calling julia code:\n{}\n\nCode:\n{}'
302+
.format(exception_type, exception_msg, src))
302303
return ans
303304

304-
def _unwrap_exception(self, jl_exc):
305+
def _capture_showerror_for_last_julia_exception(self):
306+
msg = self.api.jl_eval_string(u"""
307+
try
308+
rethrow()
309+
catch ex
310+
sprint(showerror, ex, catch_backtrace())
311+
end""")
312+
return char_p(msg).value.encode("utf-8")
313+
314+
def _typeof_julia_exception_in_transit(self):
305315
exception = void_p.in_dll(self.api, 'jl_exception_in_transit')
306316
msg = self.api.jl_typeof_str(exception)
307317
return char_p(msg).value

julia/magic.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import sys
2222

2323
from IPython.core.magic import Magics, magics_class, line_cell_magic
24-
from julia import Julia
24+
from julia import Julia, JuliaError
2525

2626
#-----------------------------------------------------------------------------
2727
# Main classes
@@ -55,7 +55,14 @@ def julia(self, line, cell=None):
5555
Python namespace.
5656
"""
5757
src = unicode(line if cell is None else cell)
58-
return self.julia.eval(src)
58+
59+
try:
60+
ans = self.julia.eval(src)
61+
except JuliaError as e:
62+
print(e.message, file=sys.stderr)
63+
ans = None
64+
65+
return ans
5966

6067

6168
# Add to the global docstring the class information.

0 commit comments

Comments
 (0)