Skip to content

Commit 197ef9c

Browse files
committed
Fix struct.calcsize to take bytes argument
1 parent 7f7cad3 commit 197ef9c

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

vm/src/stdlib/pystruct.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::function::Args;
1818
use crate::obj::{
1919
objbytes::PyBytesRef, objstr::PyStringRef, objtuple::PyTuple, objtype::PyClassRef,
2020
};
21-
use crate::pyobject::{PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
21+
use crate::pyobject::{Either, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
2222
use crate::VirtualMachine;
2323

2424
#[derive(Debug)]
@@ -414,9 +414,12 @@ where
414414
}
415415
}
416416

417-
fn struct_calcsize(fmt: PyStringRef, vm: &VirtualMachine) -> PyResult<usize> {
418-
let fmt_str = fmt.as_str();
419-
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| vm.new_value_error(e))?;
417+
fn struct_calcsize(fmt: Either<PyStringRef, PyBytesRef>, vm: &VirtualMachine) -> PyResult<usize> {
418+
let parsed = match fmt {
419+
Either::A(string) => FormatSpec::parse(string.as_str()),
420+
Either::B(bytes) => FormatSpec::parse(std::str::from_utf8(&bytes).unwrap()),
421+
};
422+
let format_spec = parsed.map_err(|e| vm.new_value_error(e))?;
420423
Ok(format_spec.size())
421424
}
422425

0 commit comments

Comments
 (0)