Commits

Armin Rigo committed 758d9da

Fix issue #44: ffi.new/cast("undefined") raises confusing exception.

Comments (0)

Files changed (3)

                     self._declare('variable ' + decl.name, tp)
 
     def parse_type(self, cdecl, consider_function_as_funcptr=False):
-        ast, macros = self._parse('void __dummy(%s);' % cdecl)
+        ast, macros = self._parse('void __dummy(\n%s\n);' % cdecl)
         assert not macros
-        typenode = ast.ext[-1].type.args.params[0].type
-        return self._get_type(typenode,
+        exprnode = ast.ext[-1].type.args.params[0]
+        if isinstance(exprnode, pycparser.c_ast.ID):
+            raise api.CDefError("unknown identifier '%s'" % (exprnode.name,))
+        return self._get_type(exprnode.type,
                      consider_function_as_funcptr=consider_function_as_funcptr)
 
     def _declare(self, name, obj):

testing/backend_tests.py

         ffi1 = FFI(backend=backend)
         ffi2 = FFI(backend=backend)
         ffi1.cdef("typedef signed char schar_t;")
-        py.test.raises((AttributeError, TypeError), ffi2.cast, "schar_t", 142)
+        py.test.raises(CDefError, ffi2.cast, "schar_t", 142)
 
     def test_include_typedef(self):
         backend = self.Backend()

testing/test_parsing.py

     assert str(e.value) == (
            "enum foo_e: the '{}' declaration should appear on the "
            "first time the enum is mentioned, not later")
+
+def test_unknown_name():
+    ffi = FFI()
+    e = py.test.raises(CDefError, ffi.cast, "foobarbazunknown", 0)
+    assert str(e.value) == "unknown identifier 'foobarbazunknown'"
+    e = py.test.raises(CDefError, ffi.cast, "foobarbazunknown*", 0)
+    assert str(e.value).startswith('cannot parse "foobarbazunknown*"')
+    e = py.test.raises(CDefError, ffi.cast, "int(*)(foobarbazunknown)", 0)
+    assert str(e.value).startswith('cannot parse "int(*)(foobarbazunknown)"')