MCPcopy Index your code
hub / github.com/RustPython/RustPython / unpack

Method unpack

crates/vm/src/buffer.rs:484–523  ·  view source on GitHub ↗
(&self, mut data: &[u8], vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

482 }
483
484 pub fn unpack(&self, mut data: &[u8], vm: &VirtualMachine) -> PyResult<PyTupleRef> {
485 if self.size != data.len() {
486 return Err(new_struct_error(
487 vm,
488 format!("unpack requires a buffer of {} bytes", self.size),
489 ));
490 }
491
492 let mut items = Vec::with_capacity(self.arg_count);
493 for code in &self.codes {
494 data = &data[code.pre_padding..];
495 debug!("unpack code: {code:?}");
496 match code.code {
497 FormatType::Pad => {
498 data = &data[code.repeat..];
499 }
500 FormatType::Str => {
501 let (str_data, rest) = data.split_at(code.repeat);
502 // string is just stored inline
503 items.push(vm.ctx.new_bytes(str_data.to_vec()).into());
504 data = rest;
505 }
506 FormatType::Pascal => {
507 let (str_data, rest) = data.split_at(code.repeat);
508 items.push(unpack_pascal(vm, str_data));
509 data = rest;
510 }
511 _ => {
512 let unpack = code.info.unpack.unwrap();
513 for _ in 0..code.repeat {
514 let (item_data, rest) = data.split_at(code.info.size);
515 items.push(unpack(vm, item_data));
516 data = rest;
517 }
518 }
519 };
520 }
521
522 Ok(PyTuple::new_ref(items, &vm.ctx))
523 }
524
525 #[inline]
526 pub const fn size(&self) -> usize {

Callers 15

parseMethod · 0.45
_get_sizeMethod · 0.45
_read_intsMethod · 0.45
_read_objectMethod · 0.45
_85encodeFunction · 0.45
_read_gzip_headerFunction · 0.45
_read_eofMethod · 0.45
decompressFunction · 0.45
handleMethod · 0.45
test_transitivenessMethod · 0.45
test_new_featuresMethod · 0.45
test_p_codeMethod · 0.45

Calls 12

new_struct_errorFunction · 0.85
unpack_pascalFunction · 0.85
unpackFunction · 0.85
to_vecMethod · 0.80
to_f64Method · 0.80
ErrClass · 0.50
lenMethod · 0.45
pushMethod · 0.45
new_bytesMethod · 0.45
unwrapMethod · 0.45
to_pyobjectMethod · 0.45
new_boolMethod · 0.45

Tested by 15

test_transitivenessMethod · 0.36
test_new_featuresMethod · 0.36
test_p_codeMethod · 0.36
test_705836Method · 0.36
test_boolMethod · 0.36
test_issue29802Method · 0.36
test_half_floatMethod · 0.36
fromfileMethod · 0.36
get_cidFunction · 0.36
dissect_can_frameMethod · 0.36