Skip to content

Commit 254bfeb

Browse files
committed
Move PyErr_NormalizeException() up a few lines.
1 parent e564142 commit 254bfeb

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

include/pybind11/detail/type_caster_base.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ PYBIND11_NOINLINE std::string error_string() {
478478

479479
error_scope scope; // Preserve error state
480480

481+
PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
482+
481483
std::string errorString;
482484
if (scope.type) {
483485
errorString += handle(scope.type).attr("__name__").cast<std::string>();
@@ -486,9 +488,6 @@ PYBIND11_NOINLINE std::string error_string() {
486488
if (scope.value) {
487489
errorString += (std::string) str(scope.value);
488490
}
489-
490-
PyErr_NormalizeException(&scope.type, &scope.value, &scope.trace);
491-
492491
if (scope.trace != nullptr) {
493492
PyException_SetTraceback(scope.value, scope.trace);
494493
}

tests/test_exceptions.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ def __str__(self):
288288
"exc_type, exc_value, expected_what",
289289
(
290290
(ValueError, "plain_str", "ValueError: plain_str"),
291-
(ValueError, ("tuple_elem",), "ValueError: ('tuple_elem',)"),
292-
(FlakyException, ("happy",), "FlakyException: ('happy',)"),
291+
(ValueError, ("tuple_elem",), "ValueError: tuple_elem"),
292+
(FlakyException, ("happy",), "FlakyException: FlakyException.__str__"),
293293
),
294294
)
295295
def test_error_already_set_what_with_happy_exceptions(
@@ -307,17 +307,15 @@ def test_flaky_exception_failure_point_init():
307307
assert not py_err_set_after_what
308308
lines = what.splitlines()
309309
# 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)!
310+
assert lines[:3] == ["ValueError: triggered_failure_point_init", "", "At:"]
311+
# Checking the first two lines of the traceback as formatted in error_string():
313312
assert "test_exceptions.py(" in lines[3]
314313
assert lines[3].endswith("): __init__")
315314
assert lines[4].endswith("): test_flaky_exception_failure_point_init")
316315

317316

318317
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',)"
318+
# The error_already_set ctor fails due to a ValueError in error_string():
319+
with pytest.raises(ValueError) as excinfo:
320+
m.error_already_set_what(FlakyException, ("failure_point_str",))
321+
assert str(excinfo.value) == "triggered_failure_point_str"

0 commit comments

Comments
 (0)