Clone of @virtuald's PR #2112 with minor enhancements. (#3215)

* Add py::raise_from to enable chaining exceptions on Python 3.3+

* Use 'raise from' in initialization

* Documenting the exact base version of _PyErr_FormatVFromCause, adding back `assert`s.

Co-authored-by: Dustin Spicuzza <dustin@virtualroadside.com>
This commit is contained in:
Ralf W. Grosse-Kunstleve
2021-08-23 17:30:01 -07:00
committed by GitHub
parent 6cbabc4b8c
commit c8ce4b8df8
6 changed files with 136 additions and 0 deletions

View File

@@ -24,6 +24,22 @@ def test_error_already_set(msg):
assert msg(excinfo.value) == "foo"
@pytest.mark.skipif("env.PY2")
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"
@pytest.mark.skipif("env.PY2")
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"
def test_cross_module_exceptions(msg):
with pytest.raises(RuntimeError) as excinfo:
cm.raise_runtime_error()