def test_cmemsubsets_copy(): for data in DATA_CMEMSUBSETS: kwds = data['kwds'] for case in data['cases']: cmss = CMemSubSets(**kwds) copy = cmss.copy() ok_(cmss is not copy) yield (check_cmemsubsets, copy, case)
def test_cmemsubsets(): for data in DATA_CMEMSUBSETS: kwds = data['kwds'] for case in data['cases']: yield (check_cmemsubsets, CMemSubSets(**kwds), case)
def test_default(): for data in DATA_CMEMSUBSETS: kwds = data['kwds'] if 'data' in kwds: # no test for empty `kwds` yield (check_default, CMemSubSets(**kwds), kwds)
def __new__(cls, clsname, bases, attrs): clibdir = attr_from_attrs_or_bases(bases, attrs, '_clibdir_') clibname = attr_from_attrs_or_bases(bases, attrs, '_clibname_') cmembers = attr_from_attrs_or_bases(bases, attrs, '_cmembers_') cfuncs = attr_from_attrs_or_bases(bases, attrs, '_cfuncs_') if any(c is None for c in [clibdir, clibname, cmembers, cfuncs]): # Required attributes are not set. It is not possible to # setup C wrappers. So, do not process anything at this # point. This class is like abstract base class. return type.__new__(cls, clsname, bases, attrs) mandatory_attrs = ['_clibdir_', '_clibname_', '_cmembers_', '_cfuncs_'] if all(name not in attrs for name in mandatory_attrs): # All required attributes already exist in base classes. # Therefore, C wrappers are already ready. There is # nothing to do other than the normal inheritance. return type.__new__(cls, clsname, bases, attrs) cstructname = attr_from_attrs_or_bases( bases, attrs, '_cstructname_', clsname) cfuncprefix = attr_from_attrs_or_bases( bases, attrs, '_cfuncprefix_', cstructname + CJOINSTR) cmemsubsets = attr_from_attrs_or_bases(bases, attrs, '_cmemsubsets_') ## parse _cfuncs_ cfuncs_parsed = parse_cfuncs(cfuncs) ## parse _cmembers_ (cmems_parsed, cmems_parsed_list, idxset) = parse_cmembers(cmembers) (cmems_default_scalar, cmems_default_array) = default_of_cmembers(cmems_parsed_list) attrs.update( _cmems_parsed_=cmems_parsed, _cmems_default_scalar_=cmems_default_scalar, _cmems_default_array_=cmems_default_array, _idxset_=idxset, array_alias=array_alias_from_cmems_parsed(cmems_parsed), cinfo=CInfo(cmems_parsed_list, idxset), ) # FIXME: most of code in MetaSimObject.__new__ and top level # functions of this module must go into CInfo. ## set _struct_type_ and _struct_type_p_ StructClass = get_struct_class(cmems_parsed_list, cstructname) struct_type_p = POINTER(StructClass) attrs.update(_struct_type_=StructClass, _struct_type_p_=struct_type_p) ## set getter/setter for (vname, parsed) in cmems_parsed.iteritems(): if parsed.valtype == 'array': attrs[vname] = _gene_prop_array(vname) elif parsed.valtype == 'scalar': attrs[vname] = _gene_prop_scalar(vname) elif parsed.valtype == 'object': attrs[vname] = _gene_prop_object(vname) else: ValueError('valtype "%s" is not recognized' % parsed.valtype) ## load c-functions cdll = numpy.ctypeslib.load_library(clibname, clibdir) cfunc_loaded = load_cfunc(cdll, cfuncs_parsed, struct_type_p, cfuncprefix, idxset) attrs.update( _cdll_=cdll, _cfunc_loaded_=cfunc_loaded, _cmemsubsets_parsed_=CMemSubSets( cmemsubsets, set(cfunc_loaded), set(cmems_parsed)), ) funcattrs = {} for (fname, parsed) in cfuncs_parsed.iteritems(): funcattrs[fname] = gene_cfpywrap(attrs, parsed) cbase = type("DummyCBase", (object,), funcattrs) bases = bases + (cbase,) return type.__new__(cls, clsname, bases, attrs)
def __new__(cls, clsname, bases, attrs): normal = super(MetaSimObject, cls).__new__(cls, clsname, bases, attrs) try: clibdir = normal._clibdir_ clibname = normal._clibname_ cmembers = normal._cmembers_ cfuncs = normal._cfuncs_ except AttributeError: # Required attributes are not set. It is not possible to # setup C wrappers. So, do not process anything at this # point. This class is like abstract base class. return normal mandatory_attrs = ['_clibdir_', '_clibname_', '_cmembers_', '_cfuncs_'] if (all(name not in attrs for name in mandatory_attrs) and hasattr(normal, 'cinfo')): # All required attributes already exist in base classes. # Therefore, C wrappers are already ready. There is # nothing to do other than the normal inheritance. return normal # Otherwise, (1) at least one of the mandatory attribute is # *newly* specified or (2) the following code is not executed # against one of the super classes. # FIXME: Some C functions in DummyCBase may not be used for # the case (1). Setting _cfunc_loaded_ could be # enough, unless new functions are added in the current # `_cfuncs_`. cstructname = getattr(normal, '_cstructname_', clsname) cfuncprefix = getattr(normal, '_cfuncprefix_', cstructname + CJOINSTR) cmemsubsets = getattr(normal, '_cmemsubsets_', None) ## parse _cfuncs_ cfuncs_parsed = parse_cfuncs(cfuncs) ## parse _cmembers_ (cmems_parsed, cmems_parsed_list, idxset) = parse_cmembers(cmembers) (cmems_default_scalar, cmems_default_array) = default_of_cmembers(cmems_parsed_list) attrs.update( _cmems_parsed_=cmems_parsed, _cmems_default_scalar_=cmems_default_scalar, _cmems_default_array_=cmems_default_array, _idxset_=idxset, array_alias=array_alias_from_cmems_parsed(cmems_parsed), cinfo=CInfo(cmems_parsed_list, idxset), ) # FIXME: most of code in MetaSimObject.__new__ and top level # functions of this module must go into CInfo. ## set _struct_type_ and _struct_type_p_ StructClass = get_struct_class(cmems_parsed_list, cstructname) struct_type_p = POINTER(StructClass) attrs.update(_struct_type_=StructClass, _struct_type_p_=struct_type_p) ## set getter/setter for (vname, parsed) in cmems_parsed.items(): if parsed.valtype == 'array': attrs[vname] = _gene_prop_array(vname) elif parsed.valtype == 'scalar': attrs[vname] = _gene_prop_scalar(vname) elif parsed.valtype == 'object': attrs[vname] = _gene_prop_object(vname) else: ValueError('valtype "%s" is not recognized' % parsed.valtype) ## load c-functions cdll = numpy.ctypeslib.load_library(clibname, clibdir) cfunc_loaded = load_cfunc(cdll, cfuncs_parsed, struct_type_p, cfuncprefix, idxset) attrs.update( _cdll_=cdll, _cfunc_loaded_=cfunc_loaded, _cmemsubsets_parsed_=CMemSubSets( cmemsubsets, set(cfunc_loaded), set(cmems_parsed)), ) funcattrs = {} for (fname, parsed) in cfuncs_parsed.items(): funcattrs[fname] = gene_cfpywrap(attrs, parsed) cbase = type("DummyCBase", (object,), funcattrs) bases = bases + (cbase,) return super(MetaSimObject, cls).__new__(cls, clsname, bases, attrs)