[pypy-commit] cffi default: Fix str() to default to exactly repr(), not cdata_repr().

arigo noreply at buildbot.pypy.org
Mon Jul 9 16:59:07 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r604:a5d95e8f69f6
Date: 2012-07-09 16:02 +0200
http://bitbucket.org/cffi/cffi/changeset/a5d95e8f69f6/

Log:	Fix str() to default to exactly repr(), not cdata_repr().

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1098,7 +1098,7 @@
     else if (cd->c_type->ct_flags & CT_IS_ENUM)
         return convert_to_object(cd->c_data, cd->c_type);
     else
-        return cdata_repr(cd);
+        return Py_TYPE(cd)->tp_repr((PyObject *)cd);
 }
 
 static PyObject *cdataowning_repr(CDataObject *cd)
@@ -1589,9 +1589,12 @@
 
  bad_number_of_arguments:
     {
-        PyObject *s = cdata_repr(cd);
-        PyErr_Format(PyExc_TypeError, errormsg,
-                     PyString_AsString(s), nargs_declared, nargs);
+        PyObject *s = Py_TYPE(cd)->tp_repr((PyObject *)cd);
+        if (s != NULL) {
+            PyErr_Format(PyExc_TypeError, errormsg,
+                         PyString_AS_STRING(s), nargs_declared, nargs);
+            Py_DECREF(s);
+        }
         goto error;
     }
 
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -293,8 +293,11 @@
     py.test.raises(TypeError, "p[0]")
 
 def test_default_str():
-    p = new_primitive_type("int")
-    x = cast(p, 42)
+    BInt = new_primitive_type("int")
+    x = cast(BInt, 42)
+    assert str(x) == repr(x)
+    BArray = new_array_type(new_pointer_type(BInt), 10)
+    x = newp(BArray, None)
     assert str(x) == repr(x)
 
 def test_cast_from_cdataint():
@@ -847,6 +850,8 @@
     assert f(-142) == -141
     assert repr(f).startswith(
         "<cdata 'int(*)(int)' calling <function cb at 0x")
+    e = py.test.raises(TypeError, f)
+    assert str(e.value) == "%r expects 1 arguments, got 0" % (f,)
 
 def test_callback_return_type():
     for rettype in ["signed char", "short", "int", "long", "long long",


More information about the pypy-commit mailing list