-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow dis.dis
to take file
#6078
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant VM
participant dis
participant disassemble_to_file
participant FileObj
participant Stdout
Caller->>dis: call with DisArgs (obj, optional file)
dis->>disassemble_to_file: delegate with code object, file, vm
alt file is provided
disassemble_to_file->>FileObj: call write(output)
alt FileObj has write
FileObj-->>disassemble_to_file: write succeeds
else FileObj lacks write
disassemble_to_file-->>VM: raise TypeError
end
else no file provided
disassemble_to_file->>Stdout: print output
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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 (1)
stdlib/src/dis.rs (1)
58-82
: Consider flushing stdout and improving error handling.The implementation is functionally correct, but there are a few areas for improvement:
- Stdout flushing: The
print!
macro doesn't automatically flush, which may cause delayed output in some scenarios.- Error handling: The current approach is adequate but could provide more context.
Consider this improvement for better stdout handling:
None => { // Write to stdout print!("{output}"); + use std::io::Write; + std::io::stdout().flush().ok(); }The error handling for the file object is appropriate and follows Python conventions by checking for the
write
method's existence.
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
stdlib/src/dis.rs
(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs
📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)
**/*.rs
: Follow the default rustfmt code style (cargo fmt
to format)
Always run clippy to lint code (cargo clippy
) before completing tasks. Fix any warnings or lints that are introduced by your 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:
stdlib/src/dis.rs
🧠 Learnings (1)
📚 Learning: applies to **/*.rs : use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implement...
Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-07-19T03:16:56.511Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust
Applied to files:
stdlib/src/dis.rs
🔇 Additional comments (5)
stdlib/src/dis.rs (5)
5-10
: LGTM: Import additions are appropriate.The added imports
OptionalArg
and the reorganization of imports follow Rust conventions and support the new functionality correctly.
12-18
: Well-structured argument handling with proper PyO3 macros.The
DisArgs
struct properly uses the#[derive(FromArgs)]
macro with appropriate argument attributes. The#[pyarg(any, optional)]
correctly allows thefile
parameter to be passed as a keyword argument.
21-22
: Function signature change maintains backward compatibility.The restructured
dis
function signature usingDisArgs
maintains the same Python interface while enabling the new file parameter functionality. The delegation todisassemble_to_file
is clean and appropriate.Also applies to: 46-46
50-56
: Enhanceddisassemble
function signature follows Python conventions.The updated signature with
OptionalArg<PyObjectRef>
for the file parameter correctly matches Python'sdis.disassemble
behavior and properly delegates to the shared implementation.
58-82
: Implementation correctly mirrors Python's dis module behavior.The
disassemble_to_file
function properly implements the expected behavior:
- Accepts optional file objects with
write()
methods- Falls back to stdout when no file is provided
- Raises appropriate
TypeError
for invalid file objects- Maintains consistency with Python's
dis
module interface
Summary by CodeRabbit
New Features
Bug Fixes