Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r2122:7aea388413bd
Date: 2015-05-28 13:56 +0200
http://bitbucket.org/cffi/cffi/changeset/7aea388413bd/

Log:    more about issue 198: Test and fix

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1232,8 +1232,11 @@
 {
     const char *expected;
 
-    if (force_lazy_struct(ct) < 0)
+    if (force_lazy_struct(ct) <= 0) {
+        if (!PyErr_Occurred())
+            PyErr_Format(PyExc_TypeError, "'%s' is opaque", ct->ct_name);
         return -1;
+    }
 
     if (ct->ct_flags & CT_UNION) {
         Py_ssize_t n = PyObject_Size(init);
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
@@ -861,3 +861,23 @@
                        'test_constant_of_unknown_size', "stuff")
     assert str(e.value) == ("constant CONSTANT: constant 'CONSTANT' is of "
                             "type 'opaque_t', whose size is not known")
+
+def test_variable_of_unknown_size():
+    ffi = FFI()
+    ffi.cdef("""
+        typedef ... opaque_t;
+        opaque_t globvar;
+    """)
+    lib = verify(ffi, 'test_constant_of_unknown_size', """
+        typedef char opaque_t[6];
+        opaque_t globvar = "hello";
+    """)
+    # can't read or write it at all
+    e = py.test.raises(TypeError, getattr, lib, 'globvar')
+    assert str(e.value) == "cdata 'opaque_t' is opaque"
+    e = py.test.raises(TypeError, setattr, lib, 'globvar', [])
+    assert str(e.value) == "'opaque_t' is opaque"
+    # but we can get its address
+    p = ffi.addressof(lib, 'globvar')
+    assert ffi.typeof(p) == ffi.typeof('opaque_t *')
+    assert ffi.string(ffi.cast("char *", p), 8) == "hello"
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to