[pypy-commit] creflect default: in-progress

arigo noreply at buildbot.pypy.org
Thu Dec 11 13:41:18 CET 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r194:ee34a7bd891b
Date: 2014-12-11 11:37 +0000
http://bitbucket.org/cffi/creflect/changeset/ee34a7bd891b/

Log:	in-progress

diff --git a/zeffir/builder.c b/zeffir/builder.c
--- a/zeffir/builder.c
+++ b/zeffir/builder.c
@@ -411,18 +411,23 @@
 }
 
 static _crx_type_t *_zef_struct_or_union(_crx_builder_t *cb, const char *name,
-                                         int flag)
+                                         int flag, const char *prefix_name)
 {
     if (PyErr_Occurred())
         return NULL;
 
     PyObject *name_obj;
     CTypeDescrObject *ct;
+    size_t name_len = strlen(name);
+    size_t prefix_len = strlen(prefix_name);
 
-    name_obj = PyString_FromString(name);
+    name_obj = PyString_FromStringAndSize(NULL, name_len + prefix_len);
     if (name_obj == NULL)
         return NULL;
 
+    memcpy(PyString_AS_STRING(name_obj), prefix_name, prefix_len);
+    memcpy(PyString_AS_STRING(name_obj) + prefix_len, name, name_len);
+
     ct = get_cached_type(get_types_dict(cb), name_obj);
     if (ct && (ct->ct_flags == flag))
         goto done;
@@ -433,6 +438,9 @@
 
     ct->ct_flags = flag;
 
+    if (put_cached_type(get_types_dict(cb), name_obj, ct) < 0)
+        ct = NULL;
+
  done:
     Py_DECREF(name_obj);
     return ct;
@@ -440,12 +448,12 @@
 
 static _crx_type_t *zef_get_struct_type(_crx_builder_t *cb, const char *name)
 {
-    return _zef_struct_or_union(cb, name, CT_STRUCT);
+    return _zef_struct_or_union(cb, name, CT_STRUCT, "struct ");
 }
 
 static _crx_type_t *zef_get_union_type(_crx_builder_t *cb, const char *name)
 {
-    return _zef_struct_or_union(cb, name, CT_UNION);
+    return _zef_struct_or_union(cb, name, CT_UNION, "union ");
 }
 
 static _crx_type_t *zef_get_enum_type(_crx_builder_t *cb, const char *name)
diff --git a/zeffir/ctype.c b/zeffir/ctype.c
--- a/zeffir/ctype.c
+++ b/zeffir/ctype.c
@@ -112,33 +112,24 @@
 static PyObject *ctypeget_kind(CTypeDescrObject *ct, void *context)
 {
     char *result;
-    if (ct->ct_flags & CT_PRIMITIVE_ANY) {
-        if (ct->ct_flags & CT_IS_ENUM)
-            result = "enum";
-        else
-            result = "primitive";
-    }
-    else if (ct->ct_flags & CT_POINTER) {
+    if (ct->ct_flags & CT_IS_ENUM)
+        result = "enum";
+    else if (ct->ct_flags & CT_PRIMITIVE_ANY)
+        result = "primitive";
+    else if (ct->ct_flags & CT_POINTER)
         result = "pointer";
-    }
-    else if (ct->ct_flags & CT_ARRAY) {
+    else if (ct->ct_flags & CT_ARRAY)
         result = "array";
-    }
-    else if (ct->ct_flags & CT_VOID) {
+    else if (ct->ct_flags & CT_VOID)
         result = "void";
-    }
-    else if (ct->ct_flags & CT_STRUCT) {
+    else if (ct->ct_flags & CT_STRUCT)
         result = "struct";
-    }
-    else if (ct->ct_flags & CT_UNION) {
+    else if (ct->ct_flags & CT_UNION)
         result = "union";
-    }
-    else if (ct->ct_flags & CT_FUNCTION) {
+    else if (ct->ct_flags & CT_FUNCTION)
         result = "function";
-    }
-    else if (ct->ct_flags & CT_UNKNOWN) {
+    else if (ct->ct_flags & CT_UNKNOWN)
         result = "opaque";
-    }
     else
         result = "?";
 
@@ -208,9 +199,15 @@
 static PyObject *ctypeget_elements(CTypeDescrObject *ct, void *context)
 {
     if (ct->ct_flags & CT_IS_ENUM) {
-        PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1);
-        if (res) res = PyDict_Copy(res);
-        return res;
+        if (ct->ct_size >= 0) {
+            PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1);
+            if (res) res = PyDict_Copy(res);
+            return res;
+        }
+        else {
+            Py_INCREF(Py_None);
+            return Py_None;
+        }
     }
     return nosuchattr("elements");
 }
@@ -218,9 +215,15 @@
 static PyObject *ctypeget_relements(CTypeDescrObject *ct, void *context)
 {
     if (ct->ct_flags & CT_IS_ENUM) {
-        PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0);
-        if (res) res = PyDict_Copy(res);
-        return res;
+        if (ct->ct_size >= 0) {
+            PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0);
+            if (res) res = PyDict_Copy(res);
+            return res;
+        }
+        else {
+            Py_INCREF(Py_None);
+            return Py_None;
+        }
     }
     return nosuchattr("relements");
 }
diff --git a/zeffir/test/struct.crx b/zeffir/test/struct.crx
--- a/zeffir/test/struct.crx
+++ b/zeffir/test/struct.crx
@@ -3,7 +3,7 @@
     long b;
 } mystruct_t;
 
-typedef struct {
+typedef struct myarrstr_s {
     int foo[5];
     int bar[7];
     int baz[];
@@ -16,7 +16,7 @@
     int a, b;
 } mystruct_t;
 
-typedef struct {
+typedef struct myarrstr_s {
     int foo[5];
     int bar[];
     int baz[];
diff --git a/zeffir/test/test_struct.py b/zeffir/test/test_struct.py
--- a/zeffir/test/test_struct.py
+++ b/zeffir/test/test_struct.py
@@ -11,6 +11,13 @@
     p.b += 1
     assert p.b == 0x600112234
 
+def test_types():
+    ffi, lib = support.compile_and_open('struct')
+    assert 'mystruct_t' in ffi.types
+    assert 'struct myarrstr_s' in ffi.types
+    assert 'myarrstr_t' in ffi.types
+    ffi.new("struct myarrstr_s")
+
 def test_offsetof():
     ffi, lib = support.compile_and_open('struct')
     assert ffi.offsetof("mystruct_t", "a") == 0


More information about the pypy-commit mailing list