Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

youknowone
Copy link
Member

@youknowone youknowone commented Aug 6, 2025

Summary by CodeRabbit

  • New Features

    • Added support for specifying an optional file-like object to receive disassembly output, in addition to standard output.
  • Bug Fixes

    • Improved error handling when an invalid file object is provided for disassembly output.

Copy link
Contributor

coderabbitai bot commented Aug 6, 2025

Walkthrough

The dis and disassemble functions in the stdlib/src/dis.rs module have been refactored to accept an optional output file parameter. A new disassemble_to_file function centralizes the logic for formatting and outputting disassembly, either to a provided file-like object or standard output.

Changes

Cohort / File(s) Change Summary
Disassembly API Refactor
stdlib/src/dis.rs
Updated dis and disassemble functions to accept an optional file argument; introduced disassemble_to_file to handle output logic; adjusted function signatures and internal delegation accordingly.

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A hop and a skip, the code now can write
Its secrets to files, not just to the light.
With disassemble_to_file, output takes flight—
To paper or screen, both options are right!
🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • 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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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:

  1. Stdout flushing: The print! macro doesn't automatically flush, which may cause delayed output in some scenarios.
  2. 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

📥 Commits

Reviewing files that changed from the base of the PR and between 93eacda and 7d724bb.

📒 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 the file parameter to be passed as a keyword argument.


21-22: Function signature change maintains backward compatibility.

The restructured dis function signature using DisArgs maintains the same Python interface while enabling the new file parameter functionality. The delegation to disassemble_to_file is clean and appropriate.

Also applies to: 46-46


50-56: Enhanced disassemble function signature follows Python conventions.

The updated signature with OptionalArg<PyObjectRef> for the file parameter correctly matches Python's dis.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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant