[pypy-commit] cffi cffi-1.0: cdef() unions
arigo
noreply at buildbot.pypy.org
Wed Apr 22 16:13:12 CEST 2015
Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1777:f19eb734b709
Date: 2015-04-22 16:13 +0200
http://bitbucket.org/cffi/cffi/changeset/f19eb734b709/
Log: cdef() unions
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -571,9 +571,12 @@
struct_unions = []
pending_completion = []
for name, tp in sorted(ffi._parser._declarations.items()):
- if name.startswith('struct '):
- basename = name[7:]
- BType = _cffi1_backend.new_struct_type(basename)
+ kind, basename = name.split(' ', 1)
+ if kind == 'struct' or kind == 'union':
+ if kind == 'struct':
+ BType = _cffi1_backend.new_struct_type(basename)
+ else:
+ BType = _cffi1_backend.new_union_type(basename)
struct_unions.append(basename)
struct_unions.append(BType)
if not tp.partial and tp.fldtypes is not None:
diff --git a/new/ffi_obj.c b/new/ffi_obj.c
--- a/new/ffi_obj.c
+++ b/new/ffi_obj.c
@@ -573,14 +573,14 @@
goto bad_usage;
struct_unions[i].name = PyString_AS_STRING(x);
struct_unions[i].type_index = i;
- //struct_unions[i].flags = ...;
- struct_unions[i].size = (size_t)-2;
- struct_unions[i].alignment = -2;
x = PyList_GET_ITEM(lst1, i * 2 + 1);
if (!CTypeDescr_Check(x))
goto bad_usage;
types[i] = x;
+ struct_unions[i].flags = ((CTypeDescrObject *)x)->ct_flags & CT_UNION;
+ struct_unions[i].size = (size_t)-2;
+ struct_unions[i].alignment = -2;
}
for (i = 0; i < lst_length; i++) {
PyObject *x = (PyObject *)types[i];
diff --git a/new/test_dlopen.py b/new/test_dlopen.py
--- a/new/test_dlopen.py
+++ b/new/test_dlopen.py
@@ -8,6 +8,11 @@
ffi.cdef("struct foo_s { int a, b; };")
assert ffi.sizeof("struct foo_s") == 8
+def test_cdef_union():
+ ffi = FFI()
+ ffi.cdef("union foo_s { int a, b; };")
+ assert ffi.sizeof("union foo_s") == 4
+
def test_math_sin():
py.test.skip("XXX redo!")
ffi = FFI()
More information about the pypy-commit
mailing list