Skip to content

Conversation

@doojonio
Copy link
Contributor

@doojonio doojonio commented Oct 11, 2025

Closes #6146

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of OS-level read errors on Unix when reading input, providing clearer error messages and exiting the prompt loop safely.
    • The built-in input function now surfaces OS errors as proper exceptions on Unix instead of ambiguous failures.
    • Enhances stability and diagnostics during terminal I/O failures on Unix systems; existing behavior on other platforms remains unchanged.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 11, 2025

Walkthrough

Introduces a Unix-specific ReadlineResult::OsError variant and integrates handling across readline, the REPL loop, and the builtin input function. On Unix, OS-level readline errors are propagated as OS error exceptions; the REPL prints the error and exits the loop. Existing branches remain unchanged.

Changes

Cohort / File(s) Summary
Readline result extension (Unix)
vm/src/readline.rs
Adds #[cfg(unix)] variant OsError(nix::Error) to ReadlineResult. Maps ReadlineError::Errno(num) to OsError(num) in Unix code paths, updating match branches accordingly.
REPL loop error handling (Unix)
src/shell.rs
Adds a Unix-only match arm for ReadlineResult::OsError(num) in run_shell: constructs an OS error exception with the numeric code, prints it, and breaks the loop.
Builtin input error mapping (Unix)
vm/src/stdlib/builtins.rs
Adds a Unix-only match arm in builtin input to convert ReadlineResult::OsError(num) into vm.new_os_error(num.to_string()).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as User
  participant RL as Readline (Unix)
  participant VM as VM
  participant SH as REPL Shell

  rect rgba(230,240,255,0.6)
    U->>SH: Enter input
    SH->>RL: read_line()
    RL-->>SH: OsError(errno)
    note over SH: New Unix-specific branch
    SH->>VM: new_os_error(errno)
    VM-->>SH: OSError instance
    SH-->>U: Print error
    SH-->>U: Exit REPL loop
  end
Loading
sequenceDiagram
  autonumber
  participant U as User code
  participant RL as Readline (Unix)
  participant VM as VM
  participant BI as builtin input()

  rect rgba(230,255,230,0.6)
    U->>BI: input()
    BI->>RL: read_line()
    RL-->>BI: OsError(errno)
    note over BI: New Unix-specific arm
    BI->>VM: new_os_error(errno.to_string())
    VM-->>BI: OSError
    BI-->>U: Raise OSError
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I nudge the keys with velvet paws,
Catch OS winds and Unix laws;
When FDs fall and lines demur,
I whisk an OSError to confer.
No crash tonight—just gentle hop,
A tidy loop, a graceful stop.
🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “Handle OsError in REPL” succinctly and accurately describes the primary change of adding explicit OS error handling in the REPL without extraneous details or ambiguity.
Linked Issues Check ✅ Passed The changes introduce a new OsError variant and integrate it into the readline loop and builtin input function so that Errno(EBADF) is caught and raised as an OSError rather than crashing, directly satisfying the objective of issue #6146.
Out of Scope Changes Check ✅ Passed All modifications are focused solely on introducing and handling the OsError variant under the Unix configuration, with no unrelated code changes detected outside the scope of preventing the REPL crash.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • 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

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

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)
src/shell.rs (1)

212-218: Use vm.new_os_error() for consistency.

For consistency with the handling in builtins.rs (line 493) and cleaner error messages, consider using vm.new_os_error(num.to_string()) instead of manually constructing the exception.

Apply this diff:

-            #[cfg(unix)]
-            ReadlineResult::OsError(num) => {
-                let os_error =
-                    vm.new_exception_msg(vm.ctx.exceptions.os_error.to_owned(), format!("{num:?}"));
-                vm.print_exception(os_error);
-                break;
-            }
+            #[cfg(unix)]
+            ReadlineResult::OsError(num) => {
+                let os_error = vm.new_os_error(num.to_string());
+                vm.print_exception(os_error);
+                break;
+            }
📜 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 3a6fda4 and 25e0a28.

📒 Files selected for processing (3)
  • src/shell.rs (1 hunks)
  • vm/src/readline.rs (2 hunks)
  • vm/src/stdlib/builtins.rs (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.rs

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

**/*.rs: Format Rust code with the default rustfmt style (run cargo fmt)
Run clippy and fix any warnings or lints introduced by your changes
Follow Rust best practices for error handling and memory management

Files:

  • vm/src/readline.rs
  • src/shell.rs
  • vm/src/stdlib/builtins.rs
{vm,stdlib}/**/*.rs

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

Use RustPython macros (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • vm/src/readline.rs
  • vm/src/stdlib/builtins.rs
🔇 Additional comments (3)
vm/src/readline.rs (2)

16-17: LGTM!

The Unix-specific OsError variant is properly gated and provides a clean way to propagate OS-level readline errors (such as EBADF when stdout is closed).


123-124: LGTM!

The mapping from ReadlineError::Errno to ReadlineResult::OsError correctly propagates Unix-specific errno errors through the readline interface.

vm/src/stdlib/builtins.rs (1)

492-493: LGTM!

The Unix-specific error handling correctly converts readline OS errors into Python OSError exceptions using the idiomatic vm.new_os_error() method.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

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

Thank you for contributing!

@youknowone youknowone merged commit b56e469 into RustPython:main Oct 13, 2025
12 checks passed
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.

os.close(1) crashes REPL

2 participants