def is_supported(oper): """returns True if Boost.Python support the operator""" if oper.symbol == "*" and len(oper.arguments) == 0: # dereference does not make sense return False if oper.symbol != "<<": return oper.symbol in operators_helper.all args_len = len(oper.arguments) if isinstance(oper, declarations.member_operator_t): # and args_len != 1: return False # Boost.Python does not support member operator<< :-( if isinstance(oper, declarations.free_operator_t) and args_len != 2: return False if not declarations.is_same(oper.return_type, oper.arguments[0].type): return False type_ = oper.return_type if not declarations.is_reference(type_): return False type_ = declarations.remove_reference(type_) if declarations.is_const(type_): return False if args_len == 2: # second argument should has "T const &" type, otherwise the code will not compile tmp = oper.arguments[1].type if not declarations.is_reference(tmp): return False tmp = declarations.remove_reference(tmp) if not declarations.is_const(tmp): return False return declarations.is_std_ostream(type_) or declarations.is_std_wostream(type_)
def _get_exported_var_type( self ): type_ = declarations.remove_reference( self.declaration.type ) type_ = declarations.remove_const( type_ ) if python_traits.is_immutable( type_ ): return type_ else: return self.declaration.type
def _update_containers_db( self, type_ ): #will return True is type was treated type_ = declarations.remove_alias( type_ ) type_ = declarations.remove_pointer( type_ ) type_ = declarations.remove_reference( type_ ) type_ = declarations.remove_cv( type_ ) type_ = declarations.remove_declarated( type_ ) class_traits = declarations.class_traits class_declaration_traits = declarations.class_declaration_traits if not class_traits.is_my_case( type_ ) and not class_declaration_traits.is_my_case( type_ ): return False if class_traits.is_my_case( type_ ): container_cls = class_traits.get_declaration( type_ ) else: container_cls = class_declaration_traits.get_declaration( type_ ) if None is container_cls.indexing_suite: return False try: #check extraction of element type from container container_cls.indexing_suite.element_type except RuntimeError: decls_logger = _logging_.loggers.declarations if not messages.filter_disabled_msgs([messages.W1042], container_cls.disabled_messages ): return #user disabled property warning decls_logger.warn( "%s;%s" % ( container_cls, messages.W1042 ) ) self.__containers.add( container_cls ) return True
def call_traits( type_ ): """http://boost.org/libs/utility/call_traits.htm""" type_ = declarations.remove_alias( type_ ) if is_immutable( type_ ): return "%(arg)s" #pass by value elif declarations.is_reference( type_ ): no_ref = declarations.remove_reference( type_ ) if is_immutable( no_ref ): return "%(arg)s" #pass by value else: return "boost::ref(%(arg)s)" #pass by ref elif declarations.is_pointer( type_ ) \ and not is_immutable( type_.base ) \ and not declarations.is_pointer( type_.base ): base = type_.base while hasattr(base, 'base'): base = base.base if hasattr(base.declaration, 'custom_call_trait'): custom_call_trait = base.declaration.custom_call_trait call_trait = custom_call_trait(type_) if custom_call_trait else None if call_trait: return call_trait return "boost::python::ptr(%(arg)s)" #pass by ptr else: return "%(arg)s" #pass by value
def remove_ref_or_ptr( type_ ): if declarations.is_pointer( type_ ): return declarations.remove_pointer( type_ ) elif declarations.is_reference( type_ ): return declarations.remove_reference( type_ ) else: raise TypeError( 'Type should be reference or pointer, got %s.' % type_ )
def suspicious_type(type_): if not declarations.is_reference(type_): return False type_no_ref = declarations.remove_reference(type_) return not declarations.is_const(type_no_ref) and ( declarations.is_fundamental(type_no_ref) or declarations.is_enum(type_no_ref) )
def _exportable_impl(self): if self.transformations: # It is possible that the function asked for the user attention. # The user paid attention and created a transformation. # Py++ should be silent in this case. return "" if not self.parent.name: return messages.W1057 % str(self) all_types = [arg.type for arg in self.arguments] all_types.append(self.return_type) for some_type in all_types: if isinstance(some_type, declarations.ellipsis_t): return messages.W1053 % str(self) units = declarations.decompose_type(some_type) ptr2functions = filter(lambda unit: isinstance(unit, declarations.calldef_type_t), units) if ptr2functions: return messages.W1004 # Function that take as agrument some instance of non public class # will not be exported. Same to the return variable if isinstance(units[-1], declarations.declarated_t): dtype = units[-1] if isinstance(dtype.declaration.parent, declarations.class_t): if dtype.declaration not in dtype.declaration.parent.public_members: return messages.W1005 no_ref = declarations.remove_reference(some_type) no_ptr = declarations.remove_pointer(no_ref) no_const = declarations.remove_const(no_ptr) if declarations.is_array(no_const): return messages.W1006 return self._exportable_impl_derived()
def add_decl_desc(decl): try: # assume there are some docs for the declaration desc_list = dict_decl_name_to_desc[(decl.parent.name, decl.name)] desc_count = len(desc_list)-1 reference = desc_list[desc_count] except KeyError: desc_list = None desc_count = 0 reference = None try: # assume decl is a function for a in decl.arguments: if not a.name in decl._args_docs: continue arg = a.name add_decl_boost_doc(decl, "Argument '%s':" % arg) for z in decl._args_docs[arg]: add_decl_boost_doc(decl, " "+z) if decl._output_args: # get the return value and output arguments return_list = [] if decl.return_type.partial_decl_string!='void': return_type = _D.remove_const(_D.remove_reference(decl.return_type)) pds = unique_pds(return_type.partial_decl_string) pds = current_sb.get_registered_decl_name(pds) return_list.append("(%s)" % pds) return_list.extend([x.name for x in decl._output_args]) # document it add_decl_boost_doc(decl, "Returns:") s = "" for r in return_list: s += r+", " s = s[:-2] if len(return_list) > 1: s = " ("+s+")" else: s = " "+s add_decl_boost_doc(decl, s) except AttributeError: pass if reference is not None: add_decl_boost_doc(decl, " "+reference, False, word_wrap=False) add_decl_boost_doc(decl, "Reference:", False) try: # assume decl is a function alias = decl.transformations[0].alias if len(decl.transformations) > 0 else decl.alias if alias != decl.name: add_decl_boost_doc(decl, " "+decl.name, False) add_decl_boost_doc(decl, "Wrapped function:", False) except AttributeError: pass for i in xrange(desc_count-1, -1, -1): add_decl_boost_doc(decl, desc_list[i], False)
def check_type_compatibility( self, fget, fset ): #algorithms allows "const" differences between types t1 = fget.return_type t2 = fset.arguments[0].type if declarations.is_same( t1, t2 ): return True elif declarations.is_pointer( t1 ) and declarations.is_pointer( t2 ): t1 = declarations.remove_cv( declarations.remove_pointer( t1 ) ) t2 = declarations.remove_cv( declarations.remove_pointer( t2 ) ) return declarations.is_same( t1, t2 ) elif declarations.is_reference( t1 ) and declarations.is_reference( t2 ): t1 = declarations.remove_cv( declarations.remove_reference( t1 ) ) t2 = declarations.remove_cv( declarations.remove_reference( t2 ) ) return declarations.is_same( t1, t2 ) else: return False
def str_from_ostream(ns): """ Finds all free operators, then exposes only the ones with classes currently exposed then Py++ can do the rest. """ for oper in ns.free_operators( '<<' ): rtype = declarations.remove_declarated( declarations.remove_reference( oper.return_type ) ) type_or_decl = declarations.remove_declarated( declarations.remove_const( declarations.remove_reference( oper.arguments[1].type))) if not isinstance( type_or_decl, declarations.declaration_t ): continue if type_or_decl.ignore == False: decl_logger.info("Exposing operator<<: " + str(oper)) oper.include()
def test( self ): buggy = self.global_ns.mem_fun( 'buggy' ) ExpressionError = self.global_ns.class_( 'ExpressionError' ) self.failUnless( len( buggy.exceptions ) == 1 ) err = buggy.exceptions[0] self.failUnless( declarations.is_reference( err ) ) err = declarations.remove_declarated( declarations.remove_reference( err ) ) self.failUnless( err is ExpressionError )
def find_class(type_): type_ = declarations.remove_reference(type_) if declarations.is_class(type_): return declarations.class_traits.get_declaration(type_) elif declarations.is_class_declaration(type_): return declarations.class_declaration_traits.get_declaration(type_) else: return None
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 test(self): buggy = self.global_ns.member_function('buggy') expression_error = self.global_ns.class_('ExpressionError') self.assertTrue(len(buggy.exceptions) == 1) err = buggy.exceptions[0] self.assertTrue(declarations.is_reference(err)) err = declarations.remove_declarated( declarations.remove_reference(err)) self.assertTrue(err is expression_error)
def remove_const_from_reference(type): "Helper to avoid compile errors with const-reference-protected-destructor argument types" if not type_traits.is_reference(type): return type nonref = declarations.remove_reference(type) if not type_traits.is_const(nonref): return type nonconst = declarations.remove_const(nonref) return cpptypes.reference_t(nonconst)
def __is_unqualified_enum(self, func, arg): type_ = declarations.remove_reference(declarations.remove_cv(arg.type)) if not declarations.is_enum(type_): return False enum_type = declarations.enum_declaration(type_) # GCCXML does not qualify an enum value in the default argument # but CastXML does. Split the default value and use only the # enum value for fixing it. return enum_type.has_value_name( arg.default_value.split('::')[-1])
def __fix_unqualified_enum(self, func, arg): type_ = declarations.remove_reference(declarations.remove_cv(arg.type)) enum_type = declarations.enum_declaration(type_) if self.__cxx_std.is_cxx11_or_greater: qualifier_decl_string = enum_type.decl_string else: qualifier_decl_string = enum_type.parent.decl_string return self.__join_names( qualifier_decl_string, arg.default_value.split('::')[-1])
def visit_reference( self ): no_ref = declarations.remove_const( declarations.remove_reference( self.user_type ) ) if declarations.is_same( declarations.char_t(), no_ref ): return "ctypes.c_char_p" elif declarations.is_same( declarations.wchar_t(), no_ref ): return "ctypes.c_wchar_p" elif declarations.is_same( declarations.void_t(), no_ref ): return "ctypes.c_void_p" else: base_visitor = self.create_converter( self.user_type.base ) internal_type_str = declarations.apply_visitor( base_visitor, base_visitor.user_type ) return "ctypes.POINTER( %s )" % internal_type_str
def _get_has_setter( self ): if declarations.is_const( declarations.remove_reference( self.declaration.type ) ): return False elif python_traits.is_immutable( self._get_exported_var_type() ): return True else: pass no_ref = declarations.remove_reference( self.declaration.type ) no_const = declarations.remove_const( no_ref ) base_type = declarations.remove_alias( no_const ) if not isinstance( base_type, declarations.declarated_t ): return True #TODO ???? decl = base_type.declaration if decl.is_abstract: return False if declarations.has_destructor( decl ) and not declarations.has_public_destructor( decl ): return False if not declarations.has_copy_constructor(decl): return False return True
def create_default(self): cntrl = self.controller.default_controller make_object = algorithm.create_identifier( self, 'pyplusplus::call_policies::make_object' ) make_tuple = algorithm.create_identifier( self, 'boost::python::make_tuple' ) tmpl_values = dict() tmpl_values['unique_function_name'] = self.default_name() tmpl_values['return_type'] = cntrl.wrapper_return_type.decl_string tmpl_values['arg_declarations'] = self.args_default_declaration() tmpl_values['wrapper_class'] = self.parent.wrapper_alias tmpl_values['wrapped_class'] = declarations.full_name( self.declaration.parent ) tmpl_values['wrapped_inst'] = cntrl.inst_arg.name tmpl_values['wrapped_inst_constness'] = '' if declarations.is_const( declarations.remove_reference( cntrl.inst_arg.type ) ): tmpl_values['wrapped_inst_constness'] = 'const' decl_vars = cntrl.variables[:] if not declarations.is_void( self.declaration.return_type ): decl_vars.append( cntrl.result_variable ) tmpl_values['declare_variables'] \ = os.linesep + os.linesep.join( map( lambda var: self.indent( var.declare_var_string() ) , decl_vars ) ) tmpl_values['pre_call'] = os.linesep + self.indent( os.linesep.join( cntrl.pre_call ) ) tmpl_values['save_result'] = '' if not declarations.is_void( self.declaration.return_type ): tmpl_tmp = '%(result_var_name)s = ' if declarations.is_reference( self.declaration.return_type ): tmpl_tmp = tmpl_tmp + '&' tmpl_values['save_result'] = tmpl_tmp % dict( result_var_name=cntrl.result_variable.name ) tmpl_values['function_name'] = self.declaration.name tmpl_values['arg_expressions'] = self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) return_stmt_creator = calldef_utils.return_stmt_creator_t( self , cntrl , cntrl.result_variable , cntrl.return_variables ) tmpl_values['post_call'] = os.linesep + self.indent( os.linesep.join( cntrl.post_call ) ) if return_stmt_creator.pre_return_code: tmpl_values['post_call'] \ = os.linesep.join([ tmpl_values['post_call'] , self.indent( return_stmt_creator.pre_return_code )]) tmpl_values['return'] = os.linesep + self.indent( return_stmt_creator.statement ) f_def = cntrl.template.substitute(tmpl_values) return remove_duplicate_linesep( f_def )
def visit_reference(self): no_ref = declarations.remove_const( declarations.remove_reference(self.user_type)) if declarations.is_same(declarations.char_t(), no_ref): return "ctypes.c_char_p" elif declarations.is_same(declarations.wchar_t(), no_ref): return "ctypes.c_wchar_p" elif declarations.is_same(declarations.void_t(), no_ref): return "ctypes.c_void_p" else: base_visitor = self.create_converter(self.user_type.base) internal_type_str = declarations.apply_visitor( base_visitor, base_visitor.user_type) return "ctypes.POINTER( %s )" % internal_type_str
def applyDefaultReturnPolicies(functions): for f in functions: if not f.call_policies: return_type = f.return_type if declarations.is_reference(return_type) or declarations.is_pointer(return_type): type_info = return_type type_info = declarations.remove_pointer(type_info) type_info = declarations.remove_reference(type_info) type_info = declarations.remove_const(type_info) # Se il tipo non e' esposto (potrebbe essere una classe, ma non ci sono informazioni perche' la dichiarazione non e' stata incontrata), viene gestito tramite return_opaque_pointer if declarations.is_class(type_info): f.call_policies = call_policies.return_value_policy(call_policies.reference_existing_object) else: f.call_policies = call_policies.return_value_policy(call_policies.return_opaque_pointer)
def _get_has_setter(self): if declarations.is_const( declarations.remove_reference(self.declaration.type)): return False elif python_traits.is_immutable(self._get_exported_var_type()): return True else: pass no_ref = declarations.remove_reference(self.declaration.type) no_const = declarations.remove_const(no_ref) base_type = declarations.remove_alias(no_const) if not isinstance(base_type, declarations.declarated_t): return True #TODO ???? decl = base_type.declaration if decl.is_abstract: return False if declarations.has_destructor( decl) and not declarations.has_public_destructor(decl): return False if not declarations.has_copy_constructor(decl): return False return True
def create_default(self): cntrl = self.controller.default_controller make_object = algorithm.create_identifier( self, 'pyplusplus::call_policies::make_object' ) make_tuple = algorithm.create_identifier( self, 'boost::python::make_tuple' ) tmpl_values = dict() tmpl_values['unique_function_name'] = self.default_name() tmpl_values['return_type'] = cntrl.wrapper_return_type.decl_string tmpl_values['arg_declarations'] = self.args_default_declaration() tmpl_values['wrapper_class'] = self.parent.wrapper_alias tmpl_values['wrapped_class'] = declarations.full_name( self.declaration.parent ) tmpl_values['wrapped_inst'] = cntrl.inst_arg.name tmpl_values['wrapped_inst_constness'] = '' if declarations.is_const( declarations.remove_reference( cntrl.inst_arg.type ) ): tmpl_values['wrapped_inst_constness'] = 'const' decl_vars = cntrl.variables[:] if not declarations.is_void( self.declaration.return_type ): decl_vars.append( cntrl.result_variable ) tmpl_values['declare_variables'] \ = os.linesep + os.linesep.join( [self.indent( var.declare_var_string() ) for var in decl_vars] ) tmpl_values['pre_call'] = os.linesep + self.indent( os.linesep.join( cntrl.pre_call ) ) tmpl_values['save_result'] = '' if not declarations.is_void( self.declaration.return_type ): tmpl_tmp = '%(result_var_name)s = ' if declarations.is_reference( self.declaration.return_type ): tmpl_tmp = tmpl_tmp + '&' tmpl_values['save_result'] = tmpl_tmp % dict( result_var_name=cntrl.result_variable.name ) tmpl_values['function_name'] = self.declaration.name tmpl_values['arg_expressions'] = self.PARAM_SEPARATOR.join( cntrl.arg_expressions ) return_stmt_creator = calldef_utils.return_stmt_creator_t( self , cntrl , cntrl.result_variable , cntrl.return_variables ) tmpl_values['post_call'] = os.linesep + self.indent( os.linesep.join( cntrl.post_call ) ) if return_stmt_creator.pre_return_code: tmpl_values['post_call'] \ = os.linesep.join([ tmpl_values['post_call'] , self.indent( return_stmt_creator.pre_return_code )]) tmpl_values['return'] = os.linesep + self.indent( return_stmt_creator.statement ) f_def = cntrl.template.substitute(tmpl_values) return remove_duplicate_linesep( f_def )
def check_type_compatibility(self, fget, fset): if decl_wrappers.name_based_recognizer_t.check_type_compatibility(self, fget, fset): return True # N.B.: la base non contempla il caso il cui la get restituisca "type" e la set abbia come parametro "const type &" (capita ad esempio in circostanze dove il valore di ritorno della get viene ottenuto tramite conversione da stringa) t1 = fget.return_type t2 = fset.arguments[0].type if declarations.is_reference(t2): t2 = declarations.remove_cv( declarations.remove_reference( t2 ) ) if declarations.is_same( t1, t2 ): return True return False
def call_traits( type_ ): """http://boost.org/libs/utility/call_traits.htm""" type_ = declarations.remove_alias( type_ ) if is_immutable( type_ ): return "%s" #pass by value elif declarations.is_reference( type_ ): no_ref = declarations.remove_reference( type_ ) if is_immutable( no_ref ): return "%s" #pass by value else: return "boost::ref(%s)" #pass by ref elif declarations.is_pointer( type_ ) \ and not is_immutable( type_.base ) \ and not declarations.is_pointer( type_.base ): return "boost::python::ptr(%s)" #pass by ptr else: return "%s" #pass by value
def __init__( self, function ): controller_base_t.__init__( self, function ) self.__vars_manager = create_variables_manager( function ) self.__wrapper_args = [ arg.clone() for arg in function.arguments ] initialize_expr = '' result_type = self.function.return_type if declarations.is_reference( self.function.return_type ): initialize_expr = ' = 0' result_type = declarations.pointer_t( declarations.remove_reference( self.function.return_type ) ) self.__result_var = variable_t( result_type , self.register_variable_name( 'result' ) , initialize_expr=initialize_expr ) self.__return_variables = [] self.__pre_call = [] self.__post_call = [] self.__arg_expressions = [ arg.name for arg in function.arguments ]
def __init__(self, function): controller_base_t.__init__(self, function) self.__vars_manager = create_variables_manager(function) self.__wrapper_args = [arg.clone() for arg in function.arguments] initialize_expr = '' result_type = self.function.return_type if declarations.is_reference(self.function.return_type): initialize_expr = ' = 0' result_type = declarations.pointer_t( declarations.remove_reference(self.function.return_type)) self.__result_var = variable_t(result_type, self.register_variable_name('result'), initialize_expr=initialize_expr) self.__return_variables = [] self.__pre_call = [] self.__post_call = [] self.__arg_expressions = [arg.name for arg in function.arguments]
def __call__(self, variable, hint=None): if not isinstance(variable, declarations.variable_t): return None assert hint in ('get', 'set') if not declarations.is_reference(variable.decl_type): return None no_ref = declarations.remove_reference(variable.decl_type) base_type = declarations.remove_const(no_ref) if python_traits.is_immutable(base_type): #the relevant code creator will generate code, that will return this member variable #by value return decl_wrappers.default_call_policies() if not isinstance(base_type, declarations.declarated_t): return None base_type = declarations.remove_alias(base_type) declaration = base_type.declaration if declarations.is_class_declaration(declaration): return None if declaration.is_abstract: return None if declarations.has_destructor( declaration ) and not declarations.has_public_destructor(declaration): return None if not declarations.has_copy_constructor(declaration): return None if hint == 'get': #if we rich this line, it means that we are able to create an obect using #copy constructor. if declarations.is_const(no_ref): return decl_wrappers.return_value_policy( decl_wrappers.copy_const_reference) else: return decl_wrappers.return_value_policy( decl_wrappers.copy_non_const_reference) else: return decl_wrappers.default_call_policies()
def __call__(self, calldef, hint=None): if not isinstance( calldef, declarations.calldef_t ): return None if not isinstance( calldef, declarations.member_operator_t ): return None if calldef.symbol != '[]': return None return_type = declarations.remove_cv( calldef.return_type ) if declarations.is_reference( return_type ): return_type = declarations.remove_reference( return_type ) if python_traits.is_immutable( return_type ): if declarations.is_const( calldef.return_type ): return decl_wrappers.return_value_policy( decl_wrappers.copy_const_reference ) else: return decl_wrappers.return_value_policy( decl_wrappers.copy_non_const_reference ) else: return decl_wrappers.return_internal_reference()
def __call__( self, variable, hint=None ): if not isinstance( variable, declarations.variable_t ): return None assert hint in ( 'get', 'set' ) if not declarations.is_reference( variable.type ): return None no_ref = declarations.remove_reference( variable.type ) base_type = declarations.remove_const( no_ref ) if python_traits.is_immutable( base_type ): #the relevant code creator will generate code, that will return this member variable #by value return decl_wrappers.default_call_policies() if not isinstance( base_type, declarations.declarated_t ): return None base_type = declarations.remove_alias( base_type ) decl = base_type.declaration if declarations.is_class_declaration( decl ): return None if decl.is_abstract: return None if declarations.has_destructor( decl ) and not declarations.has_public_destructor( decl ): return None if not declarations.has_copy_constructor(decl): return None if hint == 'get': #if we rich this line, it means that we are able to create an obect using #copy constructor. if declarations.is_const( no_ref ): return decl_wrappers.return_value_policy( decl_wrappers.copy_const_reference ) else: return decl_wrappers.return_value_policy( decl_wrappers.copy_non_const_reference ) else: return decl_wrappers.default_call_policies()
def _exportable_impl( self ): all_types = [ arg.type for arg in self.arguments ] all_types.append( self.return_type ) for some_type in all_types: units = declarations.decompose_type( some_type ) ptr2functions = filter( lambda unit: isinstance( unit, declarations.calldef_type_t ) , units ) if ptr2functions: return messages.W1004 #Function that take as agrument some instance of non public class #will not be exported. Same to the return variable if isinstance( units[-1], declarations.declarated_t ): dtype = units[-1] if isinstance( dtype.declaration.parent, declarations.class_t ): if dtype.declaration not in dtype.declaration.parent.public_members: return messages.W1005 no_ref = declarations.remove_reference( some_type ) no_ptr = declarations.remove_pointer( no_ref ) no_const = declarations.remove_const( no_ptr ) if declarations.is_array( no_const ): return messages.W1006 return self._exportable_impl_derived()
def check_args_exportable ( function, ns ): """ Look at each argument in the function and determine that we have exported it or it's a special. """ ret = True Specials = ['::Ogre::String'] for a in function.arguments: rawarg = declarations.remove_declarated( declarations.remove_const( declarations.remove_reference( declarations.remove_pointer ( a.type )))) ## now check if the arg is a fundemental type (int float etc), a void ## or a special .. if declarations.is_arithmetic (rawarg)\ or declarations.is_void(rawarg)\ or declarations.is_enum(rawarg): pass elif 'Ogre::' in a.type.decl_string: # assume it's a class and needs checking name = a.type.decl_string.split()[0] # let's grab the actual class name if name in Specials: # we know that the classes in specials DO exist pass else: try: tcls = ns.class_(name) if not tcls.exportable or tcls.ignore or type ( tcls.parent ) != decl_wrappers.namespace_wrapper.namespace_t: ## print "check_args_exportable: NOT EXPORTABLE:", tcls, tcls.exportable, tcls.ignore , type ( tcls.parent ) ret = False break else: pass # print name, "IS exportable" except: print "check_args_exportable: unable to find:", name ret = False else: print "check_args_exportable: NOT SURE...", a, a.type, type(a.type) return ret
def __find_out_is_read_only(self): type_ = declarations.remove_alias( self.decl_type ) if isinstance( type_, declarations.const_t ): return True if declarations.is_pointer( type_ ): type_ = declarations.remove_pointer( type_ ) if declarations.is_reference( type_ ): type_ = declarations.remove_reference( type_ ) if isinstance( type_, declarations.const_t ): return True if self.apply_smart_ptr_wa: return False #all smart pointers has assign operator if isinstance( type_, declarations.declarated_t ) \ and isinstance( type_.declaration, declarations.class_t ) \ and not declarations.has_public_assign( type_.declaration ): return True return False
def __find_out_is_read_only(self): type_ = declarations.remove_alias( self.type ) if isinstance( type_, declarations.const_t ): return True if declarations.is_pointer( type_ ): type_ = declarations.remove_pointer( type_ ) if declarations.is_reference( type_ ): type_ = declarations.remove_reference( type_ ) if isinstance( type_, declarations.const_t ): return True if self.apply_smart_ptr_wa: return False #all smart pointers has assign operator if isinstance( type_, declarations.declarated_t ) \ and isinstance( type_.declaration, declarations.class_t ) \ and not declarations.has_public_assign( type_.declaration ): return True return False
def ManualFixes ( mb ): global_ns = mb.global_ns main_ns = global_ns funcs = [ '::ssgBranch::getByName' ,'::ssgBranch::getByPath' ,'::ssgEntity::getByName' ,'::ssgEntity::getByPath' ] # for f in funcs: # main_ns.member_functions(f).call_policies = call_policies.default_call_policies() # bug in Py++ where is uses the wrong call policies on a transformed function for fun in main_ns.member_functions(allow_empty=True): if fun.transformations: if declarations.is_pointer(fun.return_type ) : rawarg = declarations.remove_declarated( declarations.remove_const( declarations.remove_reference( declarations.remove_pointer ( fun.return_type )))) if not declarations.is_arithmetic (rawarg) and not declarations.is_void(rawarg): fun.call_policies = call_policies.default_call_policies() print "Changed call policies on ", fun
'::ushort': 'uint16', 'short unsigned int': 'uint16', '::schar': 'int8', 'signed char': 'int8', '::uchar': 'uint8', 'unsigned char': 'uint8', 'bool': 'bool', } # FileStorage's 'write' functions for z in sb.mb.free_funs(lambda x: x.name=='write' and \ x.arguments[0].type.partial_decl_string.startswith('::cv::FileStorage')): z.include() if len(z.arguments) > 2 and \ z.arguments[1].type.partial_decl_string.startswith('::std::string'): t = D.remove_const(D.remove_reference(z.arguments[2].type)) else: t = D.remove_const(D.remove_reference(z.arguments[1].type)) name = 'write_' + C_to_Python_name_dict[t.partial_decl_string] z._transformer_kwds['alias'] = name z.alias = name # FileNode's 'read' functions for z in sb.mb.free_funs(lambda x: x.name=='read' and \ x.arguments[0].type.partial_decl_string.startswith('::cv::FileNode')): z.include() if z.arguments[1].name == 'keypoints': z._transformer_creators.append(FT.arg_output('keypoints')) else: z._transformer_creators.append(FT.output(z.arguments[1].name)) FT.doc_output(z, z.arguments[1])
def __is_unqualified_enum(self, func, arg): type_ = declarations.remove_reference( declarations.remove_cv( arg.type ) ) if not declarations.is_enum( type_ ): return False enum_type = declarations.enum_declaration( type_ ) return enum_type.has_value_name( arg.default_value )
def add_decl_desc(decl): try: # assume there are some docs for the declaration desc_list = dict_decl_name_to_desc[(decl.parent.name, decl.name)] desc_count = len(desc_list) - 1 reference = desc_list[desc_count] except KeyError: desc_list = None desc_count = 0 reference = None try: # assume decl is a function for a in decl.arguments: if not a.name in decl._args_docs: continue arg = a.name add_decl_boost_doc(decl, "Argument '%s':" % arg) for z in decl._args_docs[arg]: add_decl_boost_doc(decl, " " + z) if decl._output_args: # get the return value and output arguments return_list = [] if decl.return_type.partial_decl_string != 'void': return_type = _D.remove_const( _D.remove_reference(decl.return_type)) pds = unique_pds(return_type.partial_decl_string) pds = current_sb.get_registered_decl_name(pds) return_list.append("(%s)" % pds) return_list.extend([x.name for x in decl._output_args]) # document it add_decl_boost_doc(decl, "Returns:") s = "" for r in return_list: s += r + ", " s = s[:-2] if len(return_list) > 1: s = " (" + s + ")" else: s = " " + s add_decl_boost_doc(decl, s) except AttributeError: pass if reference is not None: add_decl_boost_doc(decl, " " + reference, False, word_wrap=False) add_decl_boost_doc(decl, "Reference:", False) try: # assume decl is a function alias = decl.transformations[0].alias if len( decl.transformations) > 0 else decl.alias if alias != decl.name: add_decl_boost_doc(decl, " " + decl.name, False) add_decl_boost_doc(decl, "Wrapped function:", False) except AttributeError: pass for i in xrange(desc_count - 1, -1, -1): add_decl_boost_doc(decl, desc_list[i], False)
def add_self(self, output_string): # Check for exclusions if self.exclusion_critera(): return output_string # Which definition type def_adorn = "" if self.method_decl.has_static: def_adorn += "_static" # How to point to class if not self.method_decl.has_static: self_ptr = self.class_short_name + "::*" else: self_ptr = "*" # Get the arg signature arg_signature = "" num_arg_types = len(self.method_decl.argument_types) commandline_type = ( num_arg_types == 2 and self.method_decl.arguments[0].decl_type.decl_string == 'int' and self.method_decl.arguments[1].decl_type.decl_string == 'char * *') if commandline_type: arg_signature = " std::vector<std::string> " else: for idx, eachArg in enumerate(self.method_decl.argument_types): arg_signature += eachArg.decl_string if idx < num_arg_types - 1: arg_signature += ", " # Const-ness const_adorn = "" if self.method_decl.has_const: const_adorn = ' const ' # Default args default_args = "" ref_arg_defs = "" ref_args = [] args = "" origin_args = [] if commandline_type: default_args = ", py::arg(\"argc\")" else: if not self.default_arg_exclusion_criteria(): arg_types = self.method_decl.argument_types for idx, eachArg in enumerate(self.method_decl.arguments): default_args += ', py::arg("{}")'.format(eachArg.name) if declarations.is_reference(eachArg.decl_type) and ( not declarations.is_const(eachArg.decl_type.base)): ref_arg_defs += "{0} {1};\n ".format( declarations.remove_reference(eachArg.decl_type), eachArg.name) ref_args.append(eachArg.name) else: if eachArg.default_value is not None: # Hack for missing template in default args repl_value = str(eachArg.default_value) if "<DIM>" in repl_value: if "<2>" in str(arg_types[idx]).replace( " ", ""): repl_value = repl_value.replace( "<DIM>", "<2>") elif "<3>" in str(arg_types[idx]).replace( " ", ""): repl_value = repl_value.replace( "<DIM>", "<3>") default_args += ' = ' + repl_value args += ", {0} {1}".format(eachArg.decl_type, eachArg.name) origin_args.append(eachArg.name) # Call policy pointer_call_policy = self.class_info.hierarchy_attribute( 'pointer_call_policy') reference_call_policy = self.class_info.hierarchy_attribute( 'reference_call_policy') call_policy = "" is_ptr = declarations.is_pointer(self.method_decl.return_type) if pointer_call_policy is not None and is_ptr: call_policy = ", py::return_value_policy::" + pointer_call_policy is_ref = declarations.is_reference(self.method_decl.return_type) if reference_call_policy is not None and is_ref: call_policy = ", py::return_value_policy::" + reference_call_policy # method name mapping method_name = self.method_decl.name if self.method_decl.name in self.class_info.parent.function_mapping: method_name = self.class_info.parent.function_mapping[ self.method_decl.name] # return adorn return_string = self.method_decl.return_type.decl_string return_adorn = "" return_arg = "" if return_string is not "void": return_adorn = "return" return_arg = "auto ret__ =" ref_args.append("ret__") # class override name class_short_name = self.class_short_name method_dict = { 'def_adorn': def_adorn, 'method_name_alias': method_name, 'method_name': self.method_decl.name, 'return_type': self.method_decl.return_type.decl_string, 'self_ptr': self_ptr, 'arg_signature': arg_signature, 'const_adorn': const_adorn, 'class_short_name': class_short_name, 'method_docs': '" "', 'default_args': default_args, 'call_policy': call_policy, 'args': args, 'ref_arg_defs': ref_arg_defs, 'return_arg': return_arg, 'ref_args': ref_args[0] if len(ref_args) == 1 else "py::make_tuple({0})".format(', '.join(ref_args)), 'origin_args': ', '.join(origin_args), 'return_adorn': return_adorn } if commandline_type: template = self.wrapper_templates["class_method_argc_argv"] output_string += template.format(**method_dict) else: if ref_arg_defs != "": template = self.wrapper_templates["class_method_with_ref"] output_string += template.format(**method_dict) else: template = self.wrapper_templates["class_method"] output_string += template.format(**method_dict) return output_string
def _get_var_type(self): type_ = declarations.remove_reference(self.declaration.decl_type) type_ = declarations.remove_const(type_) if python_traits.is_immutable(type_): return type_ return self.declaration.decl_type
def tt(type_): type_ = declarations.remove_reference(type_) type_ = declarations.remove_const(type_) return declarations.reference_t(type_)
def __fix_unqualified_enum( self, func, arg): type_ = declarations.remove_reference( declarations.remove_cv( arg.type ) ) enum_type = declarations.enum_declaration( type_ ) return self.__join_names( enum_type.parent.decl_string, arg.default_value )
def test_element_type(self): do_nothing = self.global_ns.free_fun('do_nothing') v = declarations.remove_reference( declarations.remove_declarated(do_nothing.arguments[0].decl_type)) declarations.vector_traits.element_type(v)