Bug fix: Remove what(); from restore().

It sure would need to be guarded by `if (m_type)`, otherwise `what()` fails and masks that no error was set (see update unit test). But since `error_already_set` is copyable, there is no point in releasing m_type, m_value, m_trace, therefore we can just as well avoid the runtime overhead of force-building `m_lazy_what`, it may never be used.
This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-05-10 17:59:27 -07:00
parent c7a7146875
commit de84a27fd4
2 changed files with 6 additions and 6 deletions

View File

@ -455,13 +455,13 @@ public:
}
/// Restores the currently-held Python error, if any (which will clear the Python error
/// indicator first if already set). After this call, the current object no longer stores the
/// error variables (but the `.what()` string is still available).
/// indicator first if already set).
void restore() {
what(); // Force-build m_lazy_what.
if (m_type) {
// As long as this type is copyable, there is no point in releasing m_type, m_value,
// m_trace.
PyErr_Restore(
m_type.release().ptr(), m_value.release().ptr(), m_trace.release().ptr());
m_type.inc_ref().ptr(), m_value.inc_ref().ptr(), m_trace.inc_ref().ptr());
}
}

View File

@ -14,9 +14,9 @@ def test_std_exception(msg):
def test_error_already_set(msg):
with pytest.raises(RuntimeError) as excinfo:
with pytest.raises(SystemError) as excinfo:
m.throw_already_set(False)
assert msg(excinfo.value) == "Unknown internal error occurred"
assert "without setting an error" in str(excinfo.value)
with pytest.raises(ValueError) as excinfo:
m.throw_already_set(True)