From 498195ac4b68b9b705a8049a282b3d8d42dd021c Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 10 May 2022 18:22:50 -0700 Subject: [PATCH] Replace extremely opaque (unhelpful) error message with a truthful reflection of what we know. --- include/pybind11/detail/type_caster_base.h | 6 ++++-- tests/test_exceptions.cpp | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 93c8238c5..cd54de285 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -473,8 +473,10 @@ PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) PYBIND11_NOINLINE std::string error_string(PyObject *exc_type, PyObject *exc_value, PyObject *exc_trace) { if (!exc_type) { - PyErr_SetString(PyExc_RuntimeError, "Unknown internal error occurred"); - return "Unknown internal error occurred"; + static const char *msg + = "Internal error: error_string() called without a Python error available."; + PyErr_SetString(PyExc_RuntimeError, msg); + return msg; } auto result = handle(exc_type).attr("__name__").cast(); diff --git a/tests/test_exceptions.cpp b/tests/test_exceptions.cpp index 3e9a3d771..99eaed36a 100644 --- a/tests/test_exceptions.cpp +++ b/tests/test_exceptions.cpp @@ -228,7 +228,10 @@ TEST_SUBMODULE(exceptions, m) { throw py::error_already_set(); } catch (const std::runtime_error &e) { if ((err && e.what() != std::string("ValueError: foo")) - || (!err && e.what() != std::string("Unknown internal error occurred"))) { + || (!err + && e.what() + != std::string("Internal error: error_string() called without a Python " + "error available."))) { PyErr_Clear(); throw std::runtime_error("error message mismatch"); }