Armin Rigo avatar Armin Rigo committed 9aa3de9

Tweaks tweaks

Comments (0)

Files changed (2)

c/_cffi_backend.c

     return convert_from_object(result, ctype, pyobj);
 }
 
-static void _my_PyErr_WriteUnraisable(PyObject *obj)
+static void _my_PyErr_WriteUnraisable(PyObject *obj, char *extra_error_line)
 {
     /* like PyErr_WriteUnraisable(), but write a full traceback */
     PyObject *f, *t, *v, *tb;
         PyFile_WriteString("From callback ", f);
         PyFile_WriteObject(obj, f, 0);
         PyFile_WriteString(":\n", f);
+        if (extra_error_line != NULL)
+            PyFile_WriteString(extra_error_line, f);
         PyErr_Display(t, v, tb);
     }
     Py_XDECREF(t);
     PyObject *py_res = NULL;
     PyObject *py_rawerr;
     Py_ssize_t i, n;
+    char *extra_error_line = NULL;
 
 #define SIGNATURE(i)  ((CTypeDescrObject *)PyTuple_GET_ITEM(signature, i))
 
     py_res = PyEval_CallObject(py_ob, py_args);
     if (py_res == NULL)
         goto error;
-    if (convert_from_object_fficallback(result, SIGNATURE(1), py_res) < 0)
+    if (convert_from_object_fficallback(result, SIGNATURE(1), py_res) < 0) {
+        extra_error_line = "Trying to convert the result back to C:\n";
         goto error;
+    }
  done:
     Py_XDECREF(py_args);
     Py_XDECREF(py_res);
     return;
 
  error:
-    _my_PyErr_WriteUnraisable(py_ob);
+    _my_PyErr_WriteUnraisable(py_ob, extra_error_line);
     if (SIGNATURE(1)->ct_size > 0) {
         py_rawerr = PyTuple_GET_ITEM(cb_args, 2);
         memcpy(result, PyBytes_AS_STRING(py_rawerr),
     complete_struct_or_union(BStruct, [('a', BArray10, -1)])
     BFunc22 = new_function_type((BStruct, BStruct), BStruct, False)
     f = cast(BFunc22, _testfunc(22))
-    p1 = newp(BStructP, {'a': range(100, 110)})
-    p2 = newp(BStructP, {'a': range(1000, 1100, 10)})
+    p1 = newp(BStructP, {'a': list(range(100, 110))})
+    p2 = newp(BStructP, {'a': list(range(1000, 1100, 10))})
     res = f(p1[0], p2[0])
     for i in range(10):
         assert res.a[i] == p1.a[i] - p2.a[i]
     assert str(e.value) == "'int(*)(int)' expects 1 arguments, got 0"
 
 def test_callback_exception():
-    import cStringIO, linecache
+    try:
+        import cStringIO
+    except ImportError:
+        import io as cStringIO    # Python 3
+    import linecache
     def matches(str, pattern):
         while '$' in pattern:
             i = pattern.index('$')
     def check_value(x):
         if x == 10000:
             raise ValueError(42)
-    def cb1(x):
+    def Zcb1(x):
         check_value(x)
         return x * 3
     BShort = new_primitive_type("short")
     BFunc = new_function_type((BShort,), BShort, False)
-    f = callback(BFunc, cb1, -42)
+    f = callback(BFunc, Zcb1, -42)
     orig_stderr = sys.stderr
     orig_getline = linecache.getline
     try:
         assert sys.stderr.getvalue() == ''
         assert f(10000) == -42
         assert matches(sys.stderr.getvalue(), """\
-From callback <function cb1 at 0x$>:
+From callback <function$Zcb1 at 0x$>:
 Traceback (most recent call last):
-  File "$", line $, in cb1
+  File "$", line $, in Zcb1
     $
   File "$", line $, in check_value
     $
         bigvalue = 20000
         assert f(bigvalue) == -42
         assert matches(sys.stderr.getvalue(), """\
-From callback <function cb1 at 0x$>:
+From callback <function$Zcb1 at 0x$>:
+Trying to convert the result back to C:
 OverflowError: integer 60000 does not fit 'short'
 """)
     finally:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.