Skip to content

Commit b8cc20a

Browse files
committed
Improve error message if bytes/bytearray casting failed
1 parent ab09f97 commit b8cc20a

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

‎src/de.rs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use pyo3::exceptions::PyKeyError;
2-
use pyo3::{Bound, intern, types::*};
2+
use pyo3::{Bound, CastIntoError, PyTypeInfo, intern, types::*};
33
use serde::Deserialize;
44
use serde::de::{self, IntoDeserializer};
55

@@ -223,11 +223,26 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> {
223223
where
224224
V: de::Visitor<'de>,
225225
{
226-
if let Ok(b) = self.input.cast::<PyByteArray>() {
226+
let obj = self.input;
227+
228+
if let Ok(b) = obj.cast::<PyBytes>() {
229+
visitor.visit_bytes(b.as_bytes())
230+
} else if let Ok(b) = obj.cast::<PyByteArray>() {
227231
visitor.visit_byte_buf(b.to_vec())
228232
} else {
229-
let b = self.input.cast::<PyBytes>()?;
230-
visitor.visit_bytes(b.as_bytes())
233+
Err(CastIntoError::new(
234+
obj.clone(),
235+
PyTuple::new(
236+
obj.py(),
237+
[
238+
PyBytes::type_object(obj.py()),
239+
PyByteArray::type_object(obj.py()),
240+
],
241+
)
242+
.unwrap()
243+
.into_any(),
244+
)
245+
.into())
231246
}
232247
}
233248

0 commit comments

Comments
 (0)