mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
try_bytes_like
This commit is contained in:
1 parent
fb23b02bf9
commit
e9f99ca080
1 file changed
+19
-1
+19
-1
@@ -22,7 +22,7 @@ impl TryFromObject for PyBytesLike {
|
||||
array @ PyArray => Ok(PyBytesLike::Array(array)),
|
||||
obj => Err(vm.new_type_error(format!(
|
||||
"a bytes-like object is required, not {}",
|
||||
obj.class()
|
||||
obj.lease_class().name
|
||||
))),
|
||||
})
|
||||
}
|
||||
@@ -72,6 +72,24 @@ impl PyBytesLike {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn try_bytes_like<R>(
|
||||
vm: &VirtualMachine,
|
||||
obj: &PyObjectRef,
|
||||
f: impl FnOnce(&[u8]) -> R,
|
||||
) -> PyResult<R> {
|
||||
let r = match_class!(match obj {
|
||||
ref b @ PyBytes => f(b.borrow_value()),
|
||||
ref b @ PyByteArray => f(&b.borrow_value().elements),
|
||||
ref array @ PyArray => f(&*array.get_bytes()),
|
||||
obj =>
|
||||
return Err(vm.new_type_error(format!(
|
||||
"a bytes-like object is required, not {}",
|
||||
obj.lease_class().name
|
||||
))),
|
||||
});
|
||||
Ok(r)
|
||||
}
|
||||
|
||||
pub enum PyRwBytesLike {
|
||||
Bytearray(PyByteArrayRef),
|
||||
Array(PyArrayRef),
|
||||
|
||||
Reference in new issue
Block a user