From 3ba24bee1419c61cebc3a6e0b7b6faece4a7ae46 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 6 Jul 2021 07:57:46 +0800 Subject: [PATCH] `PyErr::api_call_failed` should return a `SystemError` when no error is set https://github.com/python/cpython/blob/17f94e28882e1e2b331ace93f42e8615383dee59/Python/ceval.c#L4330-L4333 --- CHANGELOG.md | 2 +- src/err/mod.rs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) 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