Author: Armin Rigo <[email protected]>
Branch:
Changeset: r2205:bdbb14a1b774
Date: 2015-07-04 22:44 +0200
http://bitbucket.org/cffi/cffi/changeset/bdbb14a1b774/
Log: 'onerror' can also return a custom value to return
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -4802,13 +4802,18 @@
val1 ? val1 : Py_None,
tb1 ? tb1 : Py_None,
NULL);
- Py_XDECREF(res1);
-
- if (!PyErr_Occurred()) {
- Py_XDECREF(exc1);
- Py_XDECREF(val1);
- Py_XDECREF(tb1);
- goto no_more_exception;
+ if (res1 != NULL) {
+ if (res1 != Py_None)
+ convert_from_object_fficallback(result, SIGNATURE(1), res1);
+ Py_DECREF(res1);
+ if (!PyErr_Occurred()) {
+ Py_XDECREF(exc1);
+ Py_XDECREF(val1);
+ Py_XDECREF(tb1);
+ if (res1 != Py_None)
+ goto done;
+ goto no_more_exception;
+ }
}
/* double exception! print a double-traceback... */
PyErr_Fetch(&exc2, &val2, &tb2);
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1183,8 +1183,10 @@
f = callback(BFunc, Zcb1, -42)
#
seen = []
+ oops_result = None
def oops(*args):
seen.append(args)
+ return oops_result
ff = callback(BFunc, Zcb1, -42, oops)
#
orig_stderr = sys.stderr
@@ -1222,6 +1224,35 @@
assert exc is OverflowError
assert str(val) == "integer 60000 does not fit 'short'"
#
+ sys.stderr = cStringIO.StringIO()
+ bigvalue = 20000
+ del seen[:]
+ oops_result = 81
+ assert ff(bigvalue) == 81
+ oops_result = None
+ assert sys.stderr.getvalue() == ""
+ assert len(seen) == 1
+ exc, val, tb = seen[0]
+ assert exc is OverflowError
+ assert str(val) == "integer 60000 does not fit 'short'"
+ #
+ sys.stderr = cStringIO.StringIO()
+ bigvalue = 20000
+ del seen[:]
+ oops_result = "xy" # not None and not an int!
+ assert ff(bigvalue) == -42
+ oops_result = None
+ assert matches(sys.stderr.getvalue(), """\
+From cffi callback <function$Zcb1 at 0x$>:
+Trying to convert the result back to C:
+OverflowError: integer 60000 does not fit 'short'
+
+During the call to 'onerror', another exception occurred:
+
+TypeError: an integer is required
+""")
+ #
+ sys.stderr = cStringIO.StringIO()
seen = "not a list" # this makes the oops() function crash
assert ff(bigvalue) == -42
assert matches(sys.stderr.getvalue(), """\
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit