示例#1
0
    def check_exception(self, src=None):
        exoc = self.api.jl_exception_occurred()
        self._debug("exception occured? " + str(exoc))
        if not exoc:
            # self._debug("No Exception")
            self.api.jl_exception_clear()
            return

        # If, theoretically, an exception happens in early stage of
        # self.add_module_functions("Base"), showerror and sprint as
        # below does not work.  Let's use jl_typeof_str in such case.
        try:
            sprint = self.sprint
            showerror = self.showerror
        except AttributeError:
            res = None
        else:
            res = self.api.jl_call2(void_p(self.api.convert),
                                    void_p(self.api.PyObject),
                                    void_p(exoc))
        if res is None:
            exception = self.api.jl_typeof_str(exoc).decode('utf-8')
        else:
            exception = sprint(showerror, self._as_pyobj(res))
        raise JuliaError(u'Exception \'{}\' occurred while calling julia code:\n{}'
                         .format(exception, src))
示例#2
0
 def _as_pyobj(self, res, src=None):
     if res == 0:
         return None
     boxed_obj = self.api.jl_get_field(void_p(res), b'o')
     pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
     # make sure we incref it before returning it,
     # as this is a borrowed reference
     ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
     return pyobj
示例#3
0
    def eval(self, src):
        """ Execute code in Julia, then pull some results back to Python. """
        if src is None:
            return None
        ans = self._call(src)
        if not ans:
            return None
        res = self.api.jl_call2(void_p(self.api.convert), void_p(self.api.PyObject), void_p(ans))

        if res is None:
            self.check_exception(src)
        return self._as_pyobj(res, "convert(PyCall.PyObject, {})".format(src))
示例#4
0
文件: core.py 项目: gyenney/Tools
 def eval(self, src):
     """ Execute code in Julia, then pull some results back to Python. """
     if src is None:
         return None
     ans = self.call(src)
     res = self.api.jl_call1(void_p(self.api.PyObject), void_p(ans))
     if not res:
         # TODO: introspect the julia error object here
         raise JuliaError("ErrorException in Julia PyObject: " "{}".format(src))
     boxed_obj = self.api.jl_get_field(void_p(res), b"o")
     pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
     # make sure we incref it before returning it,
     # as this is a borrowed reference
     ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
     return pyobj
示例#5
0
文件: core.py 项目: gyenney/Tools
 def eval(self, src):
     """ Execute code in Julia, then pull some results back to Python. """
     if src is None:
         return None
     ans = self.call(src)
     res = self.api.jl_call1(void_p(self.api.PyObject), void_p(ans))
     if not res:
         #TODO: introspect the julia error object here
         raise JuliaError("ErrorException in Julia PyObject: "
                          "{}".format(src))
     boxed_obj = self.api.jl_get_field(void_p(res), b'o')
     pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
     # make sure we incref it before returning it,
     # as this is a borrowed reference
     ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
     return pyobj
示例#6
0
    def eval(self, src):
        """ Execute code in Julia, then pull some results back to Python. """
        if src is None:
            return None
        ans = self._call(src)
        if not ans:
            return None
        res = self.api.jl_call2(void_p(self.api.convert), void_p(self.api.PyObject), void_p(ans))

        if res is None:
            self.check_exception("convert(PyCall.PyObject, %s)" % src)
        if res == 0:
            return None
        boxed_obj = self.api.jl_get_field(void_p(res), b'o')
        pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
        # make sure we incref it before returning it,
        # as this is a borrowed reference
        ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
        return pyobj
示例#7
0
 def eval(self, src):
     """ Execute code in Julia, then pull some results back to Python. """
     if src is None:
         return None
     # ans = self.call(src)
     # print self.api.jl_symbol("PyObject")
     # print self.api.jl_get_global(self.api.jl_main_module, self.api.jl_symbol("PyObject"))
     # res = self.api.jl_call1(void_p(self.api.PyObject), void_p(ans))
     res = self.api.jl_eval_string("PyObject({})".format(src))
     if not res:
         # TODO: introspect the julia error object here
         print self._capture_showerror_for_last_julia_exception()
         raise JuliaError("ErrorException in Julia PyObject: " "{}".format(src))
     boxed_obj = self.api.jl_get_field(void_p(res), b"o")
     pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
     # make sure we incref it before returning it,
     # as this is a borrowed reference
     ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
     return pyobj
示例#8
0
文件: core.py 项目: jurreht/pyjulia
    def eval(self, src):
        """ Execute code in Julia, then pull some results back to Python. """
        if src is None:
            return None
        ans = self._call(src)
        if not ans:
            return None
        res = self.api.jl_call2(void_p(self.api.convert),
                                void_p(self.api.PyObject), void_p(ans))

        if res is None:
            self.check_exception("convert(PyCall.PyObject, %s)" % src)
        if res == 0:
            return None
        boxed_obj = self.api.jl_get_field(void_p(res), b'o')
        pyobj = self.api.jl_unbox_voidpointer(void_p(boxed_obj))
        # make sure we incref it before returning it,
        # as this is a borrowed reference
        ctypes.pythonapi.Py_IncRef(ctypes.py_object(pyobj))
        return pyobj
 def to_numpy(self):
     "See pycl.py for buffer_to_ndarray"
     out = np.empty((self.height, self.width, 4), dtype=np.float32)
     assert out.flags.contiguous, "Don't know how to write non-contiguous yet."
     evt = cl.clEnqueueReadImage(
         self.cldev.queue,
         self.data,
         void_p(out.__array_interface__["data"][0]),
         (0, 0, 0),
         (self.width, self.height, 1),
         self.width * 4 * 4,
         0,
     )
     evt.wait()
     return out[:self.original_height, :self.original_width]
 def from_numpy(self, source):
     h, w = source.shape[0], source.shape[1]
     if h % 8 != 0 or w % 8 != 0:
         padding = np.zeros(
             (min_ptwo(h, 8), min_ptwo(w, 8), source.shape[2]),
             dtype=source.dtype)
         padding[:h, :w] = source[:, :]
         source = padding
     ary = np.ascontiguousarray(source)
     if ary.__array_interface__["strides"]:
         raise ValueError("I don't know how to handle strided arrays yet.")
     ptr = void_p(ary.__array_interface__["data"][0])
     # print(self.data._height, self.data._width, ary.shape)
     evt = cl.clEnqueueWriteImage(self.cldev.queue, self.data, ptr,
                                  (0, 0, 0),
                                  (ary.shape[1], ary.shape[0], 1), 0, 0)
     evt.wait()