2020-08-08 10:07:14 +00:00
|
|
|
import sys
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-11-17 14:44:19 +00:00
|
|
|
import env
|
2017-07-29 01:38:23 +00:00
|
|
|
import pybind11_cross_module_tests as cm
|
2021-08-13 16:37:05 +00:00
|
|
|
from pybind11_tests import exceptions as m
|
2016-08-12 11:50:00 +00:00
|
|
|
|
2017-06-08 22:44:49 +00:00
|
|
|
|
2017-07-23 16:26:17 +00:00
|
|
|
def test_std_exception(msg):
|
2017-06-08 22:44:49 +00:00
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throw_std_exception()
|
2017-06-08 22:44:49 +00:00
|
|
|
assert msg(excinfo.value) == "This exception was intentionally thrown."
|
|
|
|
|
|
|
|
|
2016-09-07 20:10:16 +00:00
|
|
|
def test_error_already_set(msg):
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throw_already_set(False)
|
2016-09-07 20:10:16 +00:00
|
|
|
assert msg(excinfo.value) == "Unknown internal error occurred"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throw_already_set(True)
|
2016-09-07 20:10:16 +00:00
|
|
|
assert msg(excinfo.value) == "foo"
|
|
|
|
|
|
|
|
|
2021-08-24 00:30:01 +00:00
|
|
|
def test_raise_from(msg):
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
m.raise_from()
|
|
|
|
assert msg(excinfo.value) == "outer"
|
|
|
|
assert msg(excinfo.value.__cause__) == "inner"
|
|
|
|
|
|
|
|
|
|
|
|
def test_raise_from_already_set(msg):
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
m.raise_from_already_set()
|
|
|
|
assert msg(excinfo.value) == "outer"
|
|
|
|
assert msg(excinfo.value.__cause__) == "inner"
|
|
|
|
|
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
def test_cross_module_exceptions(msg):
|
2017-07-29 01:38:23 +00:00
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
cm.raise_runtime_error()
|
|
|
|
assert str(excinfo.value) == "My runtime error"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
cm.raise_value_error()
|
|
|
|
assert str(excinfo.value) == "My value error"
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
cm.throw_pybind_value_error()
|
|
|
|
assert str(excinfo.value) == "pybind11 value error"
|
|
|
|
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
cm.throw_pybind_type_error()
|
|
|
|
assert str(excinfo.value) == "pybind11 type error"
|
|
|
|
|
|
|
|
with pytest.raises(StopIteration) as excinfo:
|
|
|
|
cm.throw_stop_iteration()
|
|
|
|
|
2021-07-21 12:22:18 +00:00
|
|
|
with pytest.raises(cm.LocalSimpleException) as excinfo:
|
|
|
|
cm.throw_local_simple_error()
|
|
|
|
assert msg(excinfo.value) == "external mod"
|
|
|
|
|
|
|
|
with pytest.raises(KeyError) as excinfo:
|
|
|
|
cm.throw_local_error()
|
|
|
|
# KeyError is a repr of the key, so it has an extra set of quotes
|
|
|
|
assert str(excinfo.value) == "'just local'"
|
|
|
|
|
2017-07-29 01:38:23 +00:00
|
|
|
|
2021-05-27 15:00:18 +00:00
|
|
|
# TODO: FIXME
|
|
|
|
@pytest.mark.xfail(
|
|
|
|
"env.PYPY and env.MACOS",
|
|
|
|
raises=RuntimeError,
|
|
|
|
reason="Expected failure with PyPy and libc++ (Issue #2847 & PR #2999)",
|
|
|
|
)
|
|
|
|
def test_cross_module_exception_translator():
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
# translator registered in cross_module_tests
|
|
|
|
m.throw_should_be_translated_to_key_error()
|
|
|
|
|
|
|
|
|
2016-09-10 09:58:02 +00:00
|
|
|
def test_python_call_in_catch():
|
|
|
|
d = {}
|
2017-07-23 16:26:17 +00:00
|
|
|
assert m.python_call_in_destructor(d) is True
|
2016-09-10 09:58:02 +00:00
|
|
|
assert d["good"] is True
|
|
|
|
|
|
|
|
|
2020-12-24 14:53:23 +00:00
|
|
|
def ignore_pytest_unraisable_warning(f):
|
|
|
|
unraisable = "PytestUnraisableExceptionWarning"
|
|
|
|
if hasattr(pytest, unraisable): # Python >= 3.8 and pytest >= 6
|
2022-02-12 00:06:16 +00:00
|
|
|
dec = pytest.mark.filterwarnings(f"ignore::pytest.{unraisable}")
|
2020-12-24 14:53:23 +00:00
|
|
|
return dec(f)
|
|
|
|
else:
|
|
|
|
return f
|
|
|
|
|
|
|
|
|
2021-11-17 14:44:19 +00:00
|
|
|
# TODO: find out why this fails on PyPy, https://foss.heptapod.net/pypy/pypy/-/issues/3583
|
|
|
|
@pytest.mark.xfail(env.PYPY, reason="Failure on PyPy 3.8 (7.3.7)", strict=False)
|
2020-12-24 14:53:23 +00:00
|
|
|
@ignore_pytest_unraisable_warning
|
2020-08-08 10:07:14 +00:00
|
|
|
def test_python_alreadyset_in_destructor(monkeypatch, capsys):
|
|
|
|
hooked = False
|
2022-02-11 02:28:08 +00:00
|
|
|
triggered = False
|
2020-08-08 10:07:14 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
if hasattr(sys, "unraisablehook"): # Python 3.8+
|
2020-08-08 10:07:14 +00:00
|
|
|
hooked = True
|
2020-12-24 14:53:23 +00:00
|
|
|
# Don't take `sys.unraisablehook`, as that's overwritten by pytest
|
|
|
|
default_hook = sys.__unraisablehook__
|
2020-08-08 10:07:14 +00:00
|
|
|
|
|
|
|
def hook(unraisable_hook_args):
|
|
|
|
exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
|
2020-10-16 20:38:13 +00:00
|
|
|
if obj == "already_set demo":
|
2022-02-11 02:28:08 +00:00
|
|
|
nonlocal triggered
|
|
|
|
triggered = True
|
2020-08-08 10:07:14 +00:00
|
|
|
default_hook(unraisable_hook_args)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Use monkeypatch so pytest can apply and remove the patch as appropriate
|
2020-10-16 20:38:13 +00:00
|
|
|
monkeypatch.setattr(sys, "unraisablehook", hook)
|
2020-08-08 10:07:14 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
assert m.python_alreadyset_in_destructor("already_set demo") is True
|
2020-08-08 10:07:14 +00:00
|
|
|
if hooked:
|
2022-02-11 02:28:08 +00:00
|
|
|
assert triggered is True
|
2020-08-08 10:07:14 +00:00
|
|
|
|
|
|
|
_, captured_stderr = capsys.readouterr()
|
2022-02-11 02:28:08 +00:00
|
|
|
assert captured_stderr.startswith("Exception ignored in: 'already_set demo'")
|
|
|
|
assert captured_stderr.rstrip().endswith("KeyError: 'bar'")
|
2020-08-08 10:07:14 +00:00
|
|
|
|
|
|
|
|
2017-04-02 20:38:50 +00:00
|
|
|
def test_exception_matches():
|
2019-05-12 21:35:49 +00:00
|
|
|
assert m.exception_matches()
|
|
|
|
assert m.exception_matches_base()
|
|
|
|
assert m.modulenotfound_exception_matches_base()
|
2017-04-02 20:38:50 +00:00
|
|
|
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
def test_custom(msg):
|
2017-07-23 16:26:17 +00:00
|
|
|
# Can we catch a MyException?
|
|
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
|
|
m.throws1()
|
2016-08-12 11:50:00 +00:00
|
|
|
assert msg(excinfo.value) == "this error should go to a custom type"
|
|
|
|
|
|
|
|
# Can we translate to standard Python exceptions?
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throws2()
|
2016-08-12 11:50:00 +00:00
|
|
|
assert msg(excinfo.value) == "this error should go to a standard Python exception"
|
|
|
|
|
|
|
|
# Can we handle unknown exceptions?
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throws3()
|
2016-08-12 11:50:00 +00:00
|
|
|
assert msg(excinfo.value) == "Caught an unknown exception!"
|
|
|
|
|
|
|
|
# Can we delegate to another handler by rethrowing?
|
2017-07-23 16:26:17 +00:00
|
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
|
|
m.throws4()
|
2016-08-12 11:50:00 +00:00
|
|
|
assert msg(excinfo.value) == "this error is rethrown"
|
|
|
|
|
2017-07-23 16:26:17 +00:00
|
|
|
# Can we fall-through to the default handler?
|
2016-08-12 11:50:00 +00:00
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throws_logic_error()
|
2020-10-16 20:38:13 +00:00
|
|
|
assert (
|
|
|
|
msg(excinfo.value) == "this error should fall through to the standard handler"
|
|
|
|
)
|
2016-09-16 06:04:15 +00:00
|
|
|
|
2019-11-14 07:56:58 +00:00
|
|
|
# OverFlow error translation.
|
|
|
|
with pytest.raises(OverflowError) as excinfo:
|
|
|
|
m.throws_overflow_error()
|
|
|
|
|
2016-09-16 06:04:15 +00:00
|
|
|
# Can we handle a helper-declared exception?
|
2017-07-23 16:26:17 +00:00
|
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
|
|
m.throws5()
|
2016-09-16 06:04:15 +00:00
|
|
|
assert msg(excinfo.value) == "this is a helper-defined translated exception"
|
|
|
|
|
|
|
|
# Exception subclassing:
|
2017-07-23 16:26:17 +00:00
|
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
|
|
m.throws5_1()
|
2016-09-16 06:04:15 +00:00
|
|
|
assert msg(excinfo.value) == "MyException5 subclass"
|
2017-07-23 16:26:17 +00:00
|
|
|
assert isinstance(excinfo.value, m.MyException5_1)
|
2016-09-16 06:04:15 +00:00
|
|
|
|
2017-07-23 16:26:17 +00:00
|
|
|
with pytest.raises(m.MyException5_1) as excinfo:
|
|
|
|
m.throws5_1()
|
2016-09-16 06:04:15 +00:00
|
|
|
assert msg(excinfo.value) == "MyException5 subclass"
|
|
|
|
|
2017-07-23 16:26:17 +00:00
|
|
|
with pytest.raises(m.MyException5) as excinfo:
|
2016-09-16 06:04:15 +00:00
|
|
|
try:
|
2017-07-23 16:26:17 +00:00
|
|
|
m.throws5()
|
|
|
|
except m.MyException5_1:
|
2016-09-16 06:04:15 +00:00
|
|
|
raise RuntimeError("Exception error: caught child from parent")
|
|
|
|
assert msg(excinfo.value) == "this is a helper-defined translated exception"
|
Simplify error_already_set
`error_already_set` is more complicated than it needs to be, partly
because it manages reference counts itself rather than using
`py::object`, and partly because it tries to do more exception clearing
than is needed. This commit greatly simplifies it, and fixes #927.
Using `py::object` instead of `PyObject *` means we can rely on
implicit copy/move constructors.
The current logic did both a `PyErr_Clear` on deletion *and* a
`PyErr_Fetch` on creation. I can't see how the `PyErr_Clear` on
deletion is ever useful: the `Fetch` on creation itself clears the
error, so the only way doing a `PyErr_Clear` on deletion could do
anything if is some *other* exception was raised while the
`error_already_set` object was alive--but in that case, clearing some
other exception seems wrong. (Code that is worried about an exception
handler raising another exception would already catch a second
`error_already_set` from exception code).
The destructor itself called `clear()`, but `clear()` was a little bit
more paranoid that needed: it called `restore()` to restore the
currently captured error, but then immediately cleared it, using the
`PyErr_Restore` to release the references. That's unnecessary: it's
valid for us to release the references manually. This updates the code
to simply release the references on the three objects (preserving the
gil acquire).
`clear()`, however, also had the side effect of clearing the current
error, even if the current `error_already_set` didn't have a current
error (e.g. because of a previous `restore()` or `clear()` call). I
don't really see how clearing the error here can ever actually be
useful: the only way the current error could be set is if you called
`restore()` (in which case the current stored error-related members have
already been released), or if some *other* code raised the error, in
which case `clear()` on *this* object is clearing an error for which it
shouldn't be responsible.
Neither of those seem like intentional or desirable features, and
manually requesting deletion of the stored references similarly seems
pointless, so I've just made `clear()` an empty method and marked it
deprecated.
This also fixes a minor potential issue with the destruction: it is
technically possible for `value` to be null (though this seems likely to
be rare in practice); this updates the check to look at `type` which
will always be non-null for a `Fetch`ed exception.
This also adds error_already_set round-trip throw tests to the test
suite.
2017-07-21 03:14:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_nested_throws(capture):
|
|
|
|
"""Tests nested (e.g. C++ -> Python -> C++) exception handling"""
|
|
|
|
|
|
|
|
def throw_myex():
|
|
|
|
raise m.MyException("nested error")
|
|
|
|
|
|
|
|
def throw_myex5():
|
|
|
|
raise m.MyException5("nested error 5")
|
|
|
|
|
|
|
|
# In the comments below, the exception is caught in the first step, thrown in the last step
|
|
|
|
|
|
|
|
# C++ -> Python
|
|
|
|
with capture:
|
|
|
|
m.try_catch(m.MyException5, throw_myex5)
|
|
|
|
assert str(capture).startswith("MyException5: nested error 5")
|
|
|
|
|
|
|
|
# Python -> C++ -> Python
|
|
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
|
|
m.try_catch(m.MyException5, throw_myex)
|
|
|
|
assert str(excinfo.value) == "nested error"
|
|
|
|
|
|
|
|
def pycatch(exctype, f, *args):
|
|
|
|
try:
|
|
|
|
f(*args)
|
|
|
|
except m.MyException as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
# C++ -> Python -> C++ -> Python
|
|
|
|
with capture:
|
|
|
|
m.try_catch(
|
2020-10-16 20:38:13 +00:00
|
|
|
m.MyException5,
|
|
|
|
pycatch,
|
|
|
|
m.MyException,
|
|
|
|
m.try_catch,
|
|
|
|
m.MyException,
|
|
|
|
throw_myex5,
|
|
|
|
)
|
Simplify error_already_set
`error_already_set` is more complicated than it needs to be, partly
because it manages reference counts itself rather than using
`py::object`, and partly because it tries to do more exception clearing
than is needed. This commit greatly simplifies it, and fixes #927.
Using `py::object` instead of `PyObject *` means we can rely on
implicit copy/move constructors.
The current logic did both a `PyErr_Clear` on deletion *and* a
`PyErr_Fetch` on creation. I can't see how the `PyErr_Clear` on
deletion is ever useful: the `Fetch` on creation itself clears the
error, so the only way doing a `PyErr_Clear` on deletion could do
anything if is some *other* exception was raised while the
`error_already_set` object was alive--but in that case, clearing some
other exception seems wrong. (Code that is worried about an exception
handler raising another exception would already catch a second
`error_already_set` from exception code).
The destructor itself called `clear()`, but `clear()` was a little bit
more paranoid that needed: it called `restore()` to restore the
currently captured error, but then immediately cleared it, using the
`PyErr_Restore` to release the references. That's unnecessary: it's
valid for us to release the references manually. This updates the code
to simply release the references on the three objects (preserving the
gil acquire).
`clear()`, however, also had the side effect of clearing the current
error, even if the current `error_already_set` didn't have a current
error (e.g. because of a previous `restore()` or `clear()` call). I
don't really see how clearing the error here can ever actually be
useful: the only way the current error could be set is if you called
`restore()` (in which case the current stored error-related members have
already been released), or if some *other* code raised the error, in
which case `clear()` on *this* object is clearing an error for which it
shouldn't be responsible.
Neither of those seem like intentional or desirable features, and
manually requesting deletion of the stored references similarly seems
pointless, so I've just made `clear()` an empty method and marked it
deprecated.
This also fixes a minor potential issue with the destruction: it is
technically possible for `value` to be null (though this seems likely to
be rare in practice); this updates the check to look at `type` which
will always be non-null for a `Fetch`ed exception.
This also adds error_already_set round-trip throw tests to the test
suite.
2017-07-21 03:14:33 +00:00
|
|
|
assert str(capture).startswith("MyException5: nested error 5")
|
|
|
|
|
|
|
|
# C++ -> Python -> C++
|
|
|
|
with capture:
|
|
|
|
m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
|
|
|
|
assert capture == "this error is rethrown"
|
|
|
|
|
|
|
|
# Python -> C++ -> Python -> C++
|
|
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
|
|
m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
|
|
|
|
assert str(excinfo.value) == "this is a helper-defined translated exception"
|
2020-08-18 11:14:34 +00:00
|
|
|
|
|
|
|
|
2022-01-14 19:22:47 +00:00
|
|
|
def test_throw_nested_exception():
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
m.throw_nested_exception()
|
|
|
|
assert str(excinfo.value) == "Outer Exception"
|
|
|
|
assert str(excinfo.value.__cause__) == "Inner Exception"
|
|
|
|
|
|
|
|
|
2020-08-18 11:14:34 +00:00
|
|
|
# This can often happen if you wrap a pybind11 class in a Python wrapper
|
|
|
|
def test_invalid_repr():
|
2022-02-11 02:28:08 +00:00
|
|
|
class MyRepr:
|
2020-08-18 11:14:34 +00:00
|
|
|
def __repr__(self):
|
|
|
|
raise AttributeError("Example error")
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
m.simple_bool_passthrough(MyRepr())
|
2021-07-21 12:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_local_translator(msg):
|
|
|
|
"""Tests that a local translator works and that the local translator from
|
|
|
|
the cross module is not applied"""
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
m.throws6()
|
|
|
|
assert msg(excinfo.value) == "MyException6 only handled in this module"
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
|
|
m.throws_local_error()
|
|
|
|
assert not isinstance(excinfo.value, KeyError)
|
|
|
|
assert msg(excinfo.value) == "never caught"
|
|
|
|
|
|
|
|
with pytest.raises(Exception) as excinfo:
|
|
|
|
m.throws_local_simple_error()
|
|
|
|
assert not isinstance(excinfo.value, cm.LocalSimpleException)
|
|
|
|
assert msg(excinfo.value) == "this mod"
|