lua-assembler

Crates.iolua-assembler
lib.rslua-assembler
version0.0.0
created_at2025-10-16 05:49:15.599348+00
updated_at2025-10-16 05:49:15.599348+00
description解析以及写入 `.pyc` bytecode files
homepage
repositoryhttps://github.com/oovm/wasm.exe
max_upload_size
id1885469
size38,226
publisher (github:nyar-vm:publisher)

documentation

https://docs.rs/wasm-exe

README

lua-assembler

用于读取/写入 Python .pyc 字节码文件的 Rust 实现。

功能

  • 读取 .pyc 文件:解析 16 字节头(PEP 552)并保留主体
  • 写入 .pyc 文件:按原样写回头与主体,实现无损回写
  • 命令行工具:支持 pyc-assembler <in.pyc> <out.pyc> 进行回写验证

快速开始

构建

cargo build -p pyc-assembler

生成示例 .pyc(需要已安装 Python)

python - <<'PY'
import py_compile, pathlib
p = pathlib.Path('tests/pyc_src')
p.mkdir(parents=True, exist_ok=True)
(p/'hello.py').write_text('print("hello from pyc")\n')
py_compile.compile(str(p/'hello.py'), cfile=str(p/'hello.pyc'))
print('OK')
PY

回写并执行验证

cargo run -p pyc-assembler -- tests/pyc_src/hello.pyc tests/pyc_src/out.pyc
python tests/pyc_src/out.pyc
# 预期输出:hello from pyc

API 示例

use std::path::Path;
use pyc_assembler::formats::pyc::{read_pyc_file, write_pyc_file};

let pyc = read_pyc_file(Path::new("tests/pyc_src/hello.pyc")).unwrap();
write_pyc_file(Path::new("tests/pyc_src/out.pyc"), &pyc).unwrap();

说明

.pyc 主体为 Python 的 marshal 序列化的 code object,本库当前不解析主体,仅做无损读写;这足以用于执行验证与后续扩展。

Commit count: 6

cargo fmt