_WindowsConsoleIO #7151
Conversation
📝 WalkthroughWalkthroughAdds Windows console I/O: introduces DEFAULT_BUFFER_SIZE, a new winconsoleio/WindowsConsoleIO surface with console detection and UTF-8 console defaults, a PyO3 Changes
Sequence Diagram(s)sequenceDiagram
actor Python
participant io_open as io.open()
participant console_check as ConsoleDetection
participant winconsoleio as WindowsConsoleIO
participant fileio as FileIO
participant py_obj as PythonFileObject
Python->>io_open: open(path, mode)
io_open->>console_check: is_console(fileno)?
alt Console detected
console_check-->>io_open: true
io_open->>winconsoleio: create WindowsConsoleIO
winconsoleio->>winconsoleio: set UTF-8 default, use DEFAULT_BUFFER_SIZE
winconsoleio-->>py_obj: return console-backed file object
else Regular file
console_check-->>io_open: false
io_open->>fileio: create FileIO (uses DEFAULT_BUFFER_SIZE)
fileio-->>py_obj: return file object
end
sequenceDiagram
actor Python
participant write_input as _testconsole::write_input
participant fileno as file.fileno()
participant handle as Win32HANDLE
participant conv as UTF16Conversion
participant winapi as WriteConsoleInputW
participant error as OS_Error
Python->>write_input: write_input(file, bytes)
write_input->>fileno: call fileno()
fileno-->>handle: get HANDLE
write_input->>handle: validate HANDLE != INVALID_HANDLE_VALUE
alt Valid handle
write_input->>conv: bytes -> UTF-16LE Vec<u16>
conv->>conv: build INPUT_RECORD KEY_EVENT array
conv-->>winapi: call WriteConsoleInputW (loop until all written)
winapi-->>Python: success
else Invalid handle
handle-->>error: INVALID_HANDLE_VALUE
error-->>Python: raise exception
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
Sorry, something went wrong.
8b37b4a to
5e4c233
Compare
February 15, 2026 07:34
There was a problem hiding this comment.
Actionable comments posted: 4
🤖 Fix all issues with AI agents
In `@crates/vm/src/stdlib/io.rs`:
- Line 32: DEFAULT_BUFFER_SIZE was changed to 128 KiB and is now exported as
io.DEFAULT_BUFFER_SIZE and used for _blksize; confirm whether this divergence
from CPython's 8 KiB is intentional. Either revert DEFAULT_BUFFER_SIZE to 8 *
1024 (update the constant DEFAULT_BUFFER_SIZE) to preserve CPython
compatibility, or keep 128 * 1024 but add a clear comment and public
documentation where io.DEFAULT_BUFFER_SIZE and _blksize are defined/exported
(referencing the DEFAULT_BUFFER_SIZE constant and the _blksize export)
explaining the rationale for the larger buffer size.
- Around line 5991-6029: The mode-parsing loop currently accepts '+'/'a'/'b'/'x'
but silently ignores them; change the match in the parsing loop (the code
handling mode_str, rwa, readable, writable, console_type) so that encountering
unsupported flags ('+', 'a', 'x' — and any other non-rc/w characters) returns an
explicit vm.new_value_error (include the offending flag and mode_str in the
message) instead of falling through; leave handling for 'r' and 'w' as-is and
ensure the final rwa check remains to error when neither read nor write is
selected.
- Around line 6593-6611: The WideCharToMultiByte calls in read() (and the
similar call in readall()) currently ignore a 0 return and silently drop data;
update both functions to check if WideCharToMultiByte returns 0 and, on failure,
retrieve the Windows error code (GetLastError) and return an appropriate VM I/O
error instead of continuing — use the same error-building mechanism used
elsewhere in this module (e.g., the VM context I/O error helper) so callers get
a clear failure instead of truncated data. Ensure you reference the
WideCharToMultiByte result variable (u8n) and bail out with a descriptive error
containing the GetLastError value when u8n == 0 in both read() and readall().
- Around line 6646-6673: The initial MultiByteToWideChar call in write()
currently treats wlen == 0 as Ok(0); change this to propagate the Windows error
the same way the second call does: when the first MultiByteToWideChar returns 0,
return Err(std::io::Error::last_os_error().into_pyexception(vm)) so callers
receive the OS error instead of a successful 0 length; update the branch
handling the first wlen check around the MultiByteToWideChar invocation
accordingly (refer to MultiByteToWideChar and the surrounding write() logic).
Sorry, something went wrong.
|
reviews need to be checked |
Sorry, something went wrong.
Add _WindowsConsoleIO class to _io module with ReadConsoleW/WriteConsoleW support. Add _testconsole stdlib module with write_input for console input simulation. Integrate console detection in io_open to use _WindowsConsoleIO for console handles.
5e4c233 to
fa3f421
Compare
February 15, 2026 15:29
fa3f421 to
8552e42
Compare
February 15, 2026 15:32
📦 Library DependenciesThe following Lib/ modules were modified. Here are their dependencies: [x] lib: cpython/Lib/io.py dependencies:
dependent tests: (89 tests)
[x] test: cpython/Lib/test/test_winconsoleio.py dependencies: dependent tests: (no tests depend on winconsoleio) Legend:
|
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@crates/vm/src/stdlib/io.rs`:
- Around line 6111-6114: The code currently forces fd-backed consoles to never
close the fd by unconditionally calling zelf.closefd.store(false); instead honor
the closefd parameter passed to the console initializer (or constructor) so
io.open(closefd=...) semantics are respected: locate the initializer that sets
zelf.closefd (the branch that handles "opened by fd") and change it to store the
provided closefd value (or leave the existing value) rather than overriding it;
ensure any callers that construct from an fd accept and forward a closefd
boolean so explicit user requests to close the fd are honored.
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@crates/vm/src/stdlib/io.rs`:
- Around line 6298-6307: In copy_from_buf the implementation uses a 0 byte as a
sentinel (buf[0] != 0) which incorrectly drops valid NUL bytes and can stall
data; change the buffering to track an explicit length or index instead of
sentinel bytes (e.g., maintain a separate usize field like "len" or use a small
VecDeque) and update the logic in copy_from_buf and any producers/consumers that
mutate the SMALLBUF so they push/pop based on length rather than shifting until
a 0; locate and modify the copy_from_buf function and any associated SMALLBUF
management code to read from buf[0..len], shift or pop properly, and decrement
len (or use VecDeque::pop_front) so NUL bytes are preserved and buffer state is
explicit.
Sorry, something went wrong.
243beb6
into
RustPython:main
Feb 15, 2026
_WindowsConsoleIO
Summary by CodeRabbit
New Features
Chores