Skip to content
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

Fail with a helpful message if separate cache is not supported #186

Merged
merged 5 commits into from
Sep 10, 2018
Merged
Changes from 1 commit
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
Next Next commit
Add Julia._unbox_as
tkf committed Sep 6, 2018
commit a9549149baad821db73135c4c2030c85f646ffc1
50 changes: 50 additions & 0 deletions julia/core.py
Original file line number Diff line number Diff line change
@@ -346,6 +346,22 @@ def is_compatible_exe(jlinfo, _debug=lambda *_: None):

_julia_runtime = [False]


UNBOXABLE_TYPES = (
'bool',
'int8',
'uint8',
'int16',
'uint16',
'int32',
'uint32',
'int64',
'uint64',
'float32',
'float64',
)


class Julia(object):
"""
Implements a bridge to the Julia interpreter or library.
@@ -460,6 +476,17 @@ def __init__(self, init_julia=True, jl_runtime_path=None, jl_init_path=None,
self.api.jl_unbox_voidpointer.argtypes = [void_p]
self.api.jl_unbox_voidpointer.restype = py_object

for c_type in UNBOXABLE_TYPES:
jl_unbox = getattr(self.api, "jl_unbox_{}".format(c_type))
jl_unbox.argtypes = [void_p]
jl_unbox.restype = getattr(ctypes, "c_{}".format({
"float32": "float",
"float64": "double",
}.get(c_type, c_type)))

self.api.jl_typeof.argtypes = [void_p]
self.api.jl_typeof.restype = void_p

self.api.jl_exception_clear.restype = None
self.api.jl_stderr_obj.argtypes = []
self.api.jl_stderr_obj.restype = void_p
@@ -557,6 +584,29 @@ def _call(self, src):

return ans

@staticmethod
def _check_unboxable(c_type):
if c_type not in UNBOXABLE_TYPES:
raise ValueError("Julia value cannot be unboxed as c_type={!r}.\n"
"c_type supported by PyJulia are:\n"
"{}".format(c_type, "\n".join(UNBOXABLE_TYPES)))

def _is_unboxable_as(self, pointer, c_type):
self._check_unboxable(c_type)
jl_type = getattr(self.api, 'jl_{}_type'.format(c_type))
desired = ctypes.cast(jl_type, ctypes.POINTER(ctypes.c_void_p))[0]
actual = self.api.jl_typeof(pointer)
return actual == desired

def _unbox_as(self, pointer, c_type):
self._check_unboxable(c_type)
jl_unbox = getattr(self.api, 'jl_unbox_{}'.format(c_type))
if self._is_unboxable_as(pointer, c_type):
return jl_unbox(pointer)
else:
raise TypeError("Cannot unbox pointer {} as {}"
.format(pointer, c_type))

def check_exception(self, src="<unknown code>"):
exoc = self.api.jl_exception_occurred()
self._debug("exception occured? " + str(exoc))