Commits

Armin Rigo  committed 4bf2208

Opaque structs.

  • Participants
  • Parent commits eb296df
  • Branches cpy-extension

Comments (0)

Files changed (3)

File cffi/model.py

         BType = self.get_btype(ffi)
         ffi._cached_btypes[self] = BType
         args = [BType]
-        for tp in self.fldtypes:
-            args.append(ffi._get_cached_btype(tp))
+        if self.fldtypes is not None:
+            for tp in self.fldtypes:
+                args.append(ffi._get_cached_btype(tp))
         return args
 
     def finish_backend_type(self, ffi, BType, *fldtypes):
+        if self.fldnames is None:
+            return BType   # not completing it: it's an opaque struct
+        #
         if self.fixedlayout is None:
             lst = zip(self.fldnames, fldtypes, self.fldbitsize)
             ffi._backend.complete_struct_or_union(BType, lst, self)

File cffi/verifier.py

     # struct declarations
 
     def generate_cpy_struct_decl(self, tp, name):
+        if tp.fldnames is None:
+            return     # nothing to do with opaque structs
         assert name == tp.name
         prnt = self.prnt
         prnt('static PyObject *')
         prnt()
 
     def generate_cpy_struct_method(self, tp, name):
+        if tp.fldnames is None:
+            return     # nothing to do with opaque structs
         self.prnt('  {"_cffi_struct_%s", _cffi_struct_%s, METH_NOARGS},' % (
             name, name))
 
     def loading_cpy_struct(self, tp, name, module):
+        if tp.fldnames is None:
+            return     # nothing to do with opaque structs
         assert name == tp.name
         function = getattr(module, '_cffi_struct_%s' % name)
         layout = function()
             tp.fixedlayout = fieldofs, fieldsize, totalsize, totalalignment
 
     def loaded_cpy_struct(self, tp, name, module):
+        if tp.fldnames is None:
+            return     # nothing to do with opaque structs
         self.ffi._get_cached_btype(tp)   # force 'fixedlayout' to be considered
 
     # ----------

File testing/test_verify.py

                 py.test.raises(VerificationError, ffi.verify,
                                "typedef %s foo_t;" % real)
 
+def test_nondecl_struct():
+    ffi = FFI()
+    ffi.cdef("typedef struct foo_s foo_t; int bar(foo_t *);")
+    lib = ffi.verify("typedef struct foo_s foo_t;\n"
+                     "int bar(foo_t *f) { return 42; }\n")
+    assert lib.bar(None) == 42
+
 def test_missing_typedef():
     py.test.skip("in-progress")
     ffi = FFI()