Add PyDowncastErrorArguments to delay formatting downcast errors.
This commit is contained in:
parent
74e93a2c29
commit
10c285b283
|
@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
### Changed
|
||||
|
||||
- Default to "m" ABI tag when choosing `libpython` link name for CPython 3.7 on Unix. [#2288](https://github.com/PyO3/pyo3/pull/2288)
|
||||
- Improved performance of failing calls to `FromPyObject::extract` which is common when functions accept multiple distinct types. [#2279](https://github.com/PyO3/pyo3/pull/2279)
|
||||
|
||||
## [0.16.3] - 2022-04-05
|
||||
|
||||
|
|
|
@ -663,10 +663,34 @@ impl<'a> IntoPy<PyObject> for &'a PyErr {
|
|||
}
|
||||
}
|
||||
|
||||
struct PyDowncastErrorArguments {
|
||||
from: Py<PyType>,
|
||||
to: Cow<'static, str>,
|
||||
}
|
||||
|
||||
impl PyErrArguments for PyDowncastErrorArguments {
|
||||
fn arguments(self, py: Python<'_>) -> PyObject {
|
||||
format!(
|
||||
"'{}' object cannot be converted to '{}'",
|
||||
self.from
|
||||
.as_ref(py)
|
||||
.name()
|
||||
.unwrap_or("<failed to extract type name>"),
|
||||
self.to
|
||||
)
|
||||
.to_object(py)
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `PyDowncastError` to Python `TypeError`.
|
||||
impl<'a> std::convert::From<PyDowncastError<'a>> for PyErr {
|
||||
fn from(err: PyDowncastError<'_>) -> PyErr {
|
||||
exceptions::PyTypeError::new_err(err.to_string())
|
||||
let args = PyDowncastErrorArguments {
|
||||
from: err.from.get_type().into(),
|
||||
to: err.to,
|
||||
};
|
||||
|
||||
exceptions::PyTypeError::new_err(args)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue