Author: Armin Rigo <[email protected]>
Branch:
Changeset: r399:f688bc624218
Date: 2012-06-17 11:44 +0200
http://bitbucket.org/cffi/cffi/changeset/f688bc624218/
Log: Remove the limitation about "typedef ... foo_t", and change the
test.
diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -85,7 +85,8 @@
decl)
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType)
and decl.type.type.names == ['__dotdotdot__']):
- realtype = model.OpaqueType(decl.name)
+ realtype = model.unknown_type(decl.name)
+ self._declare('anonymous ' + decl.name, realtype)
else:
realtype = self._get_type(decl.type, name=decl.name)
self._declare('typedef ' + decl.name, realtype)
diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -267,14 +267,8 @@
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)
+def unknown_type(name):
+ tp = StructType('$%s' % name, [], [], [])
+ tp.partial = True
+ tp.forcename = name
+ return tp
diff --git a/testing/test_verify.py b/testing/test_verify.py
--- a/testing/test_verify.py
+++ b/testing/test_verify.py
@@ -153,17 +153,6 @@
"int bar(foo_t *f) { return 42; }\n")
assert lib.bar(None) == 42
-def test_missing_typedef():
- ffi = FFI()
- ffi.cdef("typedef...foo_t; int bar(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")
- py.test.raises(TypeError, ffi.new, "foo_t")
- f = ffi.cast("foo_t*", 0)
- assert lib.bar(f) == 42
-
-
def test_ffi_full_struct():
ffi = FFI()
ffi.cdef("struct foo_s { char x; int y; long *z; };")
@@ -463,3 +452,20 @@
f = ffi.new("foo_t")
f.x = 6
assert lib.foo(f) == 42
+
+def test_unknown_type():
+ ffi = FFI()
+ ffi.cdef("typedef ... token_t; int foo(token_t *);")
+ lib = ffi.verify("""
+ typedef float token_t;
+ static int foo(token_t *tk) {
+ if (!tk)
+ return -42;
+ *tk += 1.601;
+ return (int)*tk;
+ }
+ """)
+ tk = ffi.new("token_t") # zero-initialized
+ results = [lib.foo(tk) for i in range(6)]
+ assert results == [1, 3, 4, 6, 8, 9]
+ assert lib.foo(None) == -42
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit