-
Notifications
You must be signed in to change notification settings - Fork 1.4k
pythonrun #6643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pythonrun #6643
Conversation
📝 WalkthroughWalkthroughRuntime execution methods were moved out of Changes
Sequence Diagram(s)sequenceDiagram
participant CLI as CLI / src/lib.rs
participant VM as VirtualMachine
participant FS as Filesystem
participant ImportLib as importlib (path hooks/cache)
participant Compiler as Compiler
Note over CLI,VM: CLI decides run_file or run_string
CLI->>VM: run_file(path) or run_string(source)
alt run_file -> directory with __main__.py
VM->>ImportLib: get_importer(path)
ImportLib->>VM: importer (maybe package)
VM->>FS: read "__main__.py"
FS-->>VM: source bytes
VM->>Compiler: compile source -> code object
Compiler-->>VM: code object
VM->>VM: set_main_loader(...) (set __loader__, __file__, __cached__)
VM->>VM: execute code object in module dict
VM->>CLI: return / propagate exceptions
else run_file -> simple file or run_string
VM->>FS: read file (or take provided source)
FS-->>VM: source bytes
VM->>Compiler: compile source -> code object
Compiler-->>VM: code object
VM->>VM: execute code object in scope (flush_io on finish)
VM->>CLI: return / propagate exceptions
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
crates/vm/src/vm/python_run.rs (1)
115-118: Consider uncommenting or removing the deprecation notice.The commented-out
#[deprecated]attribute suggests this method may be deprecated in the future. Either uncomment it to signal intent to callers, or remove the comment entirely if deprecation is not planned.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
crates/vm/src/vm/compile.rscrates/vm/src/vm/mod.rscrates/vm/src/vm/python_run.rssrc/lib.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
crates/vm/src/vm/mod.rssrc/lib.rscrates/vm/src/vm/python_run.rscrates/vm/src/vm/compile.rs
🧠 Learnings (5)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: RustPython is a Python 3 interpreter written in Rust implementing Python 3.13.0+ compatibility with complete Python environment in Rust (not CPython bindings), clean implementation without compatibility hacks, and cross-platform support including WebAssembly compilation
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When modifying bytecode instructions, perform a full clean build by running `rm -r target/debug/build/rustpython-* && find . | grep -E '\.pyc$' | xargs rm -r`
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When testing Python code, always use RustPython instead of the standard `python` command; use `cargo run -- script.py` or `cargo run` for interactive REPL
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
crates/vm/src/vm/mod.rssrc/lib.rscrates/vm/src/vm/python_run.rscrates/vm/src/vm/compile.rs
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: RustPython is a Python 3 interpreter written in Rust implementing Python 3.13.0+ compatibility with complete Python environment in Rust (not CPython bindings), clean implementation without compatibility hacks, and cross-platform support including WebAssembly compilation
Applied to files:
crates/vm/src/vm/mod.rssrc/lib.rscrates/vm/src/vm/python_run.rs
📚 Learning: 2025-06-27T14:47:28.810Z
Learnt from: moreal
Repo: RustPython/RustPython PR: 5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.810Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration using #ifdef checks rather than providing fallback values for other platforms.
Applied to files:
crates/vm/src/vm/mod.rs
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When testing Python code, always use RustPython instead of the standard `python` command; use `cargo run -- script.py` or `cargo run` for interactive REPL
Applied to files:
crates/vm/src/vm/mod.rssrc/lib.rscrates/vm/src/vm/python_run.rs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (windows-latest)
🔇 Additional comments (13)
crates/vm/src/vm/compile.rs (1)
1-9: LGTM!Clean separation of concerns. The module now focuses solely on compilation, with a helpful doc comment directing users to
python_run.rsfor execution functions. The imports are appropriately minimal for the remaining functionality.crates/vm/src/vm/mod.rs (1)
11-12: LGTM!The new
python_runmodule is correctly feature-gated withrustpython-compiler, consistent with thecompilemodule. This ensures execution functionality is only available when compilation is enabled.src/lib.rs (4)
52-52: LGTM!Import additions are appropriate for the new functionality.
189-218: LGTM!The
get_importerfunction correctly mirrors CPython's path importer resolution logic: checking the cache first, iterating throughpath_hooks, catchingImportErrorto try the next hook, and caching successful results.
277-292: LGTM!The execution flow correctly uses the new APIs:
run_stringfor-ccommand execution with the main scoperun_filehelper for script/directory execution with proper importer detection
366-381: LGTM!Tests appropriately cover both execution paths: direct file execution via
run_any_fileand directory module execution viarun_file.crates/vm/src/vm/python_run.rs (7)
1-8: LGTM!Clear module purpose documented. Imports are appropriate for the execution functionality.
23-54: LGTM!The
run_simple_fileimplementation correctly:
- Tracks whether
__file__was set (for proper cleanup)- Sets both
__file__and__cached__attributes- Flushes IO after execution regardless of result
- Cleans up attributes after execution (silently ignoring errors, matching CPython behavior)
56-90: LGTM!The inner execution logic properly handles both
.pycfiles (viaSourcelessFileLoader.get_code) and source files (via compilation). The<stdin>special case correctly skips loader setup, and error mapping is appropriate.
92-113: LGTM!Clean API design following CPython naming conventions (
PyRun_SimpleString,PyRun_String). The deprecated alias maintains backward compatibility.
127-137: LGTM!Correctly flushes both stdout and stderr, silently ignoring errors to match CPython's cleanup behavior.
140-154: LGTM!The
set_main_loaderfunction correctly sets up the appropriate loader (SourceFileLoaderorSourcelessFileLoader) for the__main__module, matching CPython's behavior.
156-186: LGTM!The pyc detection is well-implemented:
- Fast path via extension check
- Fallback to magic number comparison (only first 2 bytes, as documented)
- Handles edge cases: directories, missing files, and short files
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/lib.rs (2)
168-174: Preferis_some()over unused pattern binding.The
_importervariable is bound but never used. Consider a more idiomatic check:🔎 Proposed refactoring
- if let Some(_importer) = get_importer(path, vm)? { + if get_importer(path, vm)?.is_some() { vm.insert_sys_path(vm.new_pyobj(path))?; let runpy = vm.import("runpy", 0)?; let run_module_as_main = runpy.get_attr("_run_module_as_main", vm)?; run_module_as_main.call((vm::identifier!(vm, __main__).to_owned(), false), vm)?; return Ok(()); }
172-172: Add comment explaining thefalseparameter.The second parameter to
_run_module_as_main(which corresponds toalter_sysin Python's runpy module) lacks explanation. A brief comment would improve readability.🔎 Proposed addition
+ // alter_sys=false: don't modify sys.argv for module execution run_module_as_main.call((vm::identifier!(vm, __main__).to_owned(), false), vm)?;
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/lib.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit inference engine (.github/copilot-instructions.md)
**/*.rs: Follow the default rustfmt code style by runningcargo fmtto format Rust code
Always run clippy to lint code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass,pymodule,pyfunction, etc.) when implementing Python functionality in Rust
Files:
src/lib.rs
🧠 Learnings (5)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: RustPython is a Python 3 interpreter written in Rust implementing Python 3.13.0+ compatibility with complete Python environment in Rust (not CPython bindings), clean implementation without compatibility hacks, and cross-platform support including WebAssembly compilation
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When testing Python code, always use RustPython instead of the standard `python` command; use `cargo run -- script.py` or `cargo run` for interactive REPL
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When modifying bytecode instructions, perform a full clean build by running `rm -r target/debug/build/rustpython-* && find . | grep -E '\.pyc$' | xargs rm -r`
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Follow Rust best practices for error handling and memory management
Applied to files:
src/lib.rs
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: When testing Python code, always use RustPython instead of the standard `python` command; use `cargo run -- script.py` or `cargo run` for interactive REPL
Applied to files:
src/lib.rs
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: RustPython is a Python 3 interpreter written in Rust implementing Python 3.13.0+ compatibility with complete Python environment in Rust (not CPython bindings), clean implementation without compatibility hacks, and cross-platform support including WebAssembly compilation
Applied to files:
src/lib.rs
📚 Learning: 2025-12-27T14:03:49.034Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-12-27T14:03:49.034Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
src/lib.rs
🧬 Code graph analysis (1)
src/lib.rs (1)
crates/vm/src/stdlib/sys.rs (4)
path(520-528)std(900-900)path_importer_cache(536-538)path_hooks(531-533)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
- GitHub Check: Run rust tests (macos-latest)
- GitHub Check: Check Rust code with clippy
- GitHub Check: Run snippets and cpython tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests (macos-latest)
- GitHub Check: Run rust tests (windows-latest)
- GitHub Check: Run rust tests (ubuntu-latest)
- GitHub Check: Run snippets and cpython tests on wasm-wasi
- GitHub Check: Run snippets and cpython tests (windows-latest)
- GitHub Check: Check the WASM package and demo
- GitHub Check: Ensure compilation on various targets
- GitHub Check: Run tests under miri
🔇 Additional comments (8)
src/lib.rs (8)
52-52: LGTM: Imports align with new functionality.The added imports support the new execution helpers and updated VM APIs.
147-147: LGTM: API update applied correctly.The change from
run_code_stringtorun_stringis consistent with the new VM API.
176-183: Past review concern addressed.The code now uses
.and_then(|p| p.to_str()).unwrap_or("")which safely handles edge cases whereparent()returnsNone(root paths) orto_str()fails (non-UTF-8 paths). This matches the previously suggested fix.
256-256: LGTM: API update applied correctly.The change from
run_code_stringtorun_simple_stringis appropriate for this simple one-liner initialization.
279-279: LGTM: API update applied correctly.The change to
run_stringis consistent with the new VM API.
287-290: LGTM: Script execution now uses the new helper.The delegation to
run_fileproperly handles both package/directory and regular file execution, matching CPython'spymain_run_file_objbehavior.
371-375: LGTM: Tests validate both execution modes.The tests appropriately verify direct file execution and directory module execution with
__main__.py, covering the newrun_filefunctionality.
188-217:sys.path_hooksis already properly initialized during VM startup.The code does not rely on
site.py(which doesn't exist in this repository) to populatepath_hooks. Instead, initialization happens automatically ininit_importlib_packageduring VM construction, whereimportlib._install_external_importers()is called andzipimporteris registered as the first path hook. This occurs before any user code—includingget_importer—is executed. Additionally,get_importersafely handles empty path hooks by falling back tovm.run_any_file.Likely an incorrect or invalid review comment.
Summary by CodeRabbit
Refactor
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.