◐ Shell
clean mode source ↗

Implement maybe_pyc_file and .pyc file execution by youknowone · Pull Request #6539 · RustPython/RustPython

📝 Walkthrough

Walkthrough

Adds CPython-aligned .pyc magic constants, detects and executes .pyc files via a sourceless loader in the compiler path, removes dynamic git-derived MAGIC_NUMBER setup in import initialization, and exposes the pyc magic token to Python via the _imp module attribute.

Changes

Cohort / File(s) Summary
PYC magic constants
crates/vm/src/version.rs
Added public constants: PYC_MAGIC_NUMBER: u16, PYC_MAGIC_NUMBER_TOKEN: u32, and PYC_MAGIC_NUMBER_BYTES: [u8;4].
Pyc detection & execution
crates/vm/src/vm/compile.rs
Added maybe_pyc_file (and maybe_pyc_file_with_magic) to detect .pyc by extension/magic; branched execution to use SourcelessFileLoader for .pyc and SourceFileLoader for source; updated set_main_loader to accept loader_name.
Import init cleanup
crates/vm/src/import.rs
Removed code that computed/assigned a dynamic 4-byte MAGIC_NUMBER from the git revision and the get_git_revision import; zipimport initialization unchanged.
Expose magic to Python
crates/vm/src/stdlib/imp.rs
Added pyc_magic_number_token attribute to the _imp module, sourced from version::PYC_MAGIC_NUMBER_TOKEN.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant CLI as RustPython CLI
    participant Detector as Pyc Detector (compile.rs)
    participant Loader as Import Loader (Sourceless/SourceFileLoader)
    participant VM as VirtualMachine

    CLI->>Detector: execute_file(path)
    Detector->>Detector: check extension and PYC magic
    alt .pyc detected
        Detector->>Loader: set_main_loader(..., "SourcelessFileLoader")
        Loader->>VM: get_code(module_name="__main__")
        VM->>VM: execute PyCode (bytecode)
    else source file
        Detector->>Loader: set_main_loader(..., "SourceFileLoader")
        Loader->>Loader: read source -> compile to PyCode
        Loader->>VM: run compiled PyCode
    end
    VM-->>CLI: execution result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐇
I sniffed the bytes and found the cue,
Magic stamped in four-byte blue,
Sourceless hops — the bytecode runs,
A rabbit's cheer for faster fun! 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.78% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly summarizes the main change: implementing maybe_pyc_file function and enabling .pyc file execution, which directly matches the file-level changes in compile.rs and the PR objectives.
Linked Issues check ✅ Passed All coding requirements from issue #6273 are met: the PR detects .pyc files via maybe_pyc_file, handles .pyc execution with SourcelessFileLoader, and executes compiled code appropriately.
Out of Scope Changes check ✅ Passed All changes are scoped to .pyc file support: version.rs adds magic number constants, compile.rs implements detection and execution, import.rs removes dynamic magic number setup, and imp.rs exposes the constant to Python.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.