Skip to content

For PyPy only, re-enable old behavior (runs the risk of masking bugs) #4079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ struct error_fetch_and_normalize {
+ " failed to obtain the name "
"of the normalized active exception type.");
}
#if defined(PYPY_VERSION)
// This behavior runs the risk of masking errors in the error handling, but avoids a
// conflict with PyPy, which relies on the normalization here to change OSError to
// FileNotFoundError (https://github.com/pybind/pybind11/issues/4075).
m_lazy_error_string = exc_type_name_norm;
#else
if (exc_type_name_norm != m_lazy_error_string) {
std::string msg = std::string(called)
+ ": MISMATCH of original and normalized "
Expand All @@ -467,6 +473,7 @@ struct error_fetch_and_normalize {
msg += ": " + format_value_and_trace();
pybind11_fail(msg);
}
#endif
}

error_fetch_and_normalize(const error_fetch_and_normalize &) = delete;
Expand Down
10 changes: 10 additions & 0 deletions tests/test_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,14 @@ TEST_SUBMODULE(exceptions, m) {
e.restore();
}
});

// https://github.com/pybind/pybind11/issues/4075
m.def("test_pypy_oserror_normalization", []() {
try {
py::module_::import("io").attr("open")("this_filename_must_not_exist", "r");
} catch (const py::error_already_set &e) {
return py::str(e.what()); // str must be built before e goes out of scope.
}
return py::str("UNEXPECTED");
});
}
6 changes: 6 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,9 @@ def test_error_already_set_double_restore():
"Internal error: pybind11::detail::error_fetch_and_normalize::restore()"
" called a second time. ORIGINAL ERROR: ValueError: Random error."
)


def test_pypy_oserror_normalization():
# https://github.com/pybind/pybind11/issues/4075
what = m.test_pypy_oserror_normalization()
assert "this_filename_must_not_exist" in what