def ext_gridloop_exceptions(self, f): """Test error handling in the extension module.""" x_p = self.c.py2my(self.xcoor) y_p1 = self.c.py2my(self.ycoor[1:]) f_p = self.c.set_pyfunc(f) try: #1 ext_gridloop.gridloop1((1, 2), x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #2 ext_gridloop.gridloop1(x_p, x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #3 ext_gridloop.gridloop2(x_p, y_p, 'abc') except: print sys.exc_type, sys.exc_value try: #4 # cannot be handled by this interface... #ext_gridloop.gridloop2(array(self.xcoor,Complex64), # self.ycoor, 'abc') pass except: print sys.exc_type, sys.exc_value try: #5 # cannot be handled by this interface... #ext_gridloop.gridloop2(array([[0,0],[1,2]]), self.ycoor, 'abc') pass except: print sys.exc_type, sys.exc_value
def ext_gridloop1(self, f): """Compute a[i,j] = f(xi,yj) in an external routine.""" lx = size(self.xcoor) ly = size(self.ycoor) a = zeros((lx, ly), Float) try: # are we in Fortran? a = ext_gridloop.as_column_major_storage(a) except: pass if isinstance(f, str): f_compiled = compile(f, '<string>', 'eval') def f_str(x, y): return eval(f_compiled) #return eval(f) # much slower func = f_str else: func = f a_p = self.c.py2my(a) x_p = self.c.py2my(self.xcoor) y_p = self.c.py2my(self.ycoor) f_p = self.c.set_pyfunc(func) ext_gridloop.gridloop1(a_p, x_p, y_p, f_p) return a
def ext_gridloop1(self, f): """Compute a[i,j] = f(xi,yj) in an external routine.""" a = zeros((self.nx, self.ny)) # C/C++ or Fortran module? if ext_gridloop.__doc__ is not None: if 'f2py' in ext_gridloop.__doc__: a = asarray(a, order='Fortran') a_p = self.c.py2my(a) x_p = self.c.py2my(self.xcoor) y_p = self.c.py2my(self.ycoor) f_p = self.c.set_pyfunc(f) ext_gridloop.gridloop1(a_p, x_p, y_p, f_p) return a
def ext_gridloop1(self, f): """Compute a[i,j] = f(xi,yj) in an external routine.""" # a is made here, sent to the routine, and then returned a = zeros((self.xcoor.size, self.ycoor.size)) # C/C++ or Fortran module? if ext_gridloop.__doc__ is not None: if 'f2py' in ext_gridloop.__doc__: # Fortran extension module a = asarray(a, order='Fortran') # could store a as self.a to avoid making Fortran # arrays in repeated calls ext_gridloop.gridloop1(a, self.xcoor, self.ycoor, f) return a
def ext_gridloop_exceptions(self, f): """Test error handling in the extension module.""" x_p = self.c.py2my(self.xcoor) y_p1 = self.c.py2my(self.ycoor[1:]) f_p = self.c.set_pyfunc(f) try: #1 ext_gridloop.gridloop1((1, 2), x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #2 ext_gridloop.gridloop1(x_p, x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #3 ext_gridloop.gridloop2(x_p, y_p, 'abc') except: print sys.exc_type, sys.exc_value
def ext_gridloop_exceptions(self, f): """Test error handling in the extension module.""" x_p = self.c.py2my(self.xcoor) y_p1 = self.c.py2my(self.ycoor[1:]) f_p = self.c.set_pyfunc(f) try: #1 ext_gridloop.gridloop1((1,2), x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #2 ext_gridloop.gridloop1(x_p, x_p, y_p1, f_p) except: print sys.exc_type, sys.exc_value try: #3 ext_gridloop.gridloop2(x_p, y_p, 'abc') except: print sys.exc_type, sys.exc_value
def ext_gridloop1(self, f): """Compute a[i,j] = f(xi,yj) in an external routine.""" # a is made here, sent to the routine, and then returned lx = size(self.xcoor); ly = size(self.ycoor) a = zeros((lx,ly), Float) try: # are we in Fortran? a = ext_gridloop.as_column_major_storage(a) except: pass if isinstance(f, str): f_compiled = compile(f, '<string>', 'eval') def f_str(x, y): return eval(f_compiled) #return eval(f) # much slower func = f_str else: func = f ext_gridloop.gridloop1(a, self.xcoor, self.ycoor, func) return a
def ext_gridloop_exceptions(self, f): """Test error handling in the extension module.""" try: #1 ext_gridloop.gridloop1((1,2), self.xcoor, self.ycoor[1:], f) except: print sys.exc_type, sys.exc_value try: #2 ext_gridloop.gridloop1(self.xcoor, self.xcoor, self.ycoor[1:], f) except: print sys.exc_type, sys.exc_value try: #3 ext_gridloop.gridloop2(self.xcoor, self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value try: #4 ext_gridloop.gridloop2(array(self.xcoor,Complex64), self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value try: #5 ext_gridloop.gridloop2(array([[0,0],[1,2]]), self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value
def ext_gridloop_exceptions(self, f): """Test error handling in the extension module.""" try: #1 ext_gridloop.gridloop1((1, 2), self.xcoor, self.ycoor[1:], f) except: print sys.exc_type, sys.exc_value try: #2 ext_gridloop.gridloop1(self.xcoor, self.xcoor, self.ycoor[1:], f) except: print sys.exc_type, sys.exc_value try: #3 ext_gridloop.gridloop2(self.xcoor, self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value try: #4 ext_gridloop.gridloop2(array(self.xcoor, Complex64), self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value try: #5 ext_gridloop.gridloop2(array([[0, 0], [1, 2]]), self.ycoor, 'abc') except: print sys.exc_type, sys.exc_value
c = ext_gridloop.Convert_MyArray() n = 3 a = zeros((n,n), Float) xcoor = sequence(0, 1, 0.5) ycoor = sequence(0, 1, 0.5) def f(x, y): print "in f(%g,%g)" % (x,y) return x+y+1 x_p = c.py2my(xcoor) y_p = c.py2my(ycoor) a_p = c.py2my(a) x_p = c.py2my(xcoor) y_p = c.py2my(ycoor) a_p = c.py2my_copy(a) f_p = c.set_pyfunc(f) print "calling gridloop1" ext_gridloop.gridloop1(a_p, x_p, y_p, f_p) b = c.my2py_copy(a_p) print "b=", b print "calling gridloop2" a2_p = ext_gridloop.gridloop2(x_p, y_p, f_p) print "converting C++ ptr to NumPy" a2 = c.my2py(a2_p) print "a2", a2
c = ext_gridloop.Convert_MyArray() n = 3 a = zeros((n,n)) xcoor = linspace(0, 1, 3) ycoor = xcoor.copy def f(x, y): print "in f(%g,%g)" % (x,y) return x+y+1 x_p = c.py2my(xcoor) y_p = c.py2my(ycoor) a_p = c.py2my(a) x_p = c.py2my(xcoor) y_p = c.py2my(ycoor) a_p = c.py2my_copy(a) f_p = c.set_pyfunc(f) print "calling gridloop1" ext_gridloop.gridloop1(a_p, x_p, y_p, f_p) b = c.my2py_copy(a_p) print "b=", b print "calling gridloop2" a2_p = ext_gridloop.gridloop2(x_p, y_p, f_p) print "converting C++ ptr to NumPy" a2 = c.my2py(a2_p) print "a2", a2