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

Method from_pyc

crates/vm/src/builtins/code.rs:444–467  ·  view source on GitHub ↗
(
        pyc_bytes: &[u8],
        name: Option<&str>,
        bytecode_path: Option<&str>,
        source_path: Option<&str>,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

442 )
443 }
444 pub fn from_pyc(
445 pyc_bytes: &[u8],
446 name: Option<&str>,
447 bytecode_path: Option<&str>,
448 source_path: Option<&str>,
449 vm: &VirtualMachine,
450 ) -> PyResult<PyRef<Self>> {
451 if !crate::import::check_pyc_magic_number_bytes(pyc_bytes) {
452 return Err(vm.new_value_error("pyc bytes has wrong MAGIC"));
453 }
454 let bootstrap_external = vm.import("_frozen_importlib_external", 0)?;
455 let compile_bytecode = bootstrap_external.get_attr("_compile_bytecode", vm)?;
456 // 16 is the pyc header length
457 let Some((_, code_bytes)) = pyc_bytes.split_at_checked(16) else {
458 return Err(vm.new_value_error(format!(
459 "pyc_bytes header is broken. 16 bytes expected but {} bytes given.",
460 pyc_bytes.len()
461 )));
462 };
463 let code_bytes_obj = vm.ctx.new_bytes(code_bytes.to_vec());
464 let compiled =
465 compile_bytecode.call((code_bytes_obj, name, bytecode_path, source_path), vm)?;
466 compiled.try_downcast(vm)
467 }
468}
469
470impl fmt::Debug for PyCode {

Callers

nothing calls this directly

Calls 8

to_vecMethod · 0.80
try_downcastMethod · 0.80
ErrClass · 0.50
importMethod · 0.45
get_attrMethod · 0.45
new_bytesMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected