Skip to content

Commit 3530295

Browse files
committed
terminalsize
1 parent cc534d2 commit 3530295

File tree

1 file changed

+26
-24
lines changed
  • crates/vm/src/stdlib

1 file changed

+26
-24
lines changed

crates/vm/src/stdlib/nt.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,34 @@ pub(crate) mod module {
203203
fd: OptionalArg<i32>,
204204
vm: &VirtualMachine,
205205
) -> PyResult<_os::TerminalSizeData> {
206-
let (columns, lines) = {
207-
let stdhandle = match fd {
208-
OptionalArg::Present(0) => Console::STD_INPUT_HANDLE,
209-
OptionalArg::Present(1) | OptionalArg::Missing => Console::STD_OUTPUT_HANDLE,
210-
OptionalArg::Present(2) => Console::STD_ERROR_HANDLE,
211-
_ => return Err(vm.new_value_error("bad file descriptor")),
212-
};
213-
let h = unsafe { Console::GetStdHandle(stdhandle) };
214-
if h.is_null() {
215-
return Err(vm.new_os_error("handle cannot be retrieved".to_owned()));
216-
}
217-
if h == INVALID_HANDLE_VALUE {
218-
return Err(errno_err(vm));
206+
let fd = fd.unwrap_or(1); // default to stdout
207+
208+
// For standard streams, use GetStdHandle which returns proper console handles
209+
// For other fds, use _get_osfhandle to convert
210+
let h = match fd {
211+
0 => unsafe { Console::GetStdHandle(Console::STD_INPUT_HANDLE) },
212+
1 => unsafe { Console::GetStdHandle(Console::STD_OUTPUT_HANDLE) },
213+
2 => unsafe { Console::GetStdHandle(Console::STD_ERROR_HANDLE) },
214+
_ => {
215+
let borrowed = unsafe { crt_fd::Borrowed::borrow_raw(fd) };
216+
let handle = crt_fd::as_handle(borrowed).map_err(|e| e.to_pyexception(vm))?;
217+
handle.as_raw_handle() as _
219218
}
220-
let mut csbi = MaybeUninit::uninit();
221-
let ret = unsafe { Console::GetConsoleScreenBufferInfo(h, csbi.as_mut_ptr()) };
222-
let csbi = unsafe { csbi.assume_init() };
223-
if ret == 0 {
224-
return Err(errno_err(vm));
225-
}
226-
let w = csbi.srWindow;
227-
(
228-
(w.Right - w.Left + 1) as usize,
229-
(w.Bottom - w.Top + 1) as usize,
230-
)
231219
};
220+
221+
if h.is_null() || h == INVALID_HANDLE_VALUE {
222+
return Err(errno_err(vm));
223+
}
224+
225+
let mut csbi = MaybeUninit::uninit();
226+
let ret = unsafe { Console::GetConsoleScreenBufferInfo(h, csbi.as_mut_ptr()) };
227+
if ret == 0 {
228+
return Err(errno_err(vm));
229+
}
230+
let csbi = unsafe { csbi.assume_init() };
231+
let w = csbi.srWindow;
232+
let columns = (w.Right - w.Left + 1) as usize;
233+
let lines = (w.Bottom - w.Top + 1) as usize;
232234
Ok(_os::TerminalSizeData { columns, lines })
233235
}
234236

0 commit comments

Comments
 (0)