Minor enhancements of new code.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2023-05-21 20:27:57 -07:00
parent 5d349f6fde
commit 5751217bcc

View File

@ -495,7 +495,14 @@ struct error_fetch_and_normalize {
"of the original active exception type."); "of the original active exception type.");
} }
m_lazy_error_string = exc_type_name_orig; m_lazy_error_string = exc_type_name_orig;
#if PY_VERSION_HEX < 0x030C0000 #if PY_VERSION_HEX >= 0x030C0000
// The presence of __notes__ is likely due to exception normalization
// errors, although that is not necessarily true, therefore insert a
// hint only:
if (PyObject_HasAttrString(m_value.ptr(), "__notes__")) {
m_lazy_error_string += "[WITH __notes__]";
}
#else
// PyErr_NormalizeException() may change the exception type if there are cascading // PyErr_NormalizeException() may change the exception type if there are cascading
// failures. This can potentially be extremely confusing. // failures. This can potentially be extremely confusing.
PyErr_NormalizeException(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr()); PyErr_NormalizeException(&m_type.ptr(), &m_value.ptr(), &m_trace.ptr());
@ -528,11 +535,6 @@ struct error_fetch_and_normalize {
pybind11_fail(msg); pybind11_fail(msg);
} }
# endif # endif
#else // Python 3.12+
// The presence of __notes__ could be due to exception normalization errors.
if (PyObject_HasAttrString(m_value.ptr(), "__notes__")) {
m_lazy_error_string += "[WITH __notes__]";
}
#endif #endif
} }
@ -575,7 +577,7 @@ struct error_fetch_and_normalize {
PyErr_Clear(); // No notes is good news. PyErr_Clear(); // No notes is good news.
} else { } else {
auto len_notes = PyList_Size(notes.ptr()); auto len_notes = PyList_Size(notes.ptr());
if (PyErr_Occurred()) { if (len_notes < 0) {
result += "\nFAILURE obtaining len(__notes__): " + detail::error_string(); result += "\nFAILURE obtaining len(__notes__): " + detail::error_string();
} else { } else {
result += "\n__notes__ (len=" + std::to_string(len_notes) + "):"; result += "\n__notes__ (len=" + std::to_string(len_notes) + "):";