Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../asyncio
File: windows_utils.py
"""Various Windows specific bits and pieces."""
[0] Fix | Delete
[1] Fix | Delete
import sys
[2] Fix | Delete
[3] Fix | Delete
if sys.platform != 'win32': # pragma: no cover
[4] Fix | Delete
raise ImportError('win32 only')
[5] Fix | Delete
[6] Fix | Delete
import _winapi
[7] Fix | Delete
import itertools
[8] Fix | Delete
import msvcrt
[9] Fix | Delete
import os
[10] Fix | Delete
import subprocess
[11] Fix | Delete
import tempfile
[12] Fix | Delete
import warnings
[13] Fix | Delete
[14] Fix | Delete
[15] Fix | Delete
__all__ = 'pipe', 'Popen', 'PIPE', 'PipeHandle'
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
# Constants/globals
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
BUFSIZE = 8192
[22] Fix | Delete
PIPE = subprocess.PIPE
[23] Fix | Delete
STDOUT = subprocess.STDOUT
[24] Fix | Delete
_mmap_counter = itertools.count()
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
# Replacement for os.pipe() using handles instead of fds
[28] Fix | Delete
[29] Fix | Delete
[30] Fix | Delete
def pipe(*, duplex=False, overlapped=(True, True), bufsize=BUFSIZE):
[31] Fix | Delete
"""Like os.pipe() but with overlapped support and using handles not fds."""
[32] Fix | Delete
address = tempfile.mktemp(
[33] Fix | Delete
prefix=r'\\.\pipe\python-pipe-{:d}-{:d}-'.format(
[34] Fix | Delete
os.getpid(), next(_mmap_counter)))
[35] Fix | Delete
[36] Fix | Delete
if duplex:
[37] Fix | Delete
openmode = _winapi.PIPE_ACCESS_DUPLEX
[38] Fix | Delete
access = _winapi.GENERIC_READ | _winapi.GENERIC_WRITE
[39] Fix | Delete
obsize, ibsize = bufsize, bufsize
[40] Fix | Delete
else:
[41] Fix | Delete
openmode = _winapi.PIPE_ACCESS_INBOUND
[42] Fix | Delete
access = _winapi.GENERIC_WRITE
[43] Fix | Delete
obsize, ibsize = 0, bufsize
[44] Fix | Delete
[45] Fix | Delete
openmode |= _winapi.FILE_FLAG_FIRST_PIPE_INSTANCE
[46] Fix | Delete
[47] Fix | Delete
if overlapped[0]:
[48] Fix | Delete
openmode |= _winapi.FILE_FLAG_OVERLAPPED
[49] Fix | Delete
[50] Fix | Delete
if overlapped[1]:
[51] Fix | Delete
flags_and_attribs = _winapi.FILE_FLAG_OVERLAPPED
[52] Fix | Delete
else:
[53] Fix | Delete
flags_and_attribs = 0
[54] Fix | Delete
[55] Fix | Delete
h1 = h2 = None
[56] Fix | Delete
try:
[57] Fix | Delete
h1 = _winapi.CreateNamedPipe(
[58] Fix | Delete
address, openmode, _winapi.PIPE_WAIT,
[59] Fix | Delete
1, obsize, ibsize, _winapi.NMPWAIT_WAIT_FOREVER, _winapi.NULL)
[60] Fix | Delete
[61] Fix | Delete
h2 = _winapi.CreateFile(
[62] Fix | Delete
address, access, 0, _winapi.NULL, _winapi.OPEN_EXISTING,
[63] Fix | Delete
flags_and_attribs, _winapi.NULL)
[64] Fix | Delete
[65] Fix | Delete
ov = _winapi.ConnectNamedPipe(h1, overlapped=True)
[66] Fix | Delete
ov.GetOverlappedResult(True)
[67] Fix | Delete
return h1, h2
[68] Fix | Delete
except:
[69] Fix | Delete
if h1 is not None:
[70] Fix | Delete
_winapi.CloseHandle(h1)
[71] Fix | Delete
if h2 is not None:
[72] Fix | Delete
_winapi.CloseHandle(h2)
[73] Fix | Delete
raise
[74] Fix | Delete
[75] Fix | Delete
[76] Fix | Delete
# Wrapper for a pipe handle
[77] Fix | Delete
[78] Fix | Delete
[79] Fix | Delete
class PipeHandle:
[80] Fix | Delete
"""Wrapper for an overlapped pipe handle which is vaguely file-object like.
[81] Fix | Delete
[82] Fix | Delete
The IOCP event loop can use these instead of socket objects.
[83] Fix | Delete
"""
[84] Fix | Delete
def __init__(self, handle):
[85] Fix | Delete
self._handle = handle
[86] Fix | Delete
[87] Fix | Delete
def __repr__(self):
[88] Fix | Delete
if self._handle is not None:
[89] Fix | Delete
handle = f'handle={self._handle!r}'
[90] Fix | Delete
else:
[91] Fix | Delete
handle = 'closed'
[92] Fix | Delete
return f'<{self.__class__.__name__} {handle}>'
[93] Fix | Delete
[94] Fix | Delete
@property
[95] Fix | Delete
def handle(self):
[96] Fix | Delete
return self._handle
[97] Fix | Delete
[98] Fix | Delete
def fileno(self):
[99] Fix | Delete
if self._handle is None:
[100] Fix | Delete
raise ValueError("I/O operation on closed pipe")
[101] Fix | Delete
return self._handle
[102] Fix | Delete
[103] Fix | Delete
def close(self, *, CloseHandle=_winapi.CloseHandle):
[104] Fix | Delete
if self._handle is not None:
[105] Fix | Delete
CloseHandle(self._handle)
[106] Fix | Delete
self._handle = None
[107] Fix | Delete
[108] Fix | Delete
def __del__(self, _warn=warnings.warn):
[109] Fix | Delete
if self._handle is not None:
[110] Fix | Delete
_warn(f"unclosed {self!r}", ResourceWarning, source=self)
[111] Fix | Delete
self.close()
[112] Fix | Delete
[113] Fix | Delete
def __enter__(self):
[114] Fix | Delete
return self
[115] Fix | Delete
[116] Fix | Delete
def __exit__(self, t, v, tb):
[117] Fix | Delete
self.close()
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
# Replacement for subprocess.Popen using overlapped pipe handles
[121] Fix | Delete
[122] Fix | Delete
[123] Fix | Delete
class Popen(subprocess.Popen):
[124] Fix | Delete
"""Replacement for subprocess.Popen using overlapped pipe handles.
[125] Fix | Delete
[126] Fix | Delete
The stdin, stdout, stderr are None or instances of PipeHandle.
[127] Fix | Delete
"""
[128] Fix | Delete
def __init__(self, args, stdin=None, stdout=None, stderr=None, **kwds):
[129] Fix | Delete
assert not kwds.get('universal_newlines')
[130] Fix | Delete
assert kwds.get('bufsize', 0) == 0
[131] Fix | Delete
stdin_rfd = stdout_wfd = stderr_wfd = None
[132] Fix | Delete
stdin_wh = stdout_rh = stderr_rh = None
[133] Fix | Delete
if stdin == PIPE:
[134] Fix | Delete
stdin_rh, stdin_wh = pipe(overlapped=(False, True), duplex=True)
[135] Fix | Delete
stdin_rfd = msvcrt.open_osfhandle(stdin_rh, os.O_RDONLY)
[136] Fix | Delete
else:
[137] Fix | Delete
stdin_rfd = stdin
[138] Fix | Delete
if stdout == PIPE:
[139] Fix | Delete
stdout_rh, stdout_wh = pipe(overlapped=(True, False))
[140] Fix | Delete
stdout_wfd = msvcrt.open_osfhandle(stdout_wh, 0)
[141] Fix | Delete
else:
[142] Fix | Delete
stdout_wfd = stdout
[143] Fix | Delete
if stderr == PIPE:
[144] Fix | Delete
stderr_rh, stderr_wh = pipe(overlapped=(True, False))
[145] Fix | Delete
stderr_wfd = msvcrt.open_osfhandle(stderr_wh, 0)
[146] Fix | Delete
elif stderr == STDOUT:
[147] Fix | Delete
stderr_wfd = stdout_wfd
[148] Fix | Delete
else:
[149] Fix | Delete
stderr_wfd = stderr
[150] Fix | Delete
try:
[151] Fix | Delete
super().__init__(args, stdin=stdin_rfd, stdout=stdout_wfd,
[152] Fix | Delete
stderr=stderr_wfd, **kwds)
[153] Fix | Delete
except:
[154] Fix | Delete
for h in (stdin_wh, stdout_rh, stderr_rh):
[155] Fix | Delete
if h is not None:
[156] Fix | Delete
_winapi.CloseHandle(h)
[157] Fix | Delete
raise
[158] Fix | Delete
else:
[159] Fix | Delete
if stdin_wh is not None:
[160] Fix | Delete
self.stdin = PipeHandle(stdin_wh)
[161] Fix | Delete
if stdout_rh is not None:
[162] Fix | Delete
self.stdout = PipeHandle(stdout_rh)
[163] Fix | Delete
if stderr_rh is not None:
[164] Fix | Delete
self.stderr = PipeHandle(stderr_rh)
[165] Fix | Delete
finally:
[166] Fix | Delete
if stdin == PIPE:
[167] Fix | Delete
os.close(stdin_rfd)
[168] Fix | Delete
if stdout == PIPE:
[169] Fix | Delete
os.close(stdout_wfd)
[170] Fix | Delete
if stderr == PIPE:
[171] Fix | Delete
os.close(stderr_wfd)
[172] Fix | Delete
[173] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function