Skip to content

Commit aafbdd0

Browse files
committed
Allow struct.unpack to take a bytes
1 parent fca8314 commit aafbdd0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

vm/src/stdlib/pystruct.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,9 +598,18 @@ mod _struct {
598598
}
599599

600600
#[pyfunction]
601-
fn unpack(fmt: PyStringRef, buffer: PyBytesRef, vm: &VirtualMachine) -> PyResult<PyTuple> {
602-
let fmt_str = fmt.as_str();
603-
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| new_struct_error(vm, e))?;
601+
fn unpack(
602+
fmt: Either<PyStringRef, PyBytesRef>,
603+
buffer: PyBytesRef,
604+
vm: &VirtualMachine,
605+
) -> PyResult<PyTuple> {
606+
// FIXME: the given fmt must be parsed as ascii string
607+
// https://github.com/RustPython/RustPython/pull/1792#discussion_r387340905
608+
let parsed = match fmt {
609+
Either::A(string) => FormatSpec::parse(string.as_str()),
610+
Either::B(bytes) => FormatSpec::parse(std::str::from_utf8(&bytes).unwrap()),
611+
};
612+
let format_spec = parsed.map_err(|e| new_struct_error(vm, e))?;
604613
format_spec.unpack(buffer.get_value(), vm)
605614
}
606615

0 commit comments

Comments
 (0)