Skip to content

host_env: renameat support for os.rename#8118

Open
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-renameat
Open

host_env: renameat support for os.rename#8118
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-renameat

Conversation

@joshuamegnauth54

@joshuamegnauth54 joshuamegnauth54 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Python's os.rename supports renaming a path relative to a directory descriptor - essentially, renameat. The syscall and its Rustix wrapper does all of the work, so this patch mainly updates the Python signature to forward to the implementation.

As of this patch, os.rename is still incorrect for Windows because it always replaces the destination file. I added a small note for future contributors (or myself, since I might tackle it) as a reminder of what's broken.

Summary

  • Adds directory descriptor support for os.rename. See os.rename.

Summary by CodeRabbit

  • New Features
    • Enhanced os.rename() and os.replace() to accept optional src_dir_fd and dst_dir_fd, enabling more flexible file operations.
    • Updated feature reporting so rename and replace are marked as supporting directory file descriptor parameters where available.
  • Bug Fixes
    • Improved behavior on non-Unix platforms by returning an error when directory file descriptors are provided but renameat support is unavailable.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 35d2d538-c79d-4a02-9de1-de692d185af0

📥 Commits

Reviewing files that changed from the base of the PR and between 3cc207d and 073fa98.

📒 Files selected for processing (2)
  • crates/host_env/src/os.rs
  • crates/vm/src/stdlib/os.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/vm/src/stdlib/os.rs
  • crates/host_env/src/os.rs

📝 Walkthrough

Walkthrough

os.rename gains optional src_dir_fd and dst_dir_fd parameters. The low-level host_env::os::rename is split into platform-gated variants: Unix uses rustix::fs::renameat with resolved directory FDs, non-Unix rejects any FD argument. The Python stdlib wrapper is updated with a RenameArgs struct, and rename/replace are marked as supporting dir_fd in os.supports_dir_fd.

Changes

os.rename dir_fd support

Layer / File(s) Summary
host_env rename with optional dir FDs
crates/host_env/src/os.rs
Adds rustix::fd::AsFd import (Unix-only) and replaces the unconditional std::fs::rename with two #[cfg]-gated implementations: non-Unix returns an error when either FD is provided; Unix maps None FDs to rustix::fs::CWD and calls rustix::fs::renameat. Public rename signature gains from_fd: Option<crt_fd::Borrowed<'_>> and to_fd: Option<crt_fd::Borrowed<'_>> parameters.
Python stdlib wrapper and supports_dir_fd
crates/vm/src/stdlib/os.rs
Introduces RenameArgs (FromArgs struct) with positional src/dst and optional src_dir_fd/dst_dir_fd; adds RENAME_DIR_FD platform constant (cfg(not(windows))); rewrites the rename pyfunction to use PathConverter and forward the optional FDs to host_env::os::rename. Updates support_funcs() to set dir_fd: Some(RENAME_DIR_FD) for both rename and replace.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • youknowone

Poem

🐇 Hop, hop, a file's on the move,
With dir_fd now part of the groove.
On Unix we renameat with flair,
On Windows we handle with care,
No path left to lose — what a smooth little groove! 🐾

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'host_env: renameat support for os.rename' accurately describes the primary change: adding renameat support to the os.rename function.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/host_env/src/os.rs`:
- Around line 271-288: The error path in the rename function at line 286 returns
io::Error::other(...) directly, but the function signature specifies a return
type of io::Result<()>, which is Result<(), io::Error>. To fix this type
mismatch, wrap the io::Error::other(...) call with Err() so the error is
properly wrapped in the Result type, making the return statement consistent with
the function's declared return type.

In `@crates/vm/src/stdlib/os.rs`:
- Around line 1953-1954: The rename and replace functions unconditionally
advertise dir_fd support via Some(true) even though it only works on Unix
platforms. Add a new const RENAME_DIR_FD near the other *_DIR_FD constants that
uses cfg!(unix) to conditionally return true only on Unix systems, then update
the SupportFunc::new calls for both the "rename" and "replace" entries to use
Some(RENAME_DIR_FD) instead of Some(true) to accurately represent
platform-specific capability.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 3982ccbf-2610-4482-8a10-527c4eb2b558

📥 Commits

Reviewing files that changed from the base of the PR and between dbfd69e and 3cc207d.

📒 Files selected for processing (2)
  • crates/host_env/src/os.rs
  • crates/vm/src/stdlib/os.rs

Comment thread crates/host_env/src/os.rs
Comment thread crates/vm/src/stdlib/os.rs Outdated
Python's `os.rename` supports renaming a path relative to a directory
descriptor - essentially, `renameat`. The syscall and its Rustix wrapper
does all of the work, so this patch mainly updates the Python signature
to forward to the implementation.

As of this patch, `os.rename` is still incorrect for Windows because it
always replaces the destination file. I added a small note for future
contributors (or myself, since I might tackle it) as a reminder of
what's broken.
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