Author: Armin Rigo <[email protected]>
Branch: cffi-1.0
Changeset: r1961:22eec64f10aa
Date: 2015-05-10 13:11 +0200
http://bitbucket.org/cffi/cffi/changeset/22eec64f10aa/

Log:    Hack hack hack to support test_include_5

diff --git a/_cffi1/realize_c_type.c b/_cffi1/realize_c_type.c
--- a/_cffi1/realize_c_type.c
+++ b/_cffi1/realize_c_type.c
@@ -329,7 +329,10 @@
             if (s->first_field_index >= 0) {
                 ct = (CTypeDescrObject *)x;
                 ct->ct_size = (Py_ssize_t)s->size;
-                ct->ct_length = s->alignment;
+                if (s->alignment == 0)
+                    ct->ct_length = 1;    /* guess; should not really matter */
+                else
+                    ct->ct_length = s->alignment;
                 ct->ct_flags &= ~CT_IS_OPAQUE;
                 ct->ct_flags |= CT_LAZY_FIELD_LIST;
                 ct->ct_extra = builder;
@@ -677,7 +680,7 @@
 
         args = Py_BuildValue("(OOOnni)", ct, fields, Py_None,
                              (Py_ssize_t)s->size,
-                             (Py_ssize_t)s->alignment,
+                             s->alignment ? (Py_ssize_t)s->alignment : -1,
                              sflags);
         Py_DECREF(fields);
         if (args == NULL)
diff --git a/_cffi1/recompiler.py b/_cffi1/recompiler.py
--- a/_cffi1/recompiler.py
+++ b/_cffi1/recompiler.py
@@ -360,7 +360,8 @@
         if getattr(tp, "origin", None) == "unknown_type":
             self._struct_ctx(tp, tp.name, approxname=None)
         elif isinstance(tp, model.NamedPointerType):
-            self._struct_ctx(tp.totype, tp.totype.name, approxname=None)
+            self._struct_ctx(tp.totype, tp.totype.name, approxname=tp.name,
+                             named_ptr=tp)
 
     # ----------
     # function declarations
@@ -543,13 +544,15 @@
         prnt('struct _cffi_align_%s { char x; %s y; };' % (approxname, cname))
         prnt()
 
-    def _struct_ctx(self, tp, cname, approxname):
+    def _struct_ctx(self, tp, cname, approxname, named_ptr=None):
         type_index = self._typesdict[tp]
         reason_for_not_expanding = None
         flags = []
         if isinstance(tp, model.UnionType):
             flags.append("_CFFI_F_UNION")
-        if tp not in self.ffi._parser._included_declarations:
+        if (tp not in self.ffi._parser._included_declarations and
+                (named_ptr is None or
+                 named_ptr not in self.ffi._parser._included_declarations)):
             if tp.fldtypes is None:
                 reason_for_not_expanding = "opaque"
             elif tp.partial or tp.has_anonymous_struct_fields():
@@ -578,9 +581,15 @@
                         fldtype.length is None):
                     size = '(size_t)-1'
                 else:
-                    size = 'sizeof(((%s)0)->%s)' % (tp.get_c_name('*'), 
fldname)
+                    size = 'sizeof(((%s)0)->%s)' % (
+                        tp.get_c_name('*') if named_ptr is None
+                                           else named_ptr.name,
+                        fldname)
                 if cname is None or fbitsize >= 0:
                     offset = '(size_t)-1'
+                elif named_ptr is not None:
+                    offset = '((char *)&((%s)0)->%s) - (char *)0' % (
+                        named_ptr.name, fldname)
                 else:
                     offset = 'offsetof(%s, %s)' % (tp.get_c_name(''), fldname)
                 c_field.append(
@@ -595,9 +604,15 @@
                     '    _cffi_FIELDS_FOR_%s, %d },' % (approxname,
                                                         len(enumfields),))
             else:
+                if named_ptr is not None:
+                    size = 'sizeof(*(%s)0)' % (named_ptr.name,)
+                    align = '0  /* unknown */'
+                else:
+                    size = 'sizeof(%s)' % (cname,)
+                    align = 'offsetof(struct _cffi_align_%s, y)' % 
(approxname,)
                 size_align = ('\n' +
-                    '    sizeof(%s),\n' % (cname,) +
-                    '    offsetof(struct _cffi_align_%s, y),\n'% (approxname,) 
+
+                    '    %s,\n' % (size,) +
+                    '    %s,\n' % (align,) +
                     '    _cffi_FIELDS_FOR_%s, %d },' % (approxname,
                                                         len(enumfields),))
         else:
diff --git a/_cffi1/test_recompiler.py b/_cffi1/test_recompiler.py
--- a/_cffi1/test_recompiler.py
+++ b/_cffi1/test_recompiler.py
@@ -499,17 +499,20 @@
     ffi.cdef("foo_t ff1(foo_t);")
     lib = verify(ffi, "test_include_1", "double ff1(double x) { return 42.5; 
}")
     assert lib.ff1(0) == 42.5
+    assert ffi1.typeof("foo_t") is ffi.typeof("foo_t") is ffi.typeof("double")
 
 def test_include_1b():
     ffi1 = FFI()
     ffi1.cdef("int foo1(int);")
-    verify(ffi1, "test_include_1b_parent", "int foo1(int x) { return x + 10; 
}")
+    lib1 = verify(ffi1, "test_include_1b_parent",
+                  "int foo1(int x) { return x + 10; }")
     ffi = FFI()
     ffi.include(ffi1)
     ffi.cdef("int foo2(int);")
     lib = verify(ffi, "test_include_1b", "int foo2(int x) { return x - 5; }")
     assert lib.foo2(42) == 37
     assert lib.foo1(42) == 52
+    assert lib.foo1 is lib1.foo1
 
 def test_include_2():
     ffi1 = FFI()
@@ -526,6 +529,7 @@
     q = lib.ff2(p)
     assert q == p
     assert p.y == 42
+    assert ffi1.typeof("struct foo_s") is ffi.typeof("struct foo_s")
 
 def test_include_3():
     ffi1 = FFI()
@@ -539,6 +543,7 @@
                  "sshort_t ff3(sshort_t x) { return x + 42; }")
     assert lib.ff3(10) == 52
     assert ffi.typeof(ffi.cast("sshort_t", 42)) is ffi.typeof("short")
+    assert ffi1.typeof("sshort_t") is ffi.typeof("sshort_t")
 
 def test_include_4():
     ffi1 = FFI()
@@ -555,23 +560,27 @@
     q = lib.ff4(p)
     assert q == p
     assert p.x == 52
+    assert ffi1.typeof("mystruct_t") is ffi.typeof("mystruct_t")
 
 def test_include_5():
-    py.test.xfail("also fails in 0.9.3")
     ffi1 = FFI()
-    ffi1.cdef("typedef struct { int x; } *mystruct_p;")
+    ffi1.cdef("typedef struct { int x[2]; int y; } *mystruct_p;")
     verify(ffi1, "test_include_5_parent",
-           "typedef struct { int x; } *mystruct_p;")
+           "typedef struct { int x[2]; int y; } *mystruct_p;")
     ffi = FFI()
     ffi.include(ffi1)
     ffi.cdef("mystruct_p ff5(mystruct_p);")
     lib = verify(ffi, "test_include_5",
-           "typedef struct {int x; } *mystruct_p; //usually from a #include\n"
-           "mystruct_p ff5(mystruct_p p) { p->x += 42; return p; }")
-    p = ffi.new("mystruct_p", [10])
+        "typedef struct {int x[2]; int y; } *mystruct_p; //usually #include\n"
+        "mystruct_p ff5(mystruct_p p) { p->x[1] += 42; return p; }")
+    assert ffi1.typeof("mystruct_p") is ffi.typeof("mystruct_p")
+    p = ffi.new("mystruct_p", [[5, 10], -17])
     q = lib.ff5(p)
     assert q == p
-    assert p.x == 52
+    assert p.x[0] == 5
+    assert p.x[1] == 52
+    assert p.y == -17
+    assert ffi.alignof(ffi.typeof(p[0])) == 4
 
 def test_include_6():
     ffi1 = FFI()
diff --git a/_cffi1/test_verify1.py b/_cffi1/test_verify1.py
--- a/_cffi1/test_verify1.py
+++ b/_cffi1/test_verify1.py
@@ -1266,9 +1266,9 @@
 
 def test_cannot_name_struct_type():
     ffi = FFI()
-    ffi.cdef("typedef struct { int x; } *sp; void foo(sp);")
+    ffi.cdef("typedef struct { int x; } **sp; void foo(sp);")
     e = py.test.raises(VerificationError, ffi.verify,
-                       "typedef struct { int x; } *sp; void foo(sp);")
+                       "typedef struct { int x; } **sp; void foo(sp x) { }")
     assert 'in argument of foo: unknown type name' in str(e.value)
 
 def test_dont_check_unnamable_fields():
@@ -1703,6 +1703,17 @@
     res = lib2.myfunc(lib2.AA)
     assert res == 2
 
+def test_named_pointer_as_argument():
+    ffi = FFI()
+    ffi.cdef("typedef struct { int x; } *mystruct_p;\n"
+             "mystruct_p ff5a(mystruct_p);")
+    lib = ffi.verify("typedef struct { int x; } *mystruct_p;\n"
+                     "mystruct_p ff5a(mystruct_p p) { p->x += 40; return p; }")
+    p = ffi.new("mystruct_p", [-2])
+    q = lib.ff5a(p)
+    assert q == p
+    assert p.x == 38
+
 def test_enum_size():
     cases = [('123',           4, 4294967295),
              ('4294967295U',   4, 4294967295),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to