[pypy-commit] cffi cffi-1.0: Test and fix for the error message

arigo noreply at buildbot.pypy.org
Tue Apr 14 19:05:45 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1708:30a839e74f3d
Date: 2015-04-14 19:06 +0200
http://bitbucket.org/cffi/cffi/changeset/30a839e74f3d/

Log:	Test and fix for the error message

diff --git a/new/realize_c_type.c b/new/realize_c_type.c
--- a/new/realize_c_type.c
+++ b/new/realize_c_type.c
@@ -51,9 +51,13 @@
         PyObject *y;
         assert(PyTuple_Check(x));
         y = PyTuple_GET_ITEM(x, 0);
-        PyErr_Format(FFIError, "the type '%s' is a function type, not a "
-                               "pointer-to-function type",
-                     ((CTypeDescrObject *)y)->ct_name);
+        char *text1 = ((CTypeDescrObject *)y)->ct_name;
+        char *text2 = text1 + ((CTypeDescrObject *)y)->ct_name_position + 1;
+        assert(text2[-3] == '(');
+        text2[-3] = '\0';
+        PyErr_Format(FFIError, "the type '%s%s' is a function type, not a "
+                               "pointer-to-function type", text1, text2);
+        text2[-3] = '(';
         Py_DECREF(x);
         return NULL;
     }
diff --git a/new/test_realize_c_type.py b/new/test_realize_c_type.py
--- a/new/test_realize_c_type.py
+++ b/new/test_realize_c_type.py
@@ -1,11 +1,17 @@
+import py
 
 
-def check(input, expected_output=None):
+def check(input, expected_output=None, expected_ffi_error=False):
     import _cffi1_backend
     ffi = _cffi1_backend.FFI()
-    ct = ffi.typeof(input)
-    assert isinstance(ct, ffi.CType)
-    assert ct.cname == (expected_output or input)
+    if not expected_ffi_error:
+        ct = ffi.typeof(input)
+        assert isinstance(ct, ffi.CType)
+        assert ct.cname == (expected_output or input)
+    else:
+        e = py.test.raises(ffi.error, ffi.typeof, input)
+        if isinstance(expected_ffi_error, str):
+            assert str(e.value) == expected_ffi_error
 
 def test_void():
     check("void", "void")
@@ -26,3 +32,7 @@
 
 def test_funcptr():
     check("int(*)(long)")
+    check("int(long)", expected_ffi_error="the type 'int(long)' is a"
+          " function type, not a pointer-to-function type")
+    check("int(void)", expected_ffi_error="the type 'int()' is a"
+          " function type, not a pointer-to-function type")


More information about the pypy-commit mailing list