Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 9, 2025

Summary by CodeRabbit

  • New Features
    • File I/O objects now properly clean up resources when destroyed.
    • TextIOWrapper now supports iteration operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 9, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

This pull request adds Destructor trait implementations to six IO classes in RustPython's stdlib module, enabling proper resource cleanup when these objects are garbage collected. Each class now declares Destructor in its pyclass declaration and implements slot_del to call close(). TextIOWrapper additionally gains Iterable and IterNext implementations.

Changes

Cohort / File(s) Summary
Destructor trait additions to IO classes
crates/vm/src/stdlib/io.rs
Added Destructor to pyclass declarations for BufferedReader, BufferedWriter, BufferedRandom, BufferedRWPair, and FileIO. Each now includes corresponding Destructor impl block with slot_del invoking close().
TextIOWrapper enhancement
crates/vm/src/stdlib/io.rs
Extended TextIOWrapper pyclass declaration to include Destructor, Iterable, and IterNext. Implemented matching Destructor and iteration trait blocks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

  • Highly repetitive pattern applied consistently across six IO classes (BufferedReader, BufferedWriter, BufferedRandom, BufferedRWPair, TextIOWrapper, FileIO)
  • Straightforward destructor logic that invokes close() via slot_del
  • TextIOWrapper adds Iterable/IterNext implementations alongside Destructor

Areas requiring extra attention:

  • Verify each Destructor impl properly chains to close() without resource leaks or double-close scenarios
  • Confirm TextIOWrapper's Iterable/IterNext implementations align with CPython iteration semantics
  • Check that del paths marked as unreachable are indeed unreachable after slot_del implementation

Possibly related PRs

  • RustPython/RustPython#6387: Directly modifies the same io.rs pyclass declarations, adding Destructor and related trait implementations to identical IO classes.

Poem

🐰 A rabbit hops through files with glee,
Adding Destructors, clean as can be,
When resources end their day,
Close() is called right away,
No leaks for this bunny to see! 🌟

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 47.62% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix test_io on windows' directly aligns with the main change: removing test_io from Windows-specific test skips in CI workflow, making the test run on Windows again.

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a47ecd7 and de11481.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_io.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/io.rs (10 hunks)

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.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2025

Code has been automatically formatted

The code in this PR has been formatted using cargo fmt --all.
Please pull the latest changes before pushing again:

git pull origin test-io

@youknowone youknowone marked this pull request as ready for review December 10, 2025 00:14
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)
crates/vm/src/stdlib/io.rs (1)

1761-1771: Consider a macro for Destructor implementations (optional).

There are now 7 nearly identical Destructor implementations. A macro could reduce duplication:

macro_rules! impl_io_destructor {
    ($type:ty) => {
        impl Destructor for $type {
            fn slot_del(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
                let _ = vm.call_method(zelf, "close", ());
                Ok(())
            }

            #[cold]
            fn del(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
                unreachable!("slot_del is implemented")
            }
        }
    };
}

However, this is entirely optional — the current explicit implementations are clear and maintainable.

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac48643 and a47ecd7.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_io.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • .github/workflows/ci.yaml (0 hunks)
  • crates/vm/src/stdlib/io.rs (10 hunks)
💤 Files with no reviewable changes (1)
  • .github/workflows/ci.yaml
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust 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/stdlib/io.rs
🧠 Learnings (1)
📚 Learning: 2025-11-29T12:17:28.606Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to **/*.rs : Use the macro system (`pyclass`, `pymodule`, `pyfunction`, etc.) when implementing Python functionality in Rust

Applied to files:

  • crates/vm/src/stdlib/io.rs
🔇 Additional comments (8)
crates/vm/src/stdlib/io.rs (8)

1755-1771: LGTM!

The Destructor implementation for BufferedReader correctly follows the established pattern from _IOBase, properly swallowing errors from close() which is appropriate behavior for a destructor.


1824-1840: LGTM!

Consistent Destructor implementation for BufferedWriter.


1878-1900: LGTM!

Consistent Destructor implementation for BufferedRandom.


1944-2003: LGTM!

Consistent Destructor implementation for BufferedRWPair.


2476-2479: LGTM!

The pyclass declaration correctly includes Destructor, Iterable, and IterNext traits.


3342-3391: LGTM!

The TextIOWrapper implementations are well-designed:

  • Destructor: Follows the established pattern.
  • Iterable: Correctly checks if closed before returning self as iterator.
  • IterNext: Properly sets telling = false during iteration (matching CPython behavior), releases the lock during the readline call to avoid holding it during I/O, and correctly restores state on StopIteration.

4284-4292: LGTM!

The necessary imports for Destructor and related types are correctly added.


4551-4779: LGTM!

The FileIO Destructor implementation is consistent with the other I/O types and correctly ensures proper cleanup.

@youknowone youknowone merged commit 30fb9d7 into RustPython:main Dec 10, 2025
12 of 13 checks passed
@youknowone youknowone deleted the test-io branch December 10, 2025 00:21
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