Skip to content

Commit e564142

Browse files
committed
Add error_already_set_what what tests, asserting the status quo.
1 parent 4624e8e commit e564142

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

tests/test_exceptions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,12 @@ TEST_SUBMODULE(exceptions, m) {
299299
std::throw_with_nested(std::runtime_error("Outer Exception"));
300300
}
301301
});
302+
303+
m.def("error_already_set_what", [](const py::object &exc_type, const py::object &exc_value) {
304+
PyErr_SetObject(exc_type.ptr(), exc_value.ptr());
305+
std::string what = py::error_already_set().what();
306+
bool py_err_set_after_what = (PyErr_Occurred() != nullptr);
307+
PyErr_Clear();
308+
return py::make_tuple(what, py_err_set_after_what);
309+
});
302310
}

tests/test_exceptions.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,54 @@ def test_local_translator(msg):
270270
m.throws_local_simple_error()
271271
assert not isinstance(excinfo.value, cm.LocalSimpleException)
272272
assert msg(excinfo.value) == "this mod"
273+
274+
275+
class FlakyException(Exception):
276+
def __init__(self, failure_point):
277+
if failure_point == "failure_point_init":
278+
raise ValueError("triggered_failure_point_init")
279+
self.failure_point = failure_point
280+
281+
def __str__(self):
282+
if self.failure_point == "failure_point_str":
283+
raise ValueError("triggered_failure_point_str")
284+
return "FlakyException.__str__"
285+
286+
287+
@pytest.mark.parametrize(
288+
"exc_type, exc_value, expected_what",
289+
(
290+
(ValueError, "plain_str", "ValueError: plain_str"),
291+
(ValueError, ("tuple_elem",), "ValueError: ('tuple_elem',)"),
292+
(FlakyException, ("happy",), "FlakyException: ('happy',)"),
293+
),
294+
)
295+
def test_error_already_set_what_with_happy_exceptions(
296+
exc_type, exc_value, expected_what
297+
):
298+
what, py_err_set_after_what = m.error_already_set_what(exc_type, exc_value)
299+
assert not py_err_set_after_what
300+
assert what == expected_what
301+
302+
303+
def test_flaky_exception_failure_point_init():
304+
what, py_err_set_after_what = m.error_already_set_what(
305+
FlakyException, ("failure_point_init",)
306+
)
307+
assert not py_err_set_after_what
308+
lines = what.splitlines()
309+
# PyErr_NormalizeException replaces the original FlakyException with ValueError:
310+
assert lines[:3] == ["FlakyException: ('failure_point_init',)", "", "At:"]
311+
# Checking the first two lines of the traceback as formatted in error_string(),
312+
# which is actually for a different exception (ValueError)!
313+
assert "test_exceptions.py(" in lines[3]
314+
assert lines[3].endswith("): __init__")
315+
assert lines[4].endswith("): test_flaky_exception_failure_point_init")
316+
317+
318+
def test_flaky_exception_failure_point_str():
319+
what, py_err_set_after_what = m.error_already_set_what(
320+
FlakyException, ("failure_point_str",)
321+
)
322+
assert not py_err_set_after_what
323+
assert what == "FlakyException: ('failure_point_str',)"

0 commit comments

Comments
 (0)