◐ Shell
clean mode source ↗

nt.terminalsize by youknowone · Pull Request #6374 · RustPython/RustPython

Walkthrough

Default to stdout (fd=1), derive a Windows HANDLE via crt_fd::Borrowed/as_handle(), call GetConsoleScreenBufferInfo; on ERROR_ACCESS_DENIED open CONOUT$, retry GetConsoleScreenBufferInfo, close temp handle, compute columns/lines from csbi.srWindow, propagate other errors via errno_err(vm).

Changes

Cohort / File(s) Summary
Windows terminal size and fd handling
crates/vm/src/stdlib/nt.rs
Use crt_fd::Borrowed / as_handle() for fd (default 1), call GetConsoleScreenBufferInfo on derived HANDLE; on ERROR_ACCESS_DENIED open CONOUT$ with `GENERIC_READ

Sequence Diagram

sequenceDiagram
    participant VM as VM code
    participant crt_fd as crt_fd resolver
    participant WinAPI as Windows API
    participant CONOUT as CONOUT$ file

    VM->>crt_fd: Borrow fd (fd.unwrap_or(1))
    crt_fd-->>VM: as_handle() -> HANDLE
    VM->>WinAPI: GetConsoleScreenBufferInfo(HANDLE)
    alt Success
        WinAPI-->>VM: CONSOLE_SCREEN_BUFFER_INFO
        VM-->>VM: compute columns/lines from srWindow
    else ERROR_ACCESS_DENIED
        WinAPI-->>VM: ERROR_ACCESS_DENIED
        VM->>CONOUT: CreateFileW("CONOUT$", GENERIC_READ|GENERIC_WRITE)
        CONOUT-->>VM: temp HANDLE
        VM->>WinAPI: GetConsoleScreenBufferInfo(temp HANDLE)
        WinAPI-->>VM: CONSOLE_SCREEN_BUFFER_INFO
        VM->>CONOUT: CloseHandle(temp HANDLE)
        VM-->>VM: compute columns/lines from srWindow
    else OtherError
        WinAPI-->>VM: error
        VM-->>VM: errno_err(vm) (propagate)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • Verify correct crt_fd::Borrowed / as_handle() usage and fd defaulting.
  • Check handling of INVALID_HANDLE_VALUE and mapping to errno_err(vm).
  • Confirm CreateFileW flags for CONOUT$, retry logic, and CloseHandle to avoid leaks.
  • Validate columns/lines computation from csbi.srWindow and error propagation paths.

Possibly related PRs

Poem

🐰 I hopped from fd to handle bright,

I peeked the console in the night.
If access barred, I sought CONOUT$,
retried, counted rows and columns stout.
Closed the gate and twitched my nose.

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'nt.terminalsize' is vague and does not clearly convey the specific change being made. While it references the module and function, it lacks context about what was actually modified or improved. Consider using a more descriptive title that explains the main change, such as 'Refactor nt.terminalsize to handle console access errors more robustly' or 'Improve nt.terminalsize with fallback CONOUT$ handle approach'.
✅ 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%.
✨ 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.