[pypy-commit] cffi cpy-extension: "Opaque" types, which become just non-filled structs in the backend.
arigo
noreply at buildbot.pypy.org
Thu Jun 14 12:20:21 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch: cpy-extension
Changeset: r325:f8d43eca9931
Date: 2012-06-14 12:20 +0200
http://bitbucket.org/cffi/cffi/changeset/f8d43eca9931/
Log: "Opaque" types, which become just non-filled structs in the backend.
Used for the syntax "typedef ... foo_t;".
diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -57,8 +57,12 @@
if not decl.name:
raise api.CDefError("typedef does not declare any name",
decl)
- self._declare('typedef ' + decl.name,
- self._get_type(decl.type))
+ if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType)
+ and decl.type.type.names == ['__dotdotdot__']):
+ realtype = model.OpaqueType(decl.name)
+ else:
+ realtype = self._get_type(decl.type)
+ self._declare('typedef ' + decl.name, realtype)
else:
raise api.CDefError("unrecognized construct", decl)
diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -251,3 +251,16 @@
self.check_not_partial()
return ffi._backend.new_enum_type(self.name, self.enumerators,
self.enumvalues)
+
+
+class OpaqueType(BaseType):
+ _attrs_ = ('name',)
+
+ def __init__(self, name):
+ self.name = name
+
+ def get_c_name(self, replace_with=''):
+ return self.name + replace_with
+
+ def new_backend_type(self, ffi):
+ return ffi._backend.new_struct_type('$' + self.name)
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -146,13 +146,13 @@
assert lib.bar(None) == 42
def test_missing_typedef():
- py.test.skip("in-progress")
ffi = FFI()
ffi.cdef("typedef ... foo_t; int bar(foo_t *);")
- py.test.raises(VerificationMissing, ffi.new, "foo_t")
+ py.test.raises(TypeError, ffi.new, "foo_t")
lib = ffi.verify("typedef struct foo_s { int x; } foo_t;\n"
"int bar(foo_t *f) { return 42; }\n")
- f = ffi.new("foo_t")
+ py.test.raises(TypeError, ffi.new, "foo_t")
+ f = ffi.cast("foo_t*", 0)
assert lib.bar(f) == 42
More information about the pypy-commit
mailing list