[pypy-commit] cffi default: Tweak the repr of cdata objects in an attempt to reduce a bit confusion.

arigo noreply at buildbot.pypy.org
Thu Jul 12 12:32:59 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r630:fd33636aebec
Date: 2012-07-12 12:32 +0200
http://bitbucket.org/cffi/cffi/changeset/fd33636aebec/

Log:	Tweak the repr of cdata objects in an attempt to reduce a bit
	confusion.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1097,7 +1097,7 @@
 
 static PyObject *cdata_repr(CDataObject *cd)
 {
-    char *p;
+    char *p, *extra;
     PyObject *result, *s = NULL;
 
     if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) {
@@ -1120,7 +1120,15 @@
         else
             p = "NULL";
     }
-    result = PyString_FromFormat("<cdata '%s' %s>", cd->c_type->ct_name, p);
+    /* it's slightly confusing to get "<cdata 'struct foo' 0x...>" because the
+       struct foo is not owned.  Trying to make it clearer, write in this
+       case "<cdata 'struct foo &' 0x...>". */
+    if (cd->c_type->ct_flags & (CT_STRUCT|CT_UNION))
+        extra = " &";
+    else
+        extra = "";
+    result = PyString_FromFormat("<cdata '%s%s' %s>",
+                                 cd->c_type->ct_name, extra, p);
     Py_XDECREF(s);
     return result;
 }
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1422,6 +1422,8 @@
     assert repr(q) == "<cdata 'struct foo' owning 12 bytes>"
     q.a1 = 123456
     assert p.a1 == 123456
+    r = cast(BStructPtr, p)
+    assert repr(r[0]).startswith("<cdata 'struct foo &' 0x")
     del p
     import gc; gc.collect()
     assert q.a1 == 123456
@@ -1437,7 +1439,7 @@
     pp = newp(BStructPtrPtr)
     pp[0] = p
     s = pp[0][0]
-    assert repr(s).startswith("<cdata 'struct foo' 0x")
+    assert repr(s).startswith("<cdata 'struct foo &' 0x")
 
 def test_owning_repr():
     BInt = new_primitive_type("int")


More information about the pypy-commit mailing list