[pypy-commit] cffi cffi-1.0: give the expected name to $struct and $enums

arigo noreply at buildbot.pypy.org
Sun Apr 26 10:13:37 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r1830:c92ee3ee4642
Date: 2015-04-26 09:58 +0200
http://bitbucket.org/cffi/cffi/changeset/c92ee3ee4642/

Log:	give the expected name to $struct and $enums

diff --git a/_cffi1/realize_c_type.c b/_cffi1/realize_c_type.c
--- a/_cffi1/realize_c_type.c
+++ b/_cffi1/realize_c_type.c
@@ -288,6 +288,38 @@
     }
 }
 
+static void _realize_name(char *target, const char *prefix, const char *srcname)
+{
+    /* "xyz" => "struct xyz"
+       "$xyz" => "xyz"
+    */
+    if (srcname[0] == '$' && srcname[1] != '$') {
+        strcpy(target, &srcname[1]);
+    }
+    else {
+        strcpy(target, prefix);
+        strcat(target, srcname);
+    }
+}
+
+static void _unrealize_name(char *target, const char *srcname)
+{
+    /* reverse of _realize_name() */
+    if (strncmp(srcname, "struct ", 7) == 0) {
+        strcpy(target, &srcname[7]);
+    }
+    else if (strncmp(srcname, "union ", 6) == 0) {
+        strcpy(target, &srcname[6]);
+    }
+    else if (strncmp(srcname, "enum ", 5) == 0) {
+        strcpy(target, &srcname[5]);
+    }
+    else {
+        strcpy(target, "$");
+        strcat(target, srcname);
+    }
+}
+
 static PyObject *
 _realize_c_type_or_func(builder_c_t *builder,
                         _cffi_opcode_t opcodes[], int index)
@@ -354,17 +386,11 @@
             Py_INCREF(x);
         }
         else {
-            int flags;
+            int flags = (s->flags & CT_UNION) ? CT_UNION : CT_STRUCT;
             char *name = alloca(8 + strlen(s->name));
-            if (s->flags & CT_UNION) {
-                strcpy(name, "union ");
-                flags = CT_UNION;
-            }
-            else {
-                strcpy(name, "struct ");
-                flags = CT_STRUCT;
-            }
-            strcat(name, s->name);
+            _realize_name(name,
+                          (s->flags & CT_UNION) ? "union " : "struct ",
+                          s->name);
             x = new_struct_or_union_type(name, flags);
 
             CTypeDescrObject *ct = NULL;
@@ -462,8 +488,7 @@
             PyObject *args = NULL;
             if (!PyErr_Occurred()) {
                 char *name = alloca(6 + strlen(e->name));
-                strcpy(name, "enum ");
-                strcat(name, e->name);
+                _realize_name(name, "enum ", e->name);
                 args = Py_BuildValue("(sOOO)", name, enumerators,
                                      enumvalues, basetd);
             }
@@ -577,11 +602,8 @@
         builder_c_t *builder = ct->ct_extra;
         assert(builder != NULL);
 
-        char *p = ct->ct_name;
-        if (memcmp(p, "struct ", 7) == 0)
-            p += 7;
-        else if (memcmp(p, "union ", 6) == 0)
-            p += 6;
+        char *p = alloca(2 + strlen(ct->ct_name));
+        _unrealize_name(p, ct->ct_name);
 
         int n = search_in_struct_unions(&builder->ctx, p, strlen(p));
         if (n < 0)
diff --git a/_cffi1/test_recompiler.py b/_cffi1/test_recompiler.py
--- a/_cffi1/test_recompiler.py
+++ b/_cffi1/test_recompiler.py
@@ -349,7 +349,7 @@
            "typedef struct { long b; int hidden, a; } foo_t;")
     p = ffi.new("foo_t *", {'b': 42})
     assert p.b == 42
-    assert repr(p).startswith("<cdata 'struct $foo_t *' ")
+    assert repr(p).startswith("<cdata 'foo_t *' ")
 
 def test_verify_anonymous_struct_with_star_typedef():
     ffi = FFI()
@@ -366,7 +366,7 @@
                  "typedef enum { BB, CC, AA } e1;")
     assert lib.AA == 2
     assert ffi.sizeof("e1") == ffi.sizeof("int")
-    assert repr(ffi.cast("e1", 2)) == "<cdata 'enum $e1' 2: AA>"
+    assert repr(ffi.cast("e1", 2)) == "<cdata 'e1' 2: AA>"
     #
     ffi = FFI()
     ffi.cdef("typedef enum { AA=%d } e1;" % sys.maxint)


More information about the pypy-commit mailing list