Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 28, 2025

Summary by CodeRabbit

  • New Features

    • os.unlink() exposed consistently across platforms (Windows, POSIX, compatibility layers).
    • os.chmod() now supports file-descriptor-based operations on Windows (fd-based chmod).
  • Performance Improvements

    • Directory/file type checks use cached metadata when available, avoiding extra filesystem lookups.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 28, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Relocates unlink to platform-specific modules (Windows and POSIX), adds Windows fd-based chmod support (fchmod_impl) with HANDLE-based attribute updates, and optimizes DirEntry::is_dir/is_file to use cached file_type short-circuits.

Changes

Cohort / File(s) Summary
Windows stdlib changes
crates/vm/src/stdlib/nt.rs
Added remove (exposed as unlink) that chooses RemoveDirectoryW vs DeleteFileW; introduced fd-aware chmod: ChmodArgs<'a> uses OsPathOrFd<'a>, added fchmod_impl and fchmod, S_IWRITE constant, and updated chmod to route fd paths to fchmod_impl. Error mapping to Python exceptions added.
POSIX unlink additions
crates/vm/src/stdlib/posix.rs, crates/vm/src/stdlib/posix_compat.rs
Added remove (exposed as unlink) implementations that call fs::remove_file, validate empty dir_fd, and map IO errors to Python OSError. Both modules expose unlink alias.
Core os module adjustments
crates/vm/src/stdlib/os.rs
Removed the cross-platform Python-facing unlink wrapper. Modified DirEntry::is_dir and DirEntry::is_file to early-return using cached file_type when present (with adjusted NotFound => false behavior and related comments).

Sequence Diagram(s)

sequenceDiagram
  participant Py as Python caller
  participant VM as Rust VM (nt.rs)
  participant WinAPI as Windows API / Kernel

  Note left of Py: call os.chmod(path_or_fd, mode, follow_symlinks?)
  Py->>VM: chmod(args)
  alt path argument
    VM->>VM: resolve path, preserve existing path-based flow
    VM->>WinAPI: GetFileAttributesExW / SetFileAttributesW (existing flow)
    WinAPI-->>VM: status
    VM-->>Py: map to Ok or OSError
  else fd argument
    VM->>VM: match OsPathOrFd::Fd(fd)
    VM->>WinAPI: _get_osfhandle(fd) → HANDLE
    WinAPI-->>VM: HANDLE
    VM->>WinAPI: GetFileInformationByHandleEx / SetFileInformationByHandle (toggle read-only)
    WinAPI-->>VM: status
    VM-->>Py: map to Ok or OSError / ValueError (if follow_symlinks unsupported)
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

🐰
Files hop to platform homes with delight,
Windows handles toggle read-only right.
POSIX removes with a skip and a cheer,
Cached DirEntry makes lookups clear,
A rabbit nods — unlink found its sight. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'upgrade shutil' is vague and generic, failing to describe the specific changes made to the codebase. Consider a more descriptive title such as 'Add unlink and fchmod support across os modules' or 'Implement file removal and chmod for file descriptors in Windows/POSIX modules' to clarify the main changes.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 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 308ac1d and 3f89316.

⛔ Files ignored due to path filters (2)
  • Lib/shutil.py is excluded by !Lib/**
  • Lib/test/test_shutil.py is excluded by !Lib/**
📒 Files selected for processing (4)
  • crates/vm/src/stdlib/nt.rs
  • crates/vm/src/stdlib/os.rs
  • crates/vm/src/stdlib/posix.rs
  • crates/vm/src/stdlib/posix_compat.rs

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.

@youknowone youknowone marked this pull request as ready for review December 28, 2025 06:45
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: 1

📜 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 04a4d1e and 308ac1d.

⛔ Files ignored due to path filters (2)
  • Lib/shutil.py is excluded by !Lib/**
  • Lib/test/test_shutil.py is excluded by !Lib/**
📒 Files selected for processing (4)
  • crates/vm/src/stdlib/nt.rs
  • crates/vm/src/stdlib/os.rs
  • crates/vm/src/stdlib/posix.rs
  • crates/vm/src/stdlib/posix_compat.rs
🧰 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 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/posix_compat.rs
  • crates/vm/src/stdlib/os.rs
  • crates/vm/src/stdlib/nt.rs
  • crates/vm/src/stdlib/posix.rs
🧬 Code graph analysis (3)
crates/vm/src/stdlib/posix_compat.rs (1)
crates/vm/src/stdlib/posix.rs (2)
  • access (399-431)
  • remove (480-483)
crates/vm/src/stdlib/nt.rs (3)
crates/vm/src/stdlib/winapi.rs (1)
  • std (455-455)
crates/vm/src/ospath.rs (1)
  • with_filename (306-316)
crates/common/src/crt_fd.rs (3)
  • borrow_raw (101-106)
  • borrow_raw (248-251)
  • as_handle (342-346)
crates/vm/src/stdlib/posix.rs (1)
crates/vm/src/stdlib/posix_compat.rs (1)
  • remove (30-33)
🔇 Additional comments (6)
crates/vm/src/stdlib/posix.rs (1)

478-483: LGTM!

The unlink implementation correctly validates the dir_fd and provides proper error context by including the filename in the error message via OSErrorBuilder.

crates/vm/src/stdlib/os.rs (1)

564-601: LGTM! Good performance optimization.

The early-return optimization using cached file_type is well-designed:

  • Avoids unnecessary stat() calls that may fail if the file was removed after the directory scan
  • Correctly handles the follow_symlinks parameter: cached data is used when not following symlinks, or when the entry is not a symlink
  • The pattern is consistently applied in both is_dir() and is_file() methods
crates/vm/src/stdlib/nt.rs (4)

80-130: LGTM! Windows-specific unlink implementation is well-structured.

The implementation correctly handles Windows-specific file types:

  • Regular files are removed with DeleteFileW
  • Directory symlinks and junctions are removed with RemoveDirectoryW
  • Reparse point detection using FindFirstFileW and dwReserved0 aligns with CPython's approach
  • Proper cleanup with FindClose after checking reparse tags
  • Error messages include filename context via OSErrorBuilder::with_filename

207-258: LGTM! File descriptor-based chmod implementation is correct.

The fchmod_impl function properly implements Windows chmod for file descriptors:

  • Correctly obtains the Windows HANDLE from the file descriptor
  • Uses GetFileInformationByHandleEx with FileBasicInfo to read current attributes
  • The readonly attribute logic is correct: S_IWRITE bit clears FILE_ATTRIBUTE_READONLY, absence sets it
  • Properly uses SetFileInformationByHandle to apply changes
  • Error handling appropriately returns OS errors

261-278: Good integration of fd support into chmod.

The function correctly handles both file descriptor and path inputs:

  • File descriptor case is handled early with a direct call to fchmod_impl
  • Appropriately rejects follow_symlinks parameter when used with file descriptor (as it's meaningless for fds)
  • The error message clearly explains why the combination is invalid

320-320: Correct readonly attribute logic.

The expression mode & S_IWRITE == 0 correctly determines when to set the readonly attribute (when write permission is absent), matching the logic in fchmod_impl.

Comment on lines +28 to +33
#[pyfunction]
#[pyfunction(name = "unlink")]
fn remove(path: OsPath, dir_fd: DirFd<'_, 0>, vm: &VirtualMachine) -> PyResult<()> {
let [] = dir_fd.0;
fs::remove_file(&path).map_err(|err| err.into_pyexception(vm))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Consider using OSErrorBuilder for consistency and better error messages.

The error handling here uses IntoPyException directly, while the equivalent function in posix.rs (lines 479-482) uses OSErrorBuilder::with_filename(&err, path, vm) to include the filename in error messages. This inconsistency means users on posix_compat platforms will get less helpful error messages.

🔎 Suggested fix
+use crate::exceptions::OSErrorBuilder;

 #[pyfunction]
 #[pyfunction(name = "unlink")]
 fn remove(path: OsPath, dir_fd: DirFd<'_, 0>, vm: &VirtualMachine) -> PyResult<()> {
     let [] = dir_fd.0;
-    fs::remove_file(&path).map_err(|err| err.into_pyexception(vm))
+    fs::remove_file(&path).map_err(|err| OSErrorBuilder::with_filename(&err, path, vm))
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#[pyfunction]
#[pyfunction(name = "unlink")]
fn remove(path: OsPath, dir_fd: DirFd<'_, 0>, vm: &VirtualMachine) -> PyResult<()> {
let [] = dir_fd.0;
fs::remove_file(&path).map_err(|err| err.into_pyexception(vm))
}
use crate::exceptions::OSErrorBuilder;
#[pyfunction]
#[pyfunction(name = "unlink")]
fn remove(path: OsPath, dir_fd: DirFd<'_, 0>, vm: &VirtualMachine) -> PyResult<()> {
let [] = dir_fd.0;
fs::remove_file(&path).map_err(|err| OSErrorBuilder::with_filename(&err, path, vm))
}
🤖 Prompt for AI Agents
In crates/vm/src/stdlib/posix_compat.rs around lines 28 to 33, the remove()
function maps fs::remove_file errors using err.into_pyexception(vm) which omits
the filename; replace that mapping with OSErrorBuilder::with_filename(&err,
path, vm).map_err(...) (i.e., construct the OSError via
OSErrorBuilder::with_filename using the original io::Error and the path, then
return that as the PyErr) so error messages match posix.rs and include the
filename for consistency.

@youknowone youknowone merged commit a37f4ec into RustPython:main Dec 28, 2025
8 of 13 checks passed
@youknowone youknowone deleted the shutil branch December 28, 2025 06:49
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