|
1 | 1 | use std::sync::Arc; |
2 | 2 |
|
3 | | -use pyo3::exceptions::{PyTypeError, PyValueError}; |
| 3 | +use pyo3::exceptions::{PyTypeError, PyValueError, PyZeroDivisionError}; |
4 | 4 | use pyo3::intern; |
5 | 5 | use pyo3::sync::PyOnceLock; |
6 | 6 | use pyo3::types::{PyDict, PyString, PyType}; |
@@ -148,24 +148,20 @@ impl Validator for FractionValidator { |
148 | 148 | pub(crate) fn create_fraction<'py>(arg: &Bound<'py, PyAny>, input: impl ToErrorValue) -> ValResult<Bound<'py, PyAny>> { |
149 | 149 | let py = arg.py(); |
150 | 150 | get_fraction_type(py).call1((arg,)).map_err(|e| { |
151 | | - let fraction_exception = match py |
152 | | - .import("fractions") |
153 | | - .and_then(|fraction_module| fraction_module.getattr("FractionException")) |
154 | | - { |
155 | | - Ok(fraction_exception) => fraction_exception, |
156 | | - Err(e) => return ValError::InternalErr(e), |
157 | | - }; |
158 | | - handle_fraction_new_error(input, e, fraction_exception) |
| 151 | + handle_fraction_new_error(input, e) |
159 | 152 | }) |
160 | 153 | } |
161 | 154 |
|
162 | | -fn handle_fraction_new_error(input: impl ToErrorValue, error: PyErr, fraction_exception: Bound<'_, PyAny>) -> ValError { |
163 | | - let py = fraction_exception.py(); |
164 | | - if error.matches(py, fraction_exception).unwrap_or(false) { |
165 | | - ValError::new(ErrorTypeDefaults::FractionParsing, input) |
166 | | - } else if error.matches(py, PyTypeError::type_object(py)).unwrap_or(false) { |
167 | | - ValError::new(ErrorTypeDefaults::FractionType, input) |
168 | | - } else { |
169 | | - ValError::InternalErr(error) |
170 | | - } |
| 155 | +fn handle_fraction_new_error(input: impl ToErrorValue, error: PyErr) -> ValError { |
| 156 | + Python::with_gil(|py| { |
| 157 | + if error.matches(py, PyValueError::type_object(py)).unwrap_or(false) { |
| 158 | + ValError::new(ErrorTypeDefaults::FractionParsing, input) |
| 159 | + } else if error.matches(py, PyTypeError::type_object(py)).unwrap_or(false) { |
| 160 | + ValError::new(ErrorTypeDefaults::FractionType, input) |
| 161 | + } else { |
| 162 | + // Let ZeroDivisionError and other exceptions bubble up as InternalErr |
| 163 | + // which will be shown to the user with the original Python error message |
| 164 | + ValError::InternalErr(error) |
| 165 | + } |
| 166 | + }) |
171 | 167 | } |
0 commit comments