def setup(self): self.lit_int = pyf.Parameter("lit_int", dtype=pyf.default_integer, expr="30-10") self.sik = pyf.Parameter("sik_10", dtype=pyf.default_integer, expr="selected_int_kind(10)") self.srk = pyf.Parameter("srk_10_20", dtype=pyf.default_integer, expr="selected_real_kind(10, lit_int)") srk_real = pyf.RealType("srk_10_20", kind="srk_10_20") self.real_arg = pyf.Argument("real_arg", dtype=srk_real, intent='inout') sik_int = pyf.IntegerType("sik_10", kind="sik_10") self.int_arg = pyf.Argument("int_arg", dtype=sik_int, dimension=[("lit_int", )]) subr = pyf.Subroutine(name="subr", args=[self.real_arg, self.int_arg], params=[self.lit_int, self.sik, self.srk]) self.ret_arg = pyf.Argument("func", dtype=srk_real) func = pyf.Function(name="func", args=[self.real_arg, self.int_arg], params=[self.lit_int, self.sik, self.srk], return_arg=self.ret_arg) self.args = [self.real_arg, self.int_arg] self.params = [self.lit_int, self.sik, self.srk]
def test_function(self): return_arg = pyf.Argument('fort_function', dtype=pyf.default_integer) ffun = pyf.Function(name="fort_function", args=(), return_arg=return_arg) ok_(ffun.name == 'fort_function') ok_(ffun.return_arg.dtype is pyf.default_integer)
def test_logical_function(): return_arg = pyf.Argument('lgcl_fun', dtype=pyf.LogicalType(fw_ktp='lgcl')) lgcl_fun = pyf.Function(name='lgcl_fun', args=[], return_arg=return_arg) lgcl_fun_wrapped = fc_wrap.FunctionWrapper(wrapped=lgcl_fun) buf = CodeBuffer() lgcl_fun_wrapped.generate_wrapper(buf) fort_file = '''\ subroutine lgcl_fun_c(fw_ret_arg, fw_iserr__, fw_errstr__) bind(c, name="lgcl_fun_c") use fwrap_ktp_mod implicit none type(c_ptr), value :: fw_ret_arg integer(kind=fwi_integer_t), intent(out) :: fw_iserr__ character(kind=fw_character_t, len=1), dimension(fw_errstr_len) :: fw_errstr__ interface function lgcl_fun() use fwrap_ktp_mod implicit none logical(kind=fwl_lgcl_t) :: lgcl_fun end function lgcl_fun end interface logical(kind=fwl_lgcl_t), pointer :: fw_fw_ret_arg fw_iserr__ = FW_INIT_ERR__ call c_f_pointer(fw_ret_arg, fw_fw_ret_arg) fw_fw_ret_arg = lgcl_fun() fw_iserr__ = FW_NO_ERR__ end subroutine lgcl_fun_c ''' compare(fort_file, buf.getvalue())
def test_generate_fc_h(): return_arg = pyf.Argument(name="two_arg", dtype=pyf.default_real) two_arg_func = pyf.Function(name='two_arg', args=[ pyf.Argument(name='a', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='b', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='c', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='d', dtype=pyf.default_integer, intent='in'), ], return_arg=return_arg) ta_wrp = fc_wrap.FunctionWrapper(wrapped=two_arg_func) ast = [ta_wrp] buf = CodeBuffer() header_name = 'foobar' fc_wrap.generate_fc_h(ast, header_name, buf) code = '''\ #include "foobar" void two_arg_c(fwr_real_t *, fwi_integer_t *, fwi_integer_t *, fwi_integer_t *, fwi_integer_t *, fwi_integer_t *, fw_character_t *); ''' compare(buf.getvalue(), code)
def test_gen_empty_func_wrapper(): return_arg = pyf.Argument("empty_func", dtype=pyf.default_integer) empty_func = pyf.Function(name='empty_func', args=(), return_arg=return_arg) empty_func_wrapper = fc_wrap.FunctionWrapper(wrapped=empty_func) empty_func_wrapped = '''\ subroutine empty_func_c(fw_ret_arg, fw_iserr__, fw_errstr__) bind(c, name="empty_func_c") use fwrap_ktp_mod implicit none integer(kind=fwi_integer_t), intent(out) :: fw_ret_arg integer(kind=fwi_integer_t), intent(out) :: fw_iserr__ character(kind=fw_character_t, len=1), dimension(fw_errstr_len) :: fw_errstr__ interface function empty_func() use fwrap_ktp_mod implicit none integer(kind=fwi_integer_t) :: empty_func end function empty_func end interface fw_iserr__ = FW_INIT_ERR__ fw_ret_arg = empty_func() fw_iserr__ = FW_NO_ERR__ end subroutine empty_func_c ''' buf = CodeBuffer() empty_func_wrapper.generate_wrapper(buf) compare(empty_func_wrapped, buf.getvalue())
def test_parse(self): ast = fwrapper.parse(self.source_file_lst) return_arg = pyf.Argument('empty_func', dtype=pyf.default_integer) empty_func = pyf.Function(name='empty_func', args=(), return_arg=return_arg) eq_(ast[0].name, empty_func.name) eq_(ast[0].return_arg.name, empty_func.return_arg.name) eq_(len(ast[0].args), len(empty_func.args))
def test_func_return_array(): args = [ pyf.Argument(name='a', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='b', dtype=pyf.default_integer, intent='in') ] return_arg = pyf.Argument(name="arr_fun", dtype=pyf.default_real, dimension=('a', 'b')) arrfun = pyf.Function(name="arr_fun", args=args, return_arg=return_arg)
def test_c_prototype_empty(self): return_arg = pyf.Argument(name="empty_func", dtype=pyf.default_integer) empty_func = pyf.Function(name='empty_func', args=(), return_arg=return_arg) empty_func_wrapper = fc_wrap.FunctionWrapper(wrapped=empty_func) eq_(empty_func_wrapper.c_prototype(), ( 'void empty_func_c(fwi_integer_t *, fwi_integer_t *, fw_character_t *);' )) empty_subr = pyf.Subroutine(name='empty_subr', args=()) empty_subr_wrapper = fc_wrap.SubroutineWrapper(wrapped=empty_subr) eq_(empty_subr_wrapper.c_prototype(), 'void empty_subr_c(fwi_integer_t *, fw_character_t *);')
def test_c_proto_return_type(self): for dtype in (pyf.default_real, pyf.default_integer): return_arg = pyf.Argument(name='ret_arg', dtype=dtype, is_return_arg=True) empty_func = pyf.Function(name='foo', args=[], return_arg=return_arg) am = fc_wrap.ArgWrapperManager(empty_func) #XXX eq_(am.c_proto_return_type(), 'void') empty_subr = pyf.Subroutine(name='foo', args=[]) am_subr = fc_wrap.ArgWrapperManager(empty_subr) #XXX eq_(am_subr.c_proto_return_type(), 'void')
def test_c_proto_args(self): args = [ pyf.Argument(name='int_arg', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='real_arg', dtype=pyf.default_real, intent='out') ] return_arg = pyf.Argument(name='fname', dtype=pyf.default_real) func = pyf.Function('foo', args=args, return_arg=return_arg) arg_man = fc_wrap.ArgWrapperManager(func) eq_(arg_man.c_proto_args(), [ 'fwr_real_t *', 'fwi_integer_t *', 'fwr_real_t *', 'fwi_integer_t *', 'fw_character_t *' ])
def setup(self): int_arg_inout = pyf.Argument("int_arg_inout", pyf.default_integer, 'inout') int_arg = pyf.Argument("int_arg", pyf.default_integer) int_array = pyf.Argument("int_array", pyf.default_integer, intent="out", dimension=[':', ':']) return_arg = pyf.Argument(name="dstring_func", dtype=pyf.default_integer) func = pyf.Function(name="dstring_func", args=[int_arg_inout, int_arg, int_array], return_arg=return_arg) fcw = fc_wrap.FunctionWrapper(wrapped=func) self.cyw = cy_wrap.ProcWrapper(wrapped=fcw)
def setup(self): int_arg_in = pyf.Argument("int_arg_in", pyf.default_integer, 'in') int_arg_inout = pyf.Argument("int_arg_inout", pyf.default_integer, 'inout') int_arg_out = pyf.Argument("int_arg_out", pyf.default_integer, 'out') real_arg = pyf.Argument("real_arg", pyf.default_real) all_args = [int_arg_in, int_arg_inout, int_arg_out, real_arg] return_arg = pyf.Argument(name="fort_func", dtype=pyf.default_integer) pyf_func = pyf.Function(name="fort_func", args=all_args, return_arg=return_arg) func_wrapper = fc_wrap.FunctionWrapper(wrapped=pyf_func) self.cy_func_wrapper = cy_wrap.ProcWrapper(wrapped=func_wrapper) pyf_subr = pyf.Subroutine(name="fort_subr", args=all_args) subr_wrapper = fc_wrap.SubroutineWrapper(wrapped=pyf_subr) self.cy_subr_wrapper = cy_wrap.ProcWrapper(wrapped=subr_wrapper)
def test_c_prototype_args(self): args = [ pyf.Argument(name='int_arg', dtype=pyf.default_integer, intent='in'), pyf.Argument(name='array', dtype=pyf.default_real, dimension=(':', ) * 3, intent='out') ] return_arg = pyf.Argument(name="func", dtype=pyf.default_integer) func = pyf.Function(name='func', args=args, return_arg=return_arg) func_wrapper = fc_wrap.FunctionWrapper(wrapped=func) eq_( func_wrapper.c_prototype(), "void func_c" "(fwi_integer_t *, " "fwi_integer_t *, " "fwi_npy_intp_t *, " "fwi_npy_intp_t *, " "fwi_npy_intp_t *, " "fwr_real_t *, " "fwi_integer_t *, " "fw_character_t *);")
def generate_ast(fsrcs): ast = [] for src in fsrcs: block = api.parse(src, analyze=True) tree = block.content for proc in tree: if not is_proc(proc): # we ignore non-top-level procedures until modules are supported. continue args = _get_args(proc) params = _get_params(proc) if proc.blocktype == 'subroutine': ast.append( pyf.Subroutine(name=proc.name, args=args, params=params)) elif proc.blocktype == 'function': ast.append( pyf.Function(name=proc.name, args=args, params=params, return_arg=_get_ret_arg(proc))) return ast
def test_function_args(self): name = 'ffun' return_arg = pyf.Argument(name, dtype=pyf.default_integer) pyf.Function(name=name, args=(pyf.Argument('a', dtype=pyf.default_integer), ), return_arg=return_arg)
def test_gen_iface(): def gen_iface_gen(ast, istr): buf = CodeBuffer() #ast.generate_interface(buf) fc_wrap.generate_interface(ast, buf) compare(istr, buf.getvalue()) args = [ pyf.Argument(name='arg1', dtype=pyf.ComplexType('sik_10_20'), intent='in'), pyf.Argument(name='arg2', dtype=pyf.RealType('double_precision'), intent='inout'), pyf.Argument(name='arg3', dtype=pyf.IntegerType('int_x_8'), intent='out') ] many_arg_subr = pyf.Subroutine(name='many_arg_subr', args=args) many_arg_subr_iface = '''\ interface subroutine many_arg_subr(arg1, arg2, arg3) use fwrap_ktp_mod implicit none complex(kind=fwc_sik_10_20_t), intent(in) :: arg1 real(kind=fwr_double_precision_t), intent(inout) :: arg2 integer(kind=fwi_int_x_8_t), intent(out) :: arg3 end subroutine many_arg_subr end interface ''' return_arg = pyf.Argument(name="one_arg_func", dtype=pyf.default_integer) one_arg_func = pyf.Function(name='one_arg_func', args=[ pyf.Argument(name='arg1', dtype=pyf.default_real, intent='inout') ], return_arg=return_arg) one_arg_func_iface = '''\ interface function one_arg_func(arg1) use fwrap_ktp_mod implicit none real(kind=fwr_real_t), intent(inout) :: arg1 integer(kind=fwi_integer_t) :: one_arg_func end function one_arg_func end interface ''' return_arg = pyf.Argument(name="one_arg_func", dtype=pyf.default_integer) empty_func = pyf.Function(name='empty_func', args=(), return_arg=return_arg) empty_func_iface = '''\ interface function empty_func() use fwrap_ktp_mod implicit none integer(kind=fwi_integer_t) :: empty_func end function empty_func end interface ''' data = [(many_arg_subr, many_arg_subr_iface), (one_arg_func, one_arg_func_iface), (empty_func, empty_func_iface)] for ast, iface in data: yield gen_iface_gen, ast, iface