-
Notifications
You must be signed in to change notification settings - Fork 0
/
winiofiles.py
67 lines (50 loc) · 1.69 KB
/
winiofiles.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import annotations
import asyncio
import io
import sys
from asyncio.proactor_events import BaseProactorEventLoop
from typing import Tuple
if sys.platform != 'win32':
raise RuntimeError("暂时不支持在非Windows平台工作")
from _winapi import CreateFile, GENERIC_READ, FILE_GENERIC_READ, FILE_GENERIC_WRITE, GENERIC_WRITE, \
FILE_FLAG_OVERLAPPED, NULL, CloseHandle, INVALID_HANDLE_VALUE, ReadFile, Overlapped, GetLastError, \
ERROR_IO_PENDING, WaitForSingleObject, INFINITE, WriteFile
OPEN_ALWAYS = 4
mode_access_map = {
"r": GENERIC_READ,
"w": GENERIC_WRITE
}
def open(
file,
mode="r",
buffering=-1,
encoding=None,
errors=None,
newline=None,
closefd=True,
opener=None,
*,
loop=None,
executor=None
):
ov = Overlapped()
file_handler = CreateFile(file, GENERIC_READ, 0, NULL, OPEN_ALWAYS,
FILE_FLAG_OVERLAPPED, NULL)
if file_handler is INVALID_HANDLE_VALUE:
return -1
return File(file_handler)
class File(io.BufferedIOBase):
def __init__(self, handle):
self.handle = handle
def fileno(self) -> int:
return self.handle
async def read(self, __size: int | None = ...) -> bytes:
r: Tuple[Overlapped, int] = ReadFile(self.handle, 2 ** 10, True)
loop = asyncio.get_event_loop()
if isinstance(loop, BaseProactorEventLoop):
res = await loop.sock_recv(self, 1024)
return res
async def write(self, __buffer) -> int:
loop = asyncio.get_event_loop()
if isinstance(loop, BaseProactorEventLoop):
res = await loop.sock_sendall(self, __buffer)