Ejemplo n.º 1
0
    def test_function_pointer(self):
        """
        Test working with pointers and function pointers.

        """

        decls = parser.parse([self.header], self.config)
        global_ns = declarations.get_global_namespace(decls)

        # Test on a function pointer
        criteria = declarations.variable_matcher(name="func1")
        variables = declarations.matcher.find(criteria, global_ns)

        self.assertTrue(variables[0].name == "func1")
        self.assertTrue(
            isinstance(variables[0].decl_type, declarations.pointer_t))
        self.assertTrue(
            str(variables[0].decl_type) == "void (*)( int,double )")
        self.assertTrue(
            declarations.is_calldef_pointer(variables[0].decl_type))
        self.assertTrue(
            isinstance(declarations.remove_pointer(variables[0].decl_type),
                       declarations.free_function_type_t))

        # Get the function (free_function_type_t) and test the return and
        # argument types
        function = variables[0].decl_type.base
        self.assertTrue(isinstance(function.return_type, declarations.void_t))
        self.assertTrue(
            isinstance(function.arguments_types[0], declarations.int_t))
        self.assertTrue(
            isinstance(function.arguments_types[1], declarations.double_t))

        # Test on a normal pointer
        criteria = declarations.variable_matcher(name="myPointer")
        variables = declarations.matcher.find(criteria, global_ns)

        self.assertTrue(variables[0].name == "myPointer")
        self.assertTrue(
            isinstance(variables[0].decl_type, declarations.pointer_t))
        self.assertFalse(
            declarations.is_calldef_pointer(variables[0].decl_type))
        self.assertTrue(
            isinstance(declarations.remove_pointer(variables[0].decl_type),
                       declarations.volatile_t))

        # Test on function pointer in struct (x8)
        for d in global_ns.declarations:
            if d.name == "x8":
                self.assertTrue(
                    isinstance(d.decl_type, declarations.pointer_t))
                self.assertTrue(declarations.is_calldef_pointer(d.decl_type))
                self.assertTrue(
                    isinstance(
                        declarations.remove_pointer(d.decl_type),
                        declarations.member_function_type_t))
                self.assertTrue(
                    str(declarations.remove_pointer(d.decl_type)) ==
                    "void ( ::some_struct_t::* )(  )")
Ejemplo n.º 2
0
    def test_function_pointer(self):
        """
        Test working with pointers and function pointers.

        """

        decls = parser.parse([self.header], self.config)
        global_ns = declarations.get_global_namespace(decls)

        # Test on a function pointer
        criteria = declarations.variable_matcher(name="func1")
        variables = declarations.matcher.find(criteria, global_ns)

        self.assertTrue(variables[0].name == "func1")
        self.assertTrue(
            isinstance(variables[0].decl_type, declarations.pointer_t))
        self.assertTrue(
            str(variables[0].decl_type) == "void (*)( int,double )")
        self.assertTrue(
            declarations.is_calldef_pointer(variables[0].decl_type))
        self.assertTrue(
            isinstance(declarations.remove_pointer(variables[0].decl_type),
                       declarations.free_function_type_t))

        # Get the function (free_function_type_t) and test the return and
        # argument types
        function = variables[0].decl_type.base
        self.assertTrue(isinstance(function.return_type, declarations.void_t))
        self.assertTrue(
            isinstance(function.arguments_types[0], declarations.int_t))
        self.assertTrue(
            isinstance(function.arguments_types[1], declarations.double_t))

        # Test on a normal pointer
        criteria = declarations.variable_matcher(name="myPointer")
        variables = declarations.matcher.find(criteria, global_ns)

        self.assertTrue(variables[0].name == "myPointer")
        self.assertTrue(
            isinstance(variables[0].decl_type, declarations.pointer_t))
        self.assertFalse(
            declarations.is_calldef_pointer(variables[0].decl_type))
        self.assertTrue(
            isinstance(declarations.remove_pointer(variables[0].decl_type),
                       declarations.volatile_t))

        # Test on function pointer in struct (x8)
        for d in global_ns.declarations:
            if d.name == "x8":
                self.assertTrue(
                    isinstance(d.decl_type, declarations.pointer_t))
                self.assertTrue(declarations.is_calldef_pointer(d.decl_type))
                self.assertTrue(
                    isinstance(
                        declarations.remove_pointer(d.decl_type),
                        declarations.member_function_type_t))
                self.assertTrue(
                    str(declarations.remove_pointer(d.decl_type)) ==
                    "void ( ::some_struct_t::* )(  )")
 def visit_pointer( self ):
     no_ptr = declarations.remove_const( declarations.remove_pointer( self.user_type ) )
     if declarations.is_same( declarations.char_t(), no_ptr ) and self.treat_char_ptr_as_binary_data == False:
         return "ctypes.c_char_p"
     elif declarations.is_same( declarations.wchar_t(), no_ptr ) and self.treat_char_ptr_as_binary_data == False:
         return "ctypes.c_wchar_p"
     elif declarations.is_same( declarations.void_t(), no_ptr ):
         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 )
         if declarations.is_calldef_pointer( self.user_type ):
             return internal_type_str
         else:
             return "ctypes.POINTER( %s )" % internal_type_str
Ejemplo n.º 4
0
 def select_problematics( calldef ):
     """Return list of problematic functions for function "calldef" """
     if 1 != len( calldef.required_args ):
         return []
     arg_type = calldef.arguments[0].type
     if declarations.is_calldef_pointer( arg_type ):
         return []
     problematics = []
     for f in calldef.overloads:
         if 1 != len( f.required_args ):
             continue
         if f.ignore:
             continue
         if None != registration_order.is_related( calldef.arguments[0].type, f.arguments[0].type ):
             problematics.append( f )
     return problematics
Ejemplo n.º 5
0
 def select_problematics( calldef ):
     """Return list of problematic functions for function "calldef" """
     if 1 != len( calldef.required_args ):
         return []
     arg_type = calldef.arguments[0].type
     if declarations.is_calldef_pointer( arg_type ):
         return []
     problematics = []
     for f in calldef.overloads:
         if 1 != len( f.required_args ):
             continue
         if f.ignore:
             continue
         if None != registration_order.is_related( calldef.arguments[0].type, f.arguments[0].type ):
             problematics.append( f )
     return problematics
Ejemplo n.º 6
0
 def visit_pointer(self):
     no_ptr = declarations.remove_const(
         declarations.remove_pointer(self.user_type))
     if declarations.is_same(
             declarations.char_t(),
             no_ptr) and self.treat_char_ptr_as_binary_data == False:
         return "ctypes.c_char_p"
     elif declarations.is_same(
             declarations.wchar_t(),
             no_ptr) and self.treat_char_ptr_as_binary_data == False:
         return "ctypes.c_wchar_p"
     elif declarations.is_same(declarations.void_t(), no_ptr):
         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)
         if declarations.is_calldef_pointer(self.user_type):
             return internal_type_str
         else:
             return "ctypes.POINTER( %s )" % internal_type_str
Ejemplo n.º 7
0
function_ptr = global_namespace.variables()[0]

# Print the name of the function pointer
print(function_ptr.name)
# > myFuncPointer

# Print the type of the declaration
print(function_ptr.decl_type)
# > void (*)( int,double )

# Print the real type of the declaration (it's just a pointer)
print(type(function_ptr.decl_type))
# > <class 'pygccxml.declarations.cpptypes.pointer_t'>

# Check if this is a function pointer
print(declarations.is_calldef_pointer(function_ptr.decl_type))
# > True

# Remove the pointer part, to access the function's type
f_type = declarations.remove_pointer(function_ptr.decl_type)

# Print the type
print(type(f_type))
# > <class 'pygccxml.declarations.cpptypes.free_function_type_t'>

# Print the return type and the arguments of the function
print(f_type.return_type)
# > void

# Print the return type and the arguments
print(str(f_type.arguments_types[0]), str(f_type.arguments_types[1]))
Ejemplo n.º 8
0
function_ptr = global_namespace.variables()[0]

# Print the name of the function pointer
print(function_ptr.name)
# > myFuncPointer

# Print the type of the declaration
print(function_ptr.decl_type)
# > void (*)( int,double )

# Print the real type of the declaration (it's just a pointer)
print(type(function_ptr.decl_type))
# > <class 'pygccxml.declarations.cpptypes.pointer_t'>

# Check if this is a function pointer
print(declarations.is_calldef_pointer(function_ptr.decl_type))
# > True

# Remove the pointer part, to access the function's type
f_type = declarations.remove_pointer(function_ptr.decl_type)

# Print the type
print(type(f_type))
# > <class 'pygccxml.declarations.cpptypes.free_function_type_t'>

# Print the return type and the arguments of the function
print(f_type.return_type)
# > void

# Print the return type and the arguments
print(str(f_type.arguments_types[0]), str(f_type.arguments_types[1]))