[pypy-commit] cffi cffi-1.0: Last fix, now test the C++ mode

arigo noreply at buildbot.pypy.org
Sun May 17 15:58:07 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r2037:e26163c83505
Date: 2015-05-17 15:58 +0200
http://bitbucket.org/cffi/cffi/changeset/e26163c83505/

Log:	Last fix, now test the C++ mode

diff --git a/cffi/recompiler.py b/cffi/recompiler.py
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -741,17 +741,23 @@
         prnt('  /* only to generate compile-time warnings or errors */')
         prnt('  (void)p;')
         for fname, ftype, fbitsize in tp.enumfields():
-            if (isinstance(ftype, model.PrimitiveType)
-                and ftype.is_integer_type()) or fbitsize >= 0:
-                # accept all integers, but complain on float or double
-                prnt('  (void)((p->%s) << 1);' % fname)
-            else:
-                # only accept exactly the type declared.
-                try:
+            try:
+                if (isinstance(ftype, model.PrimitiveType)
+                    and ftype.is_integer_type()) or fbitsize >= 0:
+                    # accept all integers, but complain on float or double
+                    prnt('  (void)((p->%s) << 1);' % fname)
+                elif (isinstance(ftype, model.ArrayType)
+                      and (ftype.length is None or ftype.length == '...')):
+                    # for C++: "int(*)tmp[] = &p->a;" errors out if p->a is
+                    # declared as "int[5]".  Instead, write "int *tmp = p->a;".
+                    prnt('  { %s = p->%s; (void)tmp; }' % (
+                        ftype.item.get_c_name('*tmp', 'field %r'%fname), fname))
+                else:
+                    # only accept exactly the type declared.
                     prnt('  { %s = &p->%s; (void)tmp; }' % (
                         ftype.get_c_name('*tmp', 'field %r'%fname), fname))
-                except ffiplatform.VerificationError as e:
-                    prnt('  /* %s */' % str(e))   # cannot verify it, ignore
+            except ffiplatform.VerificationError as e:
+                prnt('  /* %s */' % str(e))   # cannot verify it, ignore
         prnt('}')
         prnt('struct _cffi_align_%s { char x; %s y; };' % (approxname, cname))
         prnt()
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -20,7 +20,7 @@
     kwds.setdefault('undef_macros', ['NDEBUG'])
     module_name = '_CFFI_' + module_name
     ffi.set_source(module_name, source)
-    if 0:     # test the .cpp mode too
+    if 1:     # test the .cpp mode too
         kwds.setdefault('source_extension', '.cpp')
         source = 'extern "C" {\n%s\n}' % (source,)
     return recompiler._verify(ffi, module_name, source, *args, **kwds)
@@ -378,14 +378,18 @@
 def test_misdeclared_field_1():
     ffi = FFI()
     ffi.cdef("struct foo_s { int a[5]; };")
-    verify(ffi, 'test_misdeclared_field_1',
-           "struct foo_s { int a[6]; };")
-    assert ffi.sizeof("struct foo_s") == 24  # found by the actual C code
-    p = ffi.new("struct foo_s *")
-    # lazily build the fields and boom:
-    e = py.test.raises(ffi.error, "p.a")
-    assert str(e.value).startswith("struct foo_s: wrong size for field 'a' "
-                                   "(cdef says 20, but C compiler says 24)")
+    try:
+        verify(ffi, 'test_misdeclared_field_1',
+               "struct foo_s { int a[6]; };")
+    except VerificationError:
+        pass    # ok, fail during compilation already (e.g. C++)
+    else:
+        assert ffi.sizeof("struct foo_s") == 24  # found by the actual C code
+        p = ffi.new("struct foo_s *")
+        # lazily build the fields and boom:
+        e = py.test.raises(ffi.error, "p.a")
+        assert str(e.value).startswith("struct foo_s: wrong size for field 'a' "
+                                       "(cdef says 20, but C compiler says 24)")
 
 def test_open_array_in_struct():
     ffi = FFI()


More information about the pypy-commit mailing list