|
1 |
| -use super::os::errno_err; |
2 |
| -use crate::{ |
3 |
| - builtins::{PyBytes, PyStrRef}, |
4 |
| - suppress_iph, PyObjectRef, PyRef, PyResult, VirtualMachine, |
5 |
| -}; |
6 |
| -use itertools::Itertools; |
7 |
| -use winapi::{ |
8 |
| - shared::minwindef::UINT, |
9 |
| - um::{errhandlingapi::SetErrorMode, handleapi::INVALID_HANDLE_VALUE, winnt::HANDLE}, |
10 |
| -}; |
| 1 | +pub use msvcrt::*; |
11 | 2 |
|
12 |
| -pub fn setmode_binary(fd: i32) { |
13 |
| - unsafe { suppress_iph!(_setmode(fd, libc::O_BINARY)) }; |
14 |
| -} |
| 3 | +#[pymodule] |
| 4 | +mod msvcrt { |
| 5 | + use super::os::errno_err; |
| 6 | + use crate::{ |
| 7 | + builtins::{PyBytes, PyStrRef}, |
| 8 | + suppress_iph, PyObjectRef, PyRef, PyResult, VirtualMachine, |
| 9 | + }; |
| 10 | + use itertools::Itertools; |
| 11 | + use winapi::{ |
| 12 | + shared::minwindef::UINT, |
| 13 | + um::{handleapi::INVALID_HANDLE_VALUE, winnt::HANDLE}, |
| 14 | + }; |
15 | 15 |
|
16 |
| -pub fn get_errno() -> i32 { |
17 |
| - let mut e = 0; |
18 |
| - unsafe { suppress_iph!(_get_errno(&mut e)) }; |
19 |
| - e |
20 |
| -} |
| 16 | + #[pyattr] |
| 17 | + use winapi::um::winbase::{ |
| 18 | + SEM_FAILCRITICALERRORS, SEM_NOALIGNMENTFAULTEXCEPT, SEM_NOGPFAULTERRORBOX, |
| 19 | + SEM_NOOPENFILEERRORBOX, |
| 20 | + }; |
21 | 21 |
|
22 |
| -extern "C" { |
23 |
| - fn _get_errno(pValue: *mut i32) -> i32; |
24 |
| -} |
| 22 | + pub fn setmode_binary(fd: i32) { |
| 23 | + unsafe { suppress_iph!(_setmode(fd, libc::O_BINARY)) }; |
| 24 | + } |
25 | 25 |
|
26 |
| -extern "C" { |
27 |
| - fn _getch() -> i32; |
28 |
| - fn _getwch() -> u32; |
29 |
| - fn _getche() -> i32; |
30 |
| - fn _getwche() -> u32; |
31 |
| - fn _putch(c: u32) -> i32; |
32 |
| - fn _putwch(c: u16) -> u32; |
33 |
| -} |
| 26 | + pub fn get_errno() -> i32 { |
| 27 | + let mut e = 0; |
| 28 | + unsafe { suppress_iph!(_get_errno(&mut e)) }; |
| 29 | + e |
| 30 | + } |
34 | 31 |
|
35 |
| -fn msvcrt_getch() -> Vec<u8> { |
36 |
| - let c = unsafe { _getch() }; |
37 |
| - vec![c as u8] |
38 |
| -} |
39 |
| -fn msvcrt_getwch() -> String { |
40 |
| - let c = unsafe { _getwch() }; |
41 |
| - std::char::from_u32(c).unwrap().to_string() |
42 |
| -} |
43 |
| -fn msvcrt_getche() -> Vec<u8> { |
44 |
| - let c = unsafe { _getche() }; |
45 |
| - vec![c as u8] |
46 |
| -} |
47 |
| -fn msvcrt_getwche() -> String { |
48 |
| - let c = unsafe { _getwche() }; |
49 |
| - std::char::from_u32(c).unwrap().to_string() |
50 |
| -} |
51 |
| -fn msvcrt_putch(b: PyRef<PyBytes>, vm: &VirtualMachine) -> PyResult<()> { |
52 |
| - let &c = b.as_bytes().iter().exactly_one().map_err(|_| { |
53 |
| - vm.new_type_error("putch() argument must be a byte string of length 1".to_owned()) |
54 |
| - })?; |
55 |
| - unsafe { suppress_iph!(_putch(c.into())) }; |
56 |
| - Ok(()) |
57 |
| -} |
58 |
| -fn msvcrt_putwch(s: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { |
59 |
| - let c = s.as_str().chars().exactly_one().map_err(|_| { |
60 |
| - vm.new_type_error("putch() argument must be a string of length 1".to_owned()) |
61 |
| - })?; |
62 |
| - unsafe { suppress_iph!(_putwch(c as u16)) }; |
63 |
| - Ok(()) |
64 |
| -} |
| 32 | + extern "C" { |
| 33 | + fn _get_errno(pValue: *mut i32) -> i32; |
| 34 | + } |
65 | 35 |
|
66 |
| -extern "C" { |
67 |
| - fn _setmode(fd: i32, flags: i32) -> i32; |
68 |
| -} |
| 36 | + extern "C" { |
| 37 | + fn _getch() -> i32; |
| 38 | + fn _getwch() -> u32; |
| 39 | + fn _getche() -> i32; |
| 40 | + fn _getwche() -> u32; |
| 41 | + fn _putch(c: u32) -> i32; |
| 42 | + fn _putwch(c: u16) -> u32; |
| 43 | + } |
69 | 44 |
|
70 |
| -fn msvcrt_setmode(fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
71 |
| - let flags = unsafe { suppress_iph!(_setmode(fd, flags)) }; |
72 |
| - if flags == -1 { |
73 |
| - Err(errno_err(vm)) |
74 |
| - } else { |
75 |
| - Ok(flags) |
| 45 | + #[pyfunction] |
| 46 | + fn getch() -> Vec<u8> { |
| 47 | + let c = unsafe { _getch() }; |
| 48 | + vec![c as u8] |
| 49 | + } |
| 50 | + #[pyfunction] |
| 51 | + fn getwch() -> String { |
| 52 | + let c = unsafe { _getwch() }; |
| 53 | + std::char::from_u32(c).unwrap().to_string() |
| 54 | + } |
| 55 | + #[pyfunction] |
| 56 | + fn getche() -> Vec<u8> { |
| 57 | + let c = unsafe { _getche() }; |
| 58 | + vec![c as u8] |
| 59 | + } |
| 60 | + #[pyfunction] |
| 61 | + fn getwche() -> String { |
| 62 | + let c = unsafe { _getwche() }; |
| 63 | + std::char::from_u32(c).unwrap().to_string() |
| 64 | + } |
| 65 | + #[pyfunction] |
| 66 | + fn putch(b: PyRef<PyBytes>, vm: &VirtualMachine) -> PyResult<()> { |
| 67 | + let &c = b.as_bytes().iter().exactly_one().map_err(|_| { |
| 68 | + vm.new_type_error("putch() argument must be a byte string of length 1".to_owned()) |
| 69 | + })?; |
| 70 | + unsafe { suppress_iph!(_putch(c.into())) }; |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | + #[pyfunction] |
| 74 | + fn putwch(s: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { |
| 75 | + let c = s.as_str().chars().exactly_one().map_err(|_| { |
| 76 | + vm.new_type_error("putch() argument must be a string of length 1".to_owned()) |
| 77 | + })?; |
| 78 | + unsafe { suppress_iph!(_putwch(c as u16)) }; |
| 79 | + Ok(()) |
76 | 80 | }
|
77 |
| -} |
78 | 81 |
|
79 |
| -extern "C" { |
80 |
| - fn _open_osfhandle(osfhandle: isize, flags: i32) -> i32; |
81 |
| - fn _get_osfhandle(fd: i32) -> libc::intptr_t; |
82 |
| -} |
| 82 | + extern "C" { |
| 83 | + fn _setmode(fd: i32, flags: i32) -> i32; |
| 84 | + } |
83 | 85 |
|
84 |
| -fn msvcrt_open_osfhandle(handle: isize, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
85 |
| - let ret = unsafe { suppress_iph!(_open_osfhandle(handle, flags)) }; |
86 |
| - if ret == -1 { |
87 |
| - Err(errno_err(vm)) |
88 |
| - } else { |
89 |
| - Ok(ret) |
| 86 | + #[pyfunction] |
| 87 | + fn setmode(fd: i32, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
| 88 | + let flags = unsafe { suppress_iph!(_setmode(fd, flags)) }; |
| 89 | + if flags == -1 { |
| 90 | + Err(errno_err(vm)) |
| 91 | + } else { |
| 92 | + Ok(flags) |
| 93 | + } |
90 | 94 | }
|
91 |
| -} |
92 | 95 |
|
93 |
| -fn msvcrt_get_osfhandle(fd: i32, vm: &VirtualMachine) -> PyResult<isize> { |
94 |
| - let ret = unsafe { suppress_iph!(_get_osfhandle(fd)) }; |
95 |
| - if ret as HANDLE == INVALID_HANDLE_VALUE { |
96 |
| - Err(errno_err(vm)) |
97 |
| - } else { |
98 |
| - Ok(ret) |
| 96 | + extern "C" { |
| 97 | + fn _open_osfhandle(osfhandle: isize, flags: i32) -> i32; |
| 98 | + fn _get_osfhandle(fd: i32) -> libc::intptr_t; |
99 | 99 | }
|
100 |
| -} |
101 | 100 |
|
102 |
| -fn msvcrt_seterrormode(mode: UINT, _: &VirtualMachine) -> UINT { |
103 |
| - unsafe { suppress_iph!(SetErrorMode(mode)) } |
104 |
| -} |
| 101 | + #[pyfunction] |
| 102 | + fn open_osfhandle(handle: isize, flags: i32, vm: &VirtualMachine) -> PyResult<i32> { |
| 103 | + let ret = unsafe { suppress_iph!(_open_osfhandle(handle, flags)) }; |
| 104 | + if ret == -1 { |
| 105 | + Err(errno_err(vm)) |
| 106 | + } else { |
| 107 | + Ok(ret) |
| 108 | + } |
| 109 | + } |
105 | 110 |
|
106 |
| -pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { |
107 |
| - use winapi::um::winbase::{ |
108 |
| - SEM_FAILCRITICALERRORS, SEM_NOALIGNMENTFAULTEXCEPT, SEM_NOGPFAULTERRORBOX, |
109 |
| - SEM_NOOPENFILEERRORBOX, |
110 |
| - }; |
| 111 | + #[pyfunction] |
| 112 | + fn get_osfhandle(fd: i32, vm: &VirtualMachine) -> PyResult<isize> { |
| 113 | + let ret = unsafe { suppress_iph!(_get_osfhandle(fd)) }; |
| 114 | + if ret as HANDLE == INVALID_HANDLE_VALUE { |
| 115 | + Err(errno_err(vm)) |
| 116 | + } else { |
| 117 | + Ok(ret) |
| 118 | + } |
| 119 | + } |
111 | 120 |
|
112 |
| - let ctx = &vm.ctx; |
113 |
| - py_module!(vm, "msvcrt", { |
114 |
| - "getch" => named_function!(ctx, msvcrt, getch), |
115 |
| - "getwch" => named_function!(ctx, msvcrt, getwch), |
116 |
| - "getche" => named_function!(ctx, msvcrt, getche), |
117 |
| - "getwche" => named_function!(ctx, msvcrt, getwche), |
118 |
| - "putch" => named_function!(ctx, msvcrt, putch), |
119 |
| - "putwch" => named_function!(ctx, msvcrt, putwch), |
120 |
| - "setmode" => named_function!(ctx, msvcrt, setmode), |
121 |
| - "open_osfhandle" => named_function!(ctx, msvcrt, open_osfhandle), |
122 |
| - "get_osfhandle" => named_function!(ctx, msvcrt, get_osfhandle), |
123 |
| - "SetErrorMode" => named_function!(ctx, msvcrt, seterrormode), |
124 |
| - "SEM_FAILCRITICALERRORS" => ctx.new_int(SEM_FAILCRITICALERRORS), |
125 |
| - "SEM_NOALIGNMENTFAULTEXCEPT" => ctx.new_int(SEM_NOALIGNMENTFAULTEXCEPT), |
126 |
| - "SEM_NOGPFAULTERRORBOX" => ctx.new_int(SEM_NOGPFAULTERRORBOX), |
127 |
| - "SEM_NOOPENFILEERRORBOX" => ctx.new_int(SEM_NOOPENFILEERRORBOX), |
128 |
| - }) |
| 121 | + #[allow(non_snake_case)] |
| 122 | + #[pyfunction] |
| 123 | + fn SetErrorMode(mode: UINT, _: &VirtualMachine) -> UINT { |
| 124 | + unsafe { suppress_iph!(winapi::um::errhandlingapi::SetErrorMode(mode)) } |
| 125 | + } |
129 | 126 | }
|
0 commit comments