diff --git a/CHANGELOG.md b/CHANGELOG.md index f894f01b..58c9d2fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed -- Change `PyErr::fetch()` to return `Option`. [#1728](https://github.com/PyO3/pyo3/pull/1728) +- Change `PyErr::fetch()` to return `Option`. [#1717](https://github.com/PyO3/pyo3/pull/1717) ## [0.14.2] - 2021-08-09 diff --git a/src/err/mod.rs b/src/err/mod.rs index ecce456c..4f91013d 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -254,9 +254,20 @@ impl PyErr { /// Retrieves the current error from the Python interpreter's global state. /// /// The error is cleared from the Python interpreter. - /// Panics if no error is set + /// If no error is set, returns a `SystemError` in release mode, + /// panics in debug mode. pub(crate) fn api_call_failed(py: Python) -> PyErr { - PyErr::fetch(py).expect("exception missing") + #[cfg(debug_assertions)] + { + PyErr::fetch(py).expect("error return without exception set") + } + #[cfg(not(debug_assertions))] + { + use crate::exceptions::PySystemError; + + PyErr::fetch(py) + .unwrap_or_else(|| PySystemError::new_err("error return without exception set")) + } } /// Creates a new exception type with the given name, which must be of the form