Commit 2d86c6b0 authored by Armin Rigo's avatar Armin Rigo
Browse files

'onerror' can also return a custom value to return

parent 2b30856ad741
......@@ -4802,13 +4802,18 @@ static void invoke_callback(ffi_cif *cif, void *result, void **args,
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);
......
......@@ -1183,8 +1183,10 @@ def test_callback_exception():
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 @@ OverflowError: integer 60000 does not fit 'short'
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(), """\
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment