|
1 |
| -use super::os::errno_err; |
2 |
| -use crate::obj::objbytes::PyBytesRef; |
3 |
| -use crate::obj::objstr::PyStringRef; |
4 |
| -use crate::pyobject::{PyObjectRef, PyResult}; |
5 |
| -use crate::VirtualMachine; |
6 |
| - |
7 |
| -use itertools::Itertools; |
8 |
| - |
9 |
| -extern "C" { |
10 |
| - fn _getch() -> i32; |
11 |
| - fn _getwch() -> u32; |
12 |
| - fn _getche() -> i32; |
13 |
| - fn _getwche() -> u32; |
14 |
| - fn _putch(c: u32) -> i32; |
15 |
| - fn _putwch(c: u16) -> u32; |
16 |
| -} |
17 |
| - |
18 |
| -fn msvcrt_getch() -> Vec<u8> { |
19 |
| - let c = unsafe { _getch() }; |
20 |
| - vec![c as u8] |
21 |
| -} |
22 |
| -fn msvcrt_getwch() -> String { |
23 |
| - let c = unsafe { _getwch() }; |
24 |
| - std::char::from_u32(c).unwrap().to_string() |
25 |
| -} |
26 |
| -fn msvcrt_getche() -> Vec<u8> { |
27 |
| - let c = unsafe { _getche() }; |
28 |
| - vec![c as u8] |
29 |
| -} |
30 |
| -fn msvcrt_getwche() -> String { |
31 |
| - let c = unsafe { _getwche() }; |
32 |
| - std::char::from_u32(c).unwrap().to_string() |
33 |
| -} |
34 |
| -fn msvcrt_putch(b: PyBytesRef, vm: &VirtualMachine) -> PyResult<()> { |
35 |
| - let &c = b.get_value().iter().exactly_one().map_err(|_| { |
36 |
| - vm.new_type_error("putch() argument must be a byte string of length 1".to_owned()) |
37 |
| - })?; |
38 |
| - unsafe { suppress_iph!(_putch(c.into())) }; |
39 |
| - Ok(()) |
40 |
| -} |
41 |
| -fn msvcrt_putwch(s: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { |
42 |
| - let c = s.as_str().chars().exactly_one().map_err(|_| { |
43 |
| - vm.new_type_error("putch() argument must be a string of length 1".to_owned()) |
44 |
| - })?; |
45 |
| - unsafe { suppress_iph!(_putwch(c as u16)) }; |
46 |
| - Ok(()) |
47 |
| -} |
48 |
| - |
49 |
| -extern "C" { |
50 |
| - fn _setmode(fd: i32, flags: i32) -> i32; |
51 |
| -} |
52 |
| - |
53 |
| -fn msvcrt_setmode(fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
54 |
| - let flags = unsafe { suppress_iph!(_setmode(fd, flags)) }; |
55 |
| - if flags == -1 { |
56 |
| - Err(errno_err(vm)) |
57 |
| - } else { |
58 |
| - Ok(flags) |
59 |
| - } |
60 |
| -} |
61 |
| - |
62 |
| -pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { |
63 |
| - let ctx = &vm.ctx; |
64 |
| - py_module!(vm, "_msvcrt", { |
65 |
| - "getch" => ctx.new_function(msvcrt_getch), |
66 |
| - "getwch" => ctx.new_function(msvcrt_getwch), |
67 |
| - "getche" => ctx.new_function(msvcrt_getche), |
68 |
| - "getwche" => ctx.new_function(msvcrt_getwche), |
69 |
| - "putch" => ctx.new_function(msvcrt_putch), |
70 |
| - "putwch" => ctx.new_function(msvcrt_putwch), |
71 |
| - "setmode" => ctx.new_function(msvcrt_setmode), |
72 |
| - }) |
73 |
| -} |
| 1 | +use super::os::convert_io_error; |
| 2 | +use crate::obj::objbytes::PyBytesRef; |
| 3 | +use crate::obj::objstr::PyStringRef; |
| 4 | +use crate::pyobject::{PyObjectRef, PyResult}; |
| 5 | +use crate::VirtualMachine; |
| 6 | + |
| 7 | +use itertools::Itertools; |
| 8 | +use std::io; |
| 9 | + |
| 10 | +extern "C" { |
| 11 | + fn _get_errno(pValue: *mut i32) -> i32; |
| 12 | +} |
| 13 | + |
| 14 | +fn get_errno() -> i32 { |
| 15 | + let mut v = 0; |
| 16 | + unsafe { _get_errno(&mut v) }; |
| 17 | + v |
| 18 | +} |
| 19 | + |
| 20 | +extern "C" { |
| 21 | + fn _getch() -> i32; |
| 22 | + fn _getwch() -> u32; |
| 23 | + fn _getche() -> i32; |
| 24 | + fn _getwche() -> u32; |
| 25 | + fn _putch(c: u32) -> i32; |
| 26 | + fn _putwch(c: u16) -> u32; |
| 27 | +} |
| 28 | + |
| 29 | +fn msvcrt_getch() -> Vec<u8> { |
| 30 | + let c = unsafe { _getch() }; |
| 31 | + vec![c as u8] |
| 32 | +} |
| 33 | +fn msvcrt_getwch() -> String { |
| 34 | + let c = unsafe { _getwch() }; |
| 35 | + std::char::from_u32(c).unwrap().to_string() |
| 36 | +} |
| 37 | +fn msvcrt_getche() -> Vec<u8> { |
| 38 | + let c = unsafe { _getche() }; |
| 39 | + vec![c as u8] |
| 40 | +} |
| 41 | +fn msvcrt_getwche() -> String { |
| 42 | + let c = unsafe { _getwche() }; |
| 43 | + std::char::from_u32(c).unwrap().to_string() |
| 44 | +} |
| 45 | +fn msvcrt_putch(b: PyBytesRef, vm: &VirtualMachine) -> PyResult<()> { |
| 46 | + let &c = b.get_value().iter().exactly_one().map_err(|_| { |
| 47 | + vm.new_type_error("putch() argument must be a byte string of length 1".to_owned()) |
| 48 | + })?; |
| 49 | + unsafe { suppress_iph!(_putch(c.into())) }; |
| 50 | + Ok(()) |
| 51 | +} |
| 52 | +fn msvcrt_putwch(s: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { |
| 53 | + let c = s.as_str().chars().exactly_one().map_err(|_| { |
| 54 | + vm.new_type_error("putch() argument must be a string of length 1".to_owned()) |
| 55 | + })?; |
| 56 | + unsafe { suppress_iph!(_putwch(c as u16)) }; |
| 57 | + Ok(()) |
| 58 | +} |
| 59 | + |
| 60 | +extern "C" { |
| 61 | + fn _setmode(fd: i32, flags: i32) -> i32; |
| 62 | +} |
| 63 | + |
| 64 | +fn msvcrt_setmode(fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
| 65 | + let flags = unsafe { suppress_iph!(_setmode(fd, flags)) }; |
| 66 | + if flags == -1 { |
| 67 | + Err(convert_io_error( |
| 68 | + vm, |
| 69 | + io::Error::from_raw_os_error(get_errno()), |
| 70 | + )) |
| 71 | + } else { |
| 72 | + Ok(flags) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { |
| 77 | + let ctx = &vm.ctx; |
| 78 | + py_module!(vm, "_msvcrt", { |
| 79 | + "getch" => ctx.new_function(msvcrt_getch), |
| 80 | + "getwch" => ctx.new_function(msvcrt_getwch), |
| 81 | + "getche" => ctx.new_function(msvcrt_getche), |
| 82 | + "getwche" => ctx.new_function(msvcrt_getwche), |
| 83 | + "putch" => ctx.new_function(msvcrt_putch), |
| 84 | + "putwch" => ctx.new_function(msvcrt_putwch), |
| 85 | + "setmode" => ctx.new_function(msvcrt_setmode), |
| 86 | + }) |
| 87 | +} |
0 commit comments