Commits

Armin Rigo  committed 5d0dca6

Test and fix: initialize things in a different order.

  • Participants
  • Parent commits e92c620
  • Branches cpy-extension

Comments (0)

Files changed (2)

File cffi/verifier.py

         print >> self.f, what
 
     def gettypenum(self, type):
-        BType = self.ffi._get_cached_btype(type)
         try:
-            return self.typesdict[BType]
+            return self.typesdict[type]
         except KeyError:
             num = len(self.typesdict)
-            self.typesdict[BType] = num
+            self.typesdict[type] = num
             return num
 
     def verify(self, preamble, stop_on_warnings=True):
             self.prnt('void init%s()' % modname)
             self.prnt('{')
             self.prnt('  Py_InitModule("%s", _cffi_methods);' % modname)
+            self.prnt('  _cffi_init();')
             self.prnt('}')
             #
             del self.f
         except ImportError, e:
             raise ffiplatform.VerificationError(str(e))
         #
+        self.load(module, 'loading')
+        #
         revmapping = dict([(value, key)
                            for (key, value) in self.typesdict.items()])
         lst = [revmapping[i] for i in range(len(revmapping))]
+        lst = map(self.ffi._get_cached_btype, lst)
         dct = module._cffi_setup(lst, ffiplatform.VerificationError)
         del module._cffi_setup
         module.__dict__.update(dct)
         #
-        self.load(module, 'loading')
         self.load(module, 'loaded')
         #
         return module
 
 static PyObject *_cffi_setup(PyObject *self, PyObject *args)
 {
-    PyObject *module;
-    PyObject *c_api_object;
-
     if (!PyArg_ParseTuple(args, "OO", &_cffi_types, &_cffi_VerificationError))
         return NULL;
     Py_INCREF(_cffi_types);
     Py_INCREF(_cffi_VerificationError);
 
-    module = PyImport_ImportModule("_ffi_backend");
+    return _cffi_setup_custom();
+}
+
+static void _cffi_init(void)
+{
+    PyObject *module = PyImport_ImportModule("_ffi_backend");
+    PyObject *c_api_object;
+
     if (module == NULL)
-        return NULL;
+        return;
 
     c_api_object = PyObject_GetAttrString(module, "_C_API");
     if (c_api_object == NULL)
-        return NULL;
+        return;
     if (!PyCObject_Check(c_api_object)) {
         PyErr_SetNone(PyExc_ImportError);
-        return NULL;
+        return;
     }
     _cffi_exports = (void **)PyCObject_AsVoidPtr(c_api_object);
-
-    return _cffi_setup_custom();
 }
 
 #define _cffi_type(num) ((CTypeDescrObject *)PyList_GET_ITEM(_cffi_types, num))

File testing/test_verify.py

     s = ffi.new("struct foo_s")
     assert ffi.sizeof(s.a) == 17 * ffi.sizeof('int')
 
+def test_struct_array_guess_length_2():
+    ffi = FFI()
+    ffi.cdef("struct foo_s { int a[]; ...; };\n"    # <= no declared length
+             "int bar(struct foo_s *);\n")
+    lib = ffi.verify("struct foo_s { int x; int a[17]; int y; };\n"
+                     "int bar(struct foo_s *f) { return f->a[14]; }\n")
+    assert ffi.sizeof('struct foo_s') == 19 * ffi.sizeof('int')
+    s = ffi.new("struct foo_s")
+    s.a[14] = 4242
+    assert lib.bar(s) == 4242
+
 def test_global_constants():
     ffi = FFI()
     # use 'static const int', as generally documented, although in this