[pypy-commit] cffi default: Reorganize things a little bit

arigo noreply at buildbot.pypy.org
Wed Jul 11 12:47:57 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r629:e6ef78118297
Date: 2012-07-11 12:47 +0200
http://bitbucket.org/cffi/cffi/changeset/e6ef78118297/

Log:	Reorganize things a little bit

diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -138,11 +138,8 @@
         ast, macros = self._parse('void __dummy(%s);' % cdecl)
         assert not macros
         typenode = ast.ext[-1].type.args.params[0].type
-        type = self._get_type(typenode, force_pointer=force_pointer)
-        if consider_function_as_funcptr:
-            if isinstance(type, model.RawFunctionType):
-                type = self._get_type_pointer(type)
-        return type
+        return self._get_type(typenode, force_pointer=force_pointer,
+                     consider_function_as_funcptr=consider_function_as_funcptr)
 
     def _declare(self, name, obj):
         if name in self._declarations:
@@ -163,7 +160,8 @@
         return model.PointerType(type)
 
     def _get_type(self, typenode, convert_array_to_pointer=False,
-                  force_pointer=False, name=None, partial_length_ok=False):
+                  force_pointer=False, name=None, partial_length_ok=False,
+                  consider_function_as_funcptr=False):
         # first, dereference typedefs, if we have it already parsed, we're good
         if (isinstance(typenode, pycparser.c_ast.TypeDecl) and
             isinstance(typenode.type, pycparser.c_ast.IdentifierType) and
@@ -176,6 +174,9 @@
             else:
                 if force_pointer:
                     return self._get_type_pointer(type)
+                if (consider_function_as_funcptr and
+                        isinstance(type, model.RawFunctionType)):
+                    return type.as_function_pointer()
             return type
         #
         if isinstance(typenode, pycparser.c_ast.ArrayDecl):
@@ -190,7 +191,7 @@
             return model.ArrayType(self._get_type(typenode.type), length)
         #
         if force_pointer:
-            return model.PointerType(self._get_type(typenode))
+            return self._get_type_pointer(self._get_type(typenode))
         #
         if isinstance(typenode, pycparser.c_ast.PtrDecl):
             # pointer type
@@ -232,7 +233,10 @@
         #
         if isinstance(typenode, pycparser.c_ast.FuncDecl):
             # a function type
-            return self._parse_function_type(typenode, name)
+            result = self._parse_function_type(typenode, name)
+            if consider_function_as_funcptr:
+                result = result.as_function_pointer()
+            return result
         #
         raise api.FFIError("bad or unsupported type declaration")
 
@@ -252,7 +256,8 @@
                 and list(params[0].type.type.names) == ['void']):
             del params[0]
         args = [self._get_type(argdeclnode.type,
-                               convert_array_to_pointer=True)
+                               convert_array_to_pointer=True,
+                               consider_function_as_funcptr=True)
                 for argdeclnode in params]
         result = self._get_type(typenode.type)
         return model.RawFunctionType(tuple(args), result, ellipsis)
diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -107,8 +107,6 @@
         result = ffi._get_cached_btype(self.result)
         args = []
         for tp in self.args:
-            if isinstance(tp, RawFunctionType):
-                tp = tp.as_function_pointer()
             args.append(ffi._get_cached_btype(tp))
         return global_cache(ffi, 'new_function_type',
                             tuple(args), result, self.ellipsis)
diff --git a/cffi/verifier.py b/cffi/verifier.py
--- a/cffi/verifier.py
+++ b/cffi/verifier.py
@@ -167,9 +167,7 @@
             self.prnt('    %s;' % errcode)
             return
         #
-        elif isinstance(tp, model.BaseFunctionType):
-            if isinstance(tp, model.RawFunctionType):
-                tp = tp.as_function_pointer()
+        elif isinstance(tp, model.FunctionPtrType):
             converter = '(%s)_cffi_to_c_pointer' % tp.get_c_name('')
             extraarg = ', _cffi_type(%d)' % self.gettypenum(tp)
             errvalue = 'NULL'
@@ -233,8 +231,6 @@
         prnt('{')
         #
         for i, type in enumerate(tp.args):
-            if isinstance(type, model.RawFunctionType):
-                type = type.as_function_pointer()
             prnt('  %s;' % type.get_c_name(' x%d' % i))
         if not isinstance(tp.result, model.VoidType):
             result_code = 'result = '


More information about the pypy-commit mailing list