Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 12, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved IO error messages across platforms: Windows now surfaces more descriptive POSIX errno text when available (with a fallback to previous messages); Unix uses strerror-based messages when possible; other targets retain previous message behavior as a fallback.
    • No public API or behavioral changes beyond message improvements.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 12, 2025

Walkthrough

Adds platform-conditional message construction in ToPyException for std::io::Error: Windows attempts to use the C runtime strerror for POSIX errno (1–127) with fallbacks to self.to_string() or "Unknown error"; Unix uses strerror(errno) when available; other targets fall back to the original error string.

Changes

Cohort / File(s) Summary
IO errno message handling
crates/vm/src/stdlib/io.rs
Adds platform-conditional logic in ToPyException for std::io::Error. On Windows, attempts to build messages via the C runtime strerror for POSIX errno (1–127) and falls back to self.to_string() or "Unknown error"; on Unix uses strerror(errno) when available; other targets use the original string.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Inspect platform-specific FFI calls to the C runtime (strerror) and safety around pointers and encoding.
  • Verify correct errno range checks (1–127) and the "Unknown error" fallback logic on Windows.
  • Check conditional compilation for Unix, Windows, and other targets and run cross-platform tests where possible.

Possibly related PRs

  • WindowsError #6333 — Also modifies Windows-specific error handling and message construction for OSError/IO errors; related at the code-level for errno/winerror and strerror usage.

Poem

🐰 I nibble at C strings late at night,
Hopping through errno by dim moonlight,
On Windows I peek where strerror hides,
On Unix I listen where the old echo bides,
Carrots, crumbs, and clearer error sights 🥕

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 'use posix error messages under 127' accurately describes the main change: implementing POSIX error message handling for errno values under 127 in the ToPyException implementation for std::io::Error.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 b093b19 and 786a65e.

⛔ Files ignored due to path filters (3)
  • Lib/test/test_argparse.py is excluded by !Lib/**
  • Lib/test/test_py_compile.py is excluded by !Lib/**
  • Lib/test/test_subprocess.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/io.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/vm/src/stdlib/io.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 12, 2025 12:58
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)

23-44: Confirm libc::strerror semantics on Windows targets; consider centralizing helper

  • libc::strerror’s thread-safety and returned “Unknown error” text can differ by CRT/toolchain/locale; please confirm this behaves as intended on the Windows targets RustPython supports (e.g., MSVC/UCRT vs MinGW) and that the "Unknown error" guard is robust enough.
  • There’s already a strerror(e: i32) -> String helper in crates/vm/src/stdlib/os.rs:1542-1546 (per provided snippet); consider reusing/relocating a shared helper to avoid duplicating FFI + string handling logic.
📜 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 c11a72a and d44baf9.

⛔ Files ignored due to path filters (2)
  • Lib/test/test_argparse.py is excluded by !Lib/**
  • Lib/test/test_py_compile.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/io.rs (1 hunks)
🧰 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-12-09T08:46:58.660Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6358
File: crates/vm/src/exception_group.rs:173-185
Timestamp: 2025-12-09T08:46:58.660Z
Learning: In crates/vm/src/exception_group.rs, the derive() method intentionally always creates a BaseExceptionGroup instance rather than preserving the original exception class type. This is a deliberate design decision that differs from CPython's behavior.

Applied to files:

  • crates/vm/src/stdlib/io.rs
🧬 Code graph analysis (1)
crates/vm/src/stdlib/io.rs (2)
crates/vm/src/exceptions.rs (2)
  • errno (1450-1451)
  • errno (1452-1452)
crates/vm/src/stdlib/os.rs (2)
  • strerror (1543-1547)
  • std (1207-1207)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (windows-latest)
  • GitHub Check: Run rust tests (windows-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Run rust tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
  • GitHub Check: Ensure compilation on various targets
  • GitHub Check: Check the WASM package and demo

@youknowone youknowone force-pushed the winerror-msg branch 2 times, most recently from 7c59654 to b093b19 Compare December 12, 2025 14:30
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 (2)
crates/vm/src/stdlib/io.rs (2)

23-41: Consider extracting strerror logic to a shared helper.

The Windows block correctly handles POSIX errno translation with appropriate bounds checking and fallback logic. However, libc::strerror is not thread-safe—it returns a pointer to a static buffer that can be overwritten by concurrent calls. This matches the existing pattern in os.rs (lines 1542-1546), so it's consistent with the codebase.

Consider consolidating the strerror logic with the existing strerror helper in crates/vm/src/stdlib/os.rs to reduce duplication and centralize any future thread-safety improvements.


42-52: Unix strerror logic duplicates existing helper in os.rs.

This block is nearly identical to the strerror function at crates/vm/src/stdlib/os.rs:1542-1546. Consider reusing that function to avoid duplication:

 #[cfg(unix)]
 let msg = {
-    let ptr = unsafe { libc::strerror(errno) };
-    if !ptr.is_null() {
-        unsafe { std::ffi::CStr::from_ptr(ptr) }
-            .to_string_lossy()
-            .into_owned()
-    } else {
-        self.to_string()
-    }
+    // Reuse existing strerror helper from os module
+    crate::stdlib::os::strerror(errno)
 };

Note: This requires making the strerror function in os.rs public or moving it to a shared location.

📜 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 7c59654 and b093b19.

⛔ Files ignored due to path filters (2)
  • Lib/test/test_argparse.py is excluded by !Lib/**
  • Lib/test/test_py_compile.py is excluded by !Lib/**
📒 Files selected for processing (1)
  • crates/vm/src/stdlib/io.rs (1 hunks)
🧰 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-12-09T08:46:58.660Z
Learnt from: youknowone
Repo: RustPython/RustPython PR: 6358
File: crates/vm/src/exception_group.rs:173-185
Timestamp: 2025-12-09T08:46:58.660Z
Learning: In crates/vm/src/exception_group.rs, the derive() method intentionally always creates a BaseExceptionGroup instance rather than preserving the original exception class type. This is a deliberate design decision that differs from CPython's behavior.

Applied to files:

  • crates/vm/src/stdlib/io.rs
🧬 Code graph analysis (1)
crates/vm/src/stdlib/io.rs (1)
crates/vm/src/stdlib/os.rs (2)
  • strerror (1543-1547)
  • std (1207-1207)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: Run snippets and cpython tests (ubuntu-latest)
  • GitHub Check: Run snippets and cpython tests (macos-latest)
  • GitHub Check: Run rust tests (macos-latest)
  • GitHub Check: Check the WASM package and demo
  • GitHub Check: Run tests under miri
  • GitHub Check: Run snippets and cpython tests on wasm-wasi
🔇 Additional comments (1)
crates/vm/src/stdlib/io.rs (1)

20-55: Implementation correctly handles platform-specific error messages.

The overall approach is sound: using C runtime strerror for better POSIX compatibility on Windows, with appropriate null checks and fallbacks. The labeled block pattern for early return is idiomatic Rust.

@youknowone youknowone merged commit 30dd5be into RustPython:main Dec 13, 2025
13 checks passed
@youknowone youknowone deleted the winerror-msg branch December 13, 2025 00:24
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