def is_related(t1, t2): """Check whether two types\\classes t1 and t2 could introduce the problem""" if declarations.is_pointer(t1) and declarations.is_pointer(t2): return registration_order.is_related( declarations.remove_pointer(t1), declarations.remove_pointer(t2)) elif declarations.is_pointer(t1) and not declarations.is_pointer(t2): t1 = declarations.remove_cv(declarations.remove_pointer(t1)) t2 = declarations.remove_cv(t2) if declarations.is_same(t1, t2): return 1 elif not declarations.is_pointer(t1) and declarations.is_pointer(t2): t1 = declarations.remove_cv(t1) t2 = declarations.remove_cv(declarations.remove_pointer(t2)) if declarations.is_same(t1, t2): return -1 else: #not is_pointer( t1 ) and not is_pointer( t2 ): if declarations.is_integral( t1 ) and not declarations.is_bool( t1 ) \ and declarations.is_bool( t2 ): return -1 elif declarations.is_bool( t1 ) \ and declarations.is_integral( t2 ) and not declarations.is_bool( t2 ): return 1 else: pass return None
def is_related( t1, t2 ): """Check whether two types\\classes t1 and t2 could introduce the problem""" if declarations.is_pointer( t1 ) and declarations.is_pointer( t2 ): return registration_order.is_related( declarations.remove_pointer( t1 ) , declarations.remove_pointer( t2 ) ) elif declarations.is_pointer( t1 ) and not declarations.is_pointer( t2 ): t1 = declarations.remove_cv( declarations.remove_pointer( t1 ) ) t2 = declarations.remove_cv( t2 ) if declarations.is_same( t1, t2 ): return 1 elif not declarations.is_pointer( t1 ) and declarations.is_pointer( t2 ): t1 = declarations.remove_cv( t1 ) t2 = declarations.remove_cv( declarations.remove_pointer( t2 ) ) if declarations.is_same( t1, t2 ): return -1 else: #not is_pointer( t1 ) and not is_pointer( t2 ): if declarations.is_integral( t1 ) and not declarations.is_bool( t1 ) \ and declarations.is_bool( t2 ): return -1 elif declarations.is_bool( t1 ) \ and declarations.is_integral( t2 ) and not declarations.is_bool( t2 ): return 1 else: pass return None
def keywords_args(self): if not self.__args: return '' boost_arg = self.__id_creator('::boost::python::arg') boost_obj = self.__id_creator('::boost::python::object') result = ['( '] for arg in self.__args: if 1 < len(result): result.append(self.PARAM_SEPARATOR) result.append(boost_arg) result.append('("%s")' % arg.name) if self.__decl.use_default_arguments and arg.default_value: if not declarations.is_pointer( arg.type) or arg.default_value != '0': arg_type_no_alias = declarations.remove_alias(arg.type) if declarations.is_fundamental( arg_type_no_alias ) \ and declarations.is_integral( arg_type_no_alias ) \ and not arg.default_value.startswith( arg_type_no_alias.decl_string ): result.append( '=(%s)(%s)' % (arg.type.partial_decl_string, arg.default_value)) elif self.__should_use_enum_wa(arg): #Work around for bug/missing functionality in boost.python. #registration order result.append('=(long)(%s)' % arg.default_value) else: result.append('=%s' % arg.default_value) else: result.append('=%s()' % boost_obj) result.append(' )') return ''.join(result)
def __init__(self, function, buffer_arg_ref, size_arg_ref): """Constructor. :param buffer_arg_ref: "reference" to the buffer argument :param buffer_arg_ref: "reference" to argument, which holds buffer size """ transformer.transformer_t.__init__(self, function) self.buffer_arg = self.get_argument(buffer_arg_ref) self.buffer_arg_index = self.function.arguments.index(self.buffer_arg) self.size_arg = self.get_argument(size_arg_ref) self.size_arg_index = self.function.arguments.index(self.size_arg) if not is_ptr_or_array(self.buffer_arg.type): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "buffer" argument %s type must be a array or a pointer (got %s).' ) % (function, self.buffer_arg.name, self.buffer_arg.type) if not declarations.is_integral(self.size_arg.type): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "size" argument %s type must be an integral type (got %s).' ) % (function, self.size_arg.name, self.size_arg.type) self.buffer_item_type = declarations.remove_const(declarations.array_item_type(self.buffer_arg.type))
def test(self): f = self.global_ns.free_function('pygccxml_bug') t = f.arguments[0].decl_type self.assertTrue(isinstance(t, declarations.pointer_t)) self.assertTrue(isinstance(t.base, declarations.volatile_t)) self.assertTrue(isinstance(t.base.base, declarations.const_t)) self.assertTrue(declarations.is_integral(t.base.base.base))
def test(self): f = self.global_ns.free_fun('pygccxml_bug') t = f.arguments[0].type self.failUnless(isinstance(t, declarations.pointer_t)) self.failUnless(isinstance(t.base, declarations.volatile_t)) self.failUnless(isinstance(t.base.base, declarations.const_t)) self.failUnless(declarations.is_integral(t.base.base.base))
def keywords_args(self): if not self.__args: return "" boost_arg = self.__id_creator("::boost::python::arg") boost_obj = self.__id_creator("::boost::python::object") result = ["( "] for arg in self.__args: if 1 < len(result): result.append(self.PARAM_SEPARATOR) result.append(boost_arg) result.append('("%s")' % arg.name) if self.__decl.use_default_arguments and arg.default_value: if not declarations.is_pointer(arg.type) or arg.default_value != "0": arg_type_no_alias = declarations.remove_alias(arg.type) if ( declarations.is_fundamental(arg_type_no_alias) and declarations.is_integral(arg_type_no_alias) and not arg.default_value.startswith(arg_type_no_alias.decl_string) ): result.append("=(%s)(%s)" % (arg_type_no_alias.partial_decl_string, arg.default_value)) elif self.__should_use_enum_wa(arg): # Work around for bug/missing functionality in boost.python. # registration order result.append("=(long)(%s)" % arg.default_value) else: result.append("=%s" % arg.default_value) else: result.append("=%s()" % boost_obj) result.append(" )") return "".join(result)
def __is_invalid_integral(self, func, arg): type_ = declarations.remove_reference(declarations.remove_cv(arg.type)) if not declarations.is_integral(type_): return False try: int(arg.default_value) return False except: return True
def __is_invalid_integral(self, func, arg): type_ = declarations.remove_reference( declarations.remove_cv( arg.type ) ) if not declarations.is_integral( type_ ): return False try: int( arg.default_value ) return False except: return True
def _convert_opaque_to_token(x): """ convert either a string from re or a value from pygccxml to a struct token """ if type(x) is str: return { "BOOL": "?", "UINT8_T": "B", "INT8_T": "b", "UINT16_T": "H", "INT16_T": "h", "UINT32_T": "I", "INT32_T": "i", "UINT64_T": "Q", "INT64_T": "q", "FLOAT": "f", "DOUBLE": "d" }[x.upper()] elif isinstance(x, declarations.declarated_t) and not declarations.is_enum(x): return _convert_opaque_to_token(declarations.remove_alias(x)) elif declarations.is_integral(x) or declarations.is_enum(x): basetoken = {1: "b", 2: "h", 4: "i", 8: "q"}[int(x.byte_size)] try: if "unsigned" in x.CPPNAME: basetoken = basetoken.upper() if "bool" in x.CPPNAME: basetoken = '?' except AttributeError: pass return basetoken elif declarations.is_floating_point(x): if isinstance(x, declarations.float_t): return "f" else: return "d" elif declarations.is_array(x): basetoken = _convert_opaque_to_token(declarations.array_item_type(x)) return basetoken * x.size else: raise ValueError("unknown instance: " + repr(x))
def _handle_struct(entryname, struct_name): struct_info = slots_namespace.class_(struct_name) new_type_members = [] for i in struct_info.variables(): bitfield = None if declarations.is_integral(i.decl_type): try: enumerator = struct_info.enumerations()[0] bitfield = enumerator.get_name2value_dict() except RuntimeError: bitfield = None new_type_members.append( DeclaredMember(i.name, _convert_opaque_to_token(i.decl_type), bitfield)) slot_types[entryname][1] = SlotType(new_type_members)
def _create_member_list(struct_info): new_type_members = [] for i in struct_info.variables(): if i.type_qualifiers.has_static: continue bitfield = None enumer = None if declarations.is_integral(i.decl_type): try: for enumerator in struct_info.enumerations(): if enumerator.byte_size != declarations.remove_alias( i.decl_type).byte_size: continue elif enumerator.name != snake_to_camel(i.name): continue else: bitfield = enumerator.get_name2value_dict() break except RuntimeError: bitfield = None elif declarations.is_enum(i.decl_type): try: enumer = global_namespace.enumeration( i.decl_type.decl_string).get_name2value_dict() except RuntimeError: enumer = {} if declarations.is_class(i.decl_type): new_type_members.append( DeclaredMemberStruct( i.name, _create_member_list(i.decl_type.declaration))) else: new_type_members.append( DeclaredMember(i.name, _convert_opaque_to_token(i.decl_type), bitfield=bitfield, enumer=enumer)) return new_type_members
def __init__(self, function, buffer_arg_ref, size_arg_ref): """Constructor. :param buffer_arg_ref: "reference" to the buffer argument :param buffer_arg_ref: "reference" to argument, which holds buffer size """ transformer.transformer_t.__init__( self, function ) self.buffer_arg = self.get_argument( buffer_arg_ref ) self.buffer_arg_index = self.function.arguments.index( self.buffer_arg ) self.size_arg = self.get_argument( size_arg_ref ) self.size_arg_index = self.function.arguments.index( self.size_arg ) if not is_ptr_or_array( self.buffer_arg.type ): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "buffer" argument %s type must be a array or a pointer (got %s).' ) \ % ( function, self.buffer_arg.name, self.buffer_arg.type) if not declarations.is_integral( self.size_arg.type ): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "size" argument %s type must be an integral type (got %s).' ) \ % ( function, self.size_arg.name, self.size_arg.type) self.buffer_item_type = declarations.remove_const( declarations.array_item_type( self.buffer_arg.type ) )
def _create_member_list(struct_info): new_type_members = [] for i in struct_info.variables(): bitfield = None if declarations.is_integral(i.decl_type): try: enumerator = struct_info.enumerations()[0] if enumerator.byte_size != i.decl_type.byte_size: bitfield = None else: bitfield = enumerator.get_name2value_dict() except RuntimeError: bitfield = None if declarations.is_class(i.decl_type): new_type_members.append( DeclaredMemberStruct( i.name, _create_member_list(i.decl_type.declaration))) else: new_type_members.append( DeclaredMember(i.name, _convert_opaque_to_token(i.decl_type), bitfield)) return new_type_members
def prepare_special_cases(): """ Creates a map of special cases ( aliases ) for casting operator. """ special_cases = {} const_t = declarations.const_t pointer_t = declarations.pointer_t for type_ in declarations.FUNDAMENTAL_TYPES.values(): alias = None if declarations.is_same(type_, declarations.bool_t()): alias = "__int__" elif declarations.is_integral(type_): if "long" in type_.decl_string: alias = "__long__" else: alias = "__int__" elif declarations.is_floating_point(type_): alias = "__float__" else: continue # void if alias: special_cases[type_] = alias special_cases[const_t(type_)] = alias special_cases[pointer_t(const_t(declarations.char_t()))] = "__str__" std_string = "::std::basic_string<char,std::char_traits<char>,std::allocator<char> >" std_wstring1 = "::std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >" std_wstring2 = "::std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >" special_cases[std_string] = "__str__" special_cases[std_wstring1] = "__str__" special_cases[std_wstring2] = "__str__" special_cases["::std::string"] = "__str__" special_cases["::std::wstring"] = "__str__" # TODO: add # std::complex<SomeType> some type should be converted to double return special_cases
def prepare_special_cases(): """ Creates a map of special cases ( aliases ) for casting operator. """ special_cases = {} const_t = declarations.const_t pointer_t = declarations.pointer_t for type_ in declarations.FUNDAMENTAL_TYPES.values(): alias = None if declarations.is_same( type_, declarations.bool_t() ): alias = '__int__' elif declarations.is_integral( type_ ): if 'long' in type_.decl_string: alias = '__long__' else: alias = '__int__' elif declarations.is_floating_point( type_ ): alias = '__float__' else: continue #void if alias: special_cases[ type_ ] = alias special_cases[ const_t( type_ ) ] = alias special_cases[ pointer_t( const_t( declarations.char_t() ) ) ] = '__str__' std_string = '::std::basic_string<char,std::char_traits<char>,std::allocator<char> >' std_wstring1 = '::std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> >' std_wstring2 = '::std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >' special_cases[ std_string ] = '__str__' special_cases[ std_wstring1 ] = '__str__' special_cases[ std_wstring2 ] = '__str__' special_cases[ '::std::string' ] = '__str__' special_cases[ '::std::wstring' ] = '__str__' #TODO: add # std::complex<SomeType> some type should be converted to double return special_cases
def beautify_func_list(func_list): func_list = [f for f in func_list if not f.ignore] # fix default values # don't remove std::vector default values, old compilers _need_ std::allocator removed for f in func_list: for arg in f.arguments: if isinstance(arg.default_value, str): repl_list = { 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': 'std::string', 'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >': 'std::vector<cv::Point>', 'std::vector<cv::Scalar_<double>, std::allocator<cv::Scalar_<double> > >': 'std::vector<cv::Scalar>', 'std::vector<int, std::allocator<int> >': 'std::vector<int>', 'std::vector<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >': 'std::vector<cv::Vec4i>', } for z in repl_list: arg.default_value = arg.default_value.replace( z, repl_list[z]) # one-to-one function argument for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue pds = _c.unique_pds(arg.type.partial_decl_string) if pds in _c.c2cpp: f._transformer_creators.append( _FT.input_as_FixType(pds, _c.c2cpp[pds], arg.name)) elif pds in [ 'CvRNG *', 'CvRNG &', 'CvRNG cosnt *', 'CvRNG const &' ]: f._transformer_creators.append(_FT.input_asRNG(arg.name)) elif pds in ['CvFileStorage *', 'CvFileStorage const *']: f._transformer_creators.append( _FT.input_as_FileStorage(arg.name)) elif pds in ['CvFileNode *', 'CvFileNode const *']: f._transformer_creators.append(_FT.input_as_FileNode(arg.name)) elif pds in ['CvMemStorage *', 'CvMemStorage const *']: f._transformer_creators.append( _FT.input_as_MemStorage(arg.name)) elif pds in [ 'CvSparseMat *', 'CvSparseMat &', 'CvSparseMat const *', 'CvSparseMat const &' ]: f._transformer_creators.append(_FT.input_asSparseMat(arg.name)) elif pds in [ "IplImage *", "IplImage const *", "CvArr *", "CvArr const *", "CvMat *", "CvMat const *", "cv::Range const *" ]: f._transformer_creators.append(_FT.input_as_Mat(arg.name)) # function argument int *sizes and int dims for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'sizes' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == 'dims' and _D.is_integral(arg2.type): f._transformer_creators.append( _FT.input_array1d('sizes', 'dims')) break if arg.name == '_sizes' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == '_ndims' and _D.is_integral(arg2.type): f._transformer_creators.append( _FT.input_array1d('_sizes', '_ndims')) break if arg2.name == 'dims' and _D.is_integral(arg2.type): f._transformer_creators.append( _FT.input_array1d('_sizes', 'dims')) break if arg.name == '_newsz' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == '_newndims' and _D.is_integral(arg2.type): f._transformer_creators.append( _FT.input_array1d('_newsz', '_newndims')) break # function argument const CvPoint2D32f * src and const CvPoint2D32f * dst for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'src' and _D.is_pointer( arg.type) and 'CvPoint2D32f' in arg.type.decl_string: for arg2 in f.arguments: if arg2.name == 'dst' and _D.is_pointer( arg2.type ) and 'CvPoint2D32f' in arg2.type.decl_string: f._transformer_creators.append( _FT.input_array1d('src')) f._transformer_creators.append( _FT.input_array1d('dst')) break # argument 'void *data' for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'data' and _D.is_void_pointer(arg.type): f._transformer_creators.append(_FT.input_string(arg.name)) self.add_doc(f.name, "'data' is represented by a string") # final step: apply all the function transformations for f in func_list: if len(f._transformer_creators) > 0: sort_transformers(f) f.add_transformation(*f._transformer_creators, **f._transformer_kwds) if 'unique_function_name' in f._transformer_kwds: f.transformations[0].unique_name = f._transformer_kwds[ 'unique_function_name'] else: s = f.transformations[0].unique_name repl_dict = { 'operator()': '__call__', } for t in repl_dict: if t in s: s = s.replace(t, repl_dict[t]) f.transformations[0].unique_name = s f.transformations[0].alias = repl_dict[t] break _c.add_decl_desc(f)
def beautify_func_list(func_list): func_list = [f for f in func_list if not f.ignore] # fix default values # don't remove std::vector default values, old compilers _need_ std::allocator removed for f in func_list: for arg in f.arguments: if isinstance(arg.default_value, str): repl_list = { 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >': 'std::string', 'std::vector<cv::Point_<int>, std::allocator<cv::Point_<int> > >': 'std::vector<cv::Point>', 'std::vector<cv::Scalar_<double>, std::allocator<cv::Scalar_<double> > >': 'std::vector<cv::Scalar>', 'std::vector<int, std::allocator<int> >': 'std::vector<int>', 'std::vector<cv::Vec<int, 4>, std::allocator<cv::Vec<int, 4> > >': 'std::vector<cv::Vec4i>', } for z in repl_list: arg.default_value = arg.default_value.replace(z, repl_list[z]) # one-to-one function argument for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue pds = _c.unique_pds(arg.type.partial_decl_string) if pds in _c.c2cpp: f._transformer_creators.append(_FT.input_as_FixType(pds, _c.c2cpp[pds], arg.name)) elif pds in ['CvRNG *', 'CvRNG &', 'CvRNG cosnt *', 'CvRNG const &']: f._transformer_creators.append(_FT.input_asRNG(arg.name)) elif pds in ['CvFileStorage *', 'CvFileStorage const *']: f._transformer_creators.append(_FT.input_as_FileStorage(arg.name)) elif pds in ['CvFileNode *', 'CvFileNode const *']: f._transformer_creators.append(_FT.input_as_FileNode(arg.name)) elif pds in ['CvMemStorage *', 'CvMemStorage const *']: f._transformer_creators.append(_FT.input_as_MemStorage(arg.name)) elif pds in ['CvSparseMat *', 'CvSparseMat &', 'CvSparseMat const *', 'CvSparseMat const &']: f._transformer_creators.append(_FT.input_asSparseMat(arg.name)) elif pds in ["IplImage *", "IplImage const *", "CvArr *", "CvArr const *", "CvMat *", "CvMat const *", "cv::Range const *"]: f._transformer_creators.append(_FT.input_as_Mat(arg.name)) # function argument int *sizes and int dims for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'sizes' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == 'dims' and _D.is_integral(arg2.type): f._transformer_creators.append(_FT.input_array1d('sizes', 'dims')) break if arg.name == '_sizes' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == '_ndims' and _D.is_integral(arg2.type): f._transformer_creators.append(_FT.input_array1d('_sizes', '_ndims')) break if arg2.name == 'dims' and _D.is_integral(arg2.type): f._transformer_creators.append(_FT.input_array1d('_sizes', 'dims')) break if arg.name == '_newsz' and _D.is_pointer(arg.type): for arg2 in f.arguments: if arg2.name == '_newndims' and _D.is_integral(arg2.type): f._transformer_creators.append(_FT.input_array1d('_newsz', '_newndims')) break # function argument const CvPoint2D32f * src and const CvPoint2D32f * dst for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'src' and _D.is_pointer(arg.type) and 'CvPoint2D32f' in arg.type.decl_string: for arg2 in f.arguments: if arg2.name == 'dst' and _D.is_pointer(arg2.type) and 'CvPoint2D32f' in arg2.type.decl_string: f._transformer_creators.append(_FT.input_array1d('src')) f._transformer_creators.append(_FT.input_array1d('dst')) break # argument 'void *data' for f in func_list: for arg in f.arguments: if is_arg_touched(f, arg.name): continue if arg.name == 'data' and _D.is_void_pointer(arg.type): f._transformer_creators.append(_FT.input_string(arg.name)) self.add_doc(f.name, "'data' is represented by a string") # final step: apply all the function transformations for f in func_list: if len(f._transformer_creators) > 0: sort_transformers(f) f.add_transformation(*f._transformer_creators, **f._transformer_kwds) if 'unique_function_name' in f._transformer_kwds: f.transformations[0].unique_name = f._transformer_kwds['unique_function_name'] else: s = f.transformations[0].unique_name repl_dict = { 'operator()': '__call__', } for t in repl_dict: if t in s: s = s.replace(t, repl_dict[t]) f.transformations[0].unique_name = s f.transformations[0].alias = repl_dict[t] break _c.add_decl_desc(f)
def __is_invalid_integral(self, func, arg): type_ = declarations.remove_reference( declarations.remove_cv( arg.type ) ) return declarations.is_integral( type_ )