use pyc_editor::{
PycFile, dump_pyc, load_pyc,
prelude::*,
v310::{ext_instructions::ExtInstruction, opcodes::Opcode},
};
fn main() {
let data = include_bytes!("./pyc_files/modify.cpython-310.pyc");
let mut pyc_file = load_pyc(data.as_slice()).expect("Invalid pyc file");
if let PycFile::V310(ref mut pyc) = pyc_file {
let mut resolved =
pyc.code_object.code.to_resolved().expect(
"This code is generated by the Python compiler and should never fail here.",
);
let (index, &call) = resolved
.iter()
.enumerate()
.find(|(_, i)| i.get_opcode() == Opcode::CALL_FUNCTION)
.expect("Call not found");
resolved.insert_instructions(
index,
&[
ExtInstruction::DupTop(0.into()), call, ],
);
pyc.code_object.code = resolved.to_instructions();
}
dbg!(&pyc_file);
let _out = dump_pyc(pyc_file).expect("Invalid pyc file");
}