[pypy-commit] pypy cffi-1.0: enums, structs

arigo noreply at buildbot.pypy.org
Sat May 2 17:09:39 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r76978:0c05840fcfdc
Date: 2015-05-02 16:57 +0200
http://bitbucket.org/pypy/pypy/changeset/0c05840fcfdc/

Log:	enums, structs

diff --git a/pypy/module/_cffi_backend/parse_c_type.py b/pypy/module/_cffi_backend/parse_c_type.py
--- a/pypy/module/_cffi_backend/parse_c_type.py
+++ b/pypy/module/_cffi_backend/parse_c_type.py
@@ -17,7 +17,36 @@
     return rffi.llexternal(name, args, result, compilation_info=eci, **kwds)
 
 
-PCTX = rffi.CStructPtr('struct _cffi_type_context_s')
+GLOBAL_S = rffi.CStruct('struct _cffi_global_s')
+STRUCT_UNION_S = rffi.CStruct('struct _cffi_struct_union_s',
+                       ('name', rffi.CCHARP),
+                       ('type_index', rffi.INT),
+                       ('flags', rffi.INT),
+                       ('size', rffi.SIZE_T),
+                       ('alignment', rffi.INT),
+                       ('first_field_index', rffi.INT),
+                       ('num_fields', rffi.INT))
+FIELD_S = rffi.CStruct('struct _cffi_field_s')
+ENUM_S = rffi.CStruct('struct _cffi_enum_s',
+                       ('name', rffi.CCHARP),
+                       ('type_index', rffi.INT),
+                       ('type_prim', rffi.INT),
+                       ('enumerators', rffi.CCHARP))
+TYPENAME_S = rffi.CStruct('struct _cffi_typename_s')
+
+PCTX = rffi.CStructPtr('struct _cffi_type_context_s',
+                       ('types', rffi.VOIDPP),
+                       ('globals', rffi.CArrayPtr(GLOBAL_S)),
+                       ('fields', rffi.CArrayPtr(FIELD_S)),
+                       ('struct_unions', rffi.CArrayPtr(STRUCT_UNION_S)),
+                       ('enums', rffi.CArrayPtr(ENUM_S)),
+                       ('typenames', rffi.CArrayPtr(TYPENAME_S)),
+                       ('num_globals', rffi.INT),
+                       ('num_struct_unions', rffi.INT),
+                       ('num_enums', rffi.INT),
+                       ('num_typenames', rffi.INT),
+                       ('includes', rffi.CCHARPP))
+
 PINFO = rffi.CStructPtr('struct _cffi_parse_info_s',
                         ('ctx', PCTX),
                         ('output', rffi.VOIDPP),
diff --git a/pypy/module/_cffi_backend/test/test_parse_c_type.py b/pypy/module/_cffi_backend/test/test_parse_c_type.py
--- a/pypy/module/_cffi_backend/test/test_parse_c_type.py
+++ b/pypy/module/_cffi_backend/test/test_parse_c_type.py
@@ -6,33 +6,39 @@
 class ParseError(Exception):
     pass
 
-## struct_names = ["bar_s", "foo", "foo_", "foo_s", "foo_s1", "foo_s12"]
-## assert struct_names == sorted(struct_names)
+struct_names = ["bar_s", "foo", "foo_", "foo_s", "foo_s1", "foo_s12"]
+assert struct_names == sorted(struct_names)
 
-## enum_names = ["ebar_s", "efoo", "efoo_", "efoo_s", "efoo_s1", "efoo_s12"]
-## assert enum_names == sorted(enum_names)
+enum_names = ["ebar_s", "efoo", "efoo_", "efoo_s", "efoo_s1", "efoo_s12"]
+assert enum_names == sorted(enum_names)
 
-## identifier_names = ["id", "id0", "id05", "id05b", "tail"]
-## assert identifier_names == sorted(identifier_names)
+identifier_names = ["id", "id0", "id05", "id05b", "tail"]
+assert identifier_names == sorted(identifier_names)
 
-## global_names = ["FIVE", "NEG", "ZERO"]
-## assert global_names == sorted(global_names)
+global_names = ["FIVE", "NEG", "ZERO"]
+assert global_names == sorted(global_names)
 
-## ctx = ffi.new("struct _cffi_type_context_s *")
-## c_struct_names = [ffi.new("char[]", _n.encode('ascii')) for _n in struct_names]
-## ctx_structs = ffi.new("struct _cffi_struct_union_s[]", len(struct_names))
-## for _i in range(len(struct_names)):
-##     ctx_structs[_i].name = c_struct_names[_i]
-## ctx_structs[3].flags = lib._CFFI_F_UNION
-## ctx.struct_unions = ctx_structs
-## ctx.num_struct_unions = len(struct_names)
+ctx = lltype.malloc(parse_c_type.PCTX.TO, flavor='raw', zero=True,
+                    track_allocation=False)
 
-## c_enum_names = [ffi.new("char[]", _n.encode('ascii')) for _n in enum_names]
-## ctx_enums = ffi.new("struct _cffi_enum_s[]", len(enum_names))
-## for _i in range(len(enum_names)):
-##     ctx_enums[_i].name = c_enum_names[_i]
-## ctx.enums = ctx_enums
-## ctx.num_enums = len(enum_names)
+c_struct_names = [rffi.str2charp(_n.encode('ascii')) for _n in struct_names]
+ctx_structs = lltype.malloc(rffi.CArray(parse_c_type.STRUCT_UNION_S),
+                            len(struct_names), flavor='raw', zero=True,
+                            track_allocation=False)
+for _i in range(len(struct_names)):
+    ctx_structs[_i].c_name = c_struct_names[_i]
+rffi.setintfield(ctx_structs[3], 'c_flags', cffi_opcode.F_UNION)
+ctx.c_struct_unions = ctx_structs
+rffi.setintfield(ctx, 'c_num_struct_unions', len(struct_names))
+
+c_enum_names = [rffi.str2charp(_n.encode('ascii')) for _n in enum_names]
+ctx_enums = lltype.malloc(rffi.CArray(parse_c_type.ENUM_S),
+                            len(enum_names), flavor='raw', zero=True,
+                            track_allocation=False)
+for _i in range(len(enum_names)):
+    ctx_enums[_i].c_name = c_enum_names[_i]
+ctx.c_enums = ctx_enums
+rffi.setintfield(ctx, 'c_num_enums', len(enum_names))
 
 ## c_identifier_names = [ffi.new("char[]", _n.encode('ascii'))
 ##                       for _n in identifier_names]
@@ -69,15 +75,12 @@
 ## ctx.globals = ctx_globals
 ## ctx.num_globals = len(global_names)
 
-ctx = lltype.malloc(parse_c_type.PCTX.TO, flavor='raw', zero=True,
-                    track_allocation=False)
-
 
 def parse(input):
     OUTPUT_SIZE = 100
     out = lltype.malloc(rffi.VOIDPP.TO, OUTPUT_SIZE, flavor='raw',
                         track_allocation=False)
-    info = lltype.malloc(parse_c_type.PINFO.TO, flavor='raw',
+    info = lltype.malloc(parse_c_type.PINFO.TO, flavor='raw', zero=True,
                         track_allocation=False)
     info.c_ctx = ctx
     info.c_output = out


More information about the pypy-commit mailing list