forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_testconsole.rs
More file actions
35 lines (30 loc) · 1.15 KB
/
_testconsole.rs
File metadata and controls
35 lines (30 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
pub(crate) use _testconsole::module_def;
#[pymodule]
mod _testconsole {
use crate::vm::{
PyObjectRef, PyResult, VirtualMachine, convert::IntoPyException, function::ArgBytesLike,
};
use rustpython_host_env::testconsole as host_testconsole;
#[pyfunction]
fn write_input(file: PyObjectRef, s: ArgBytesLike, vm: &VirtualMachine) -> PyResult<()> {
// Get the fd from the file object via fileno()
let fd_obj = vm.call_method(&file, "fileno", ())?;
let fd: i32 = fd_obj.try_into_value(vm)?;
let data = s.borrow_buf();
let data = &*data;
// Interpret as UTF-16-LE pairs
if !data.len().is_multiple_of(2) {
return Err(vm.new_value_error("buffer must contain UTF-16-LE data (even length)"));
}
let wchars: Vec<u16> = data
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.collect();
host_testconsole::write_console_input(fd, &wchars).map_err(|e| e.into_pyexception(vm))
}
#[pyfunction]
fn read_output(_file: PyObjectRef) -> Option<()> {
// Stub, same as CPython
None
}
}