[pypy-commit] cffi default: Issue #363

arigo pypy.commits at gmail.com
Mon Apr 16 11:40:16 EDT 2018


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r3117:17c94f9cb463
Date: 2018-04-16 17:40 +0200
http://bitbucket.org/cffi/cffi/changeset/17c94f9cb463/

Log:	Issue #363

	Improve the error message for ffi.new(_, XX)

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1288,7 +1288,13 @@
         Py_ssize_t explicitlength;
         explicitlength = PyNumber_AsSsize_t(value, PyExc_OverflowError);
         if (explicitlength < 0) {
-            if (!PyErr_Occurred())
+            if (PyErr_Occurred()) {
+                if (PyErr_ExceptionMatches(PyExc_TypeError))
+                    PyErr_Format(PyExc_TypeError,
+                        "expected new array length or list/tuple/str, "
+                        "not %.200s", Py_TYPE(value)->tp_name);
+            }
+            else
                 PyErr_SetString(PyExc_ValueError, "negative array length");
             return -1;
         }
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -289,6 +289,9 @@
     assert repr(q).startswith("<cdata 'int *' 0x")
     assert p == q
     assert hash(p) == hash(q)
+    e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), None)
+    assert str(e.value) == (
+        "expected new array length or list/tuple/str, not NoneType")
 
 def test_pointer_bool():
     BInt = new_primitive_type("int")
@@ -370,6 +373,9 @@
     assert int(c) == ord(b'A')
     py.test.raises(TypeError, cast, BChar, b'foo')
     py.test.raises(TypeError, cast, BChar, u+'foo')
+    e = py.test.raises(TypeError, newp, new_array_type(BPtr, None), 12.3)
+    assert str(e.value) == (
+        "expected new array length or list/tuple/str, not float")
 
 def test_reading_pointer_to_pointer():
     BVoidP = new_pointer_type(new_void_type())


More information about the pypy-commit mailing list