def write(self, data: bytes) -> None: """Write bytes to an IPC connection.""" if sys.platform == 'win32': try: _winapi.WriteFile(self.connection, data) # this empty write is to copy the behavior of socket.sendall, # which also sends an empty message to signify it is done writing _winapi.WriteFile(self.connection, b'') except WindowsError as e: raise IPCException("Failed to write with error: {}".format(e.winerror)) else: self.connection.sendall(data) self.connection.shutdown(socket.SHUT_WR)
def write(self, data: bytes) -> None: """Write bytes to an IPC connection.""" if sys.platform == 'win32': try: ov, err = _winapi.WriteFile(self.connection, data, overlapped=True) # TODO: remove once typeshed supports Literal types assert isinstance(ov, _winapi.Overlapped) assert isinstance(err, int) try: if err == _winapi.ERROR_IO_PENDING: timeout = int( self.timeout * 1000) if self.timeout else _winapi.INFINITE res = _winapi.WaitForSingleObject(ov.event, timeout) if res != _winapi.WAIT_OBJECT_0: raise IPCException( f"Bad result from I/O wait: {res}") elif err != 0: raise IPCException( f"Failed writing to pipe with error: {err}") except BaseException: ov.cancel() raise bytes_written, err = ov.GetOverlappedResult(True) assert err == 0, err assert bytes_written == len(data) except OSError as e: raise IPCException( f"Failed to write with error: {e.winerror}") from e else: self.connection.sendall(data) self.connection.shutdown(socket.SHUT_WR)
def write(self, data: bytes) -> None: """Write bytes to an IPC connection.""" if sys.platform == 'win32': try: # Only send data if there is data to send, to avoid it # being confused with the empty message sent to terminate # the connection. (We will still send the end-of-message # empty message below, which will cause read to return.) if data: _winapi.WriteFile(self.connection, data) # this empty write is to copy the behavior of socket.sendall, # which also sends an empty message to signify it is done writing _winapi.WriteFile(self.connection, b'') except WindowsError as e: raise IPCException("Failed to write with error: {}".format( e.winerror)) else: self.connection.sendall(data) self.connection.shutdown(socket.SHUT_WR)
def _send_bytes(self, buf): ov, err = _winapi.WriteFile(self._handle, buf, overlapped=True) try: if err == _winapi.ERROR_IO_PENDING: waitres = _winapi.WaitForMultipleObjects([ov.event], False, INFINITE) assert waitres == WAIT_OBJECT_0 except: ov.cancel() raise finally: nwritten, err = ov.GetOverlappedResult(True) assert err == 0 assert nwritten == len(buf)
def dispatch(self): rc = None draining = False while rc is None or draining: if rc is None: rc = self.proc.poll() all_fileobj = [] if self.stdout is not None: all_fileobj.append(self.stdout) if self.stderr is not None: all_fileobj.append(self.stderr) if rc is None and self.io_handlers._hasInput( ) and self.stdin is not None: all_fileobj.append(self.stdio) draining = False while rc is None or draining: all_handles = [ msvcrt.get_osfhandle(fileobj.fileno()) for fileobj in all_fileobj ] result = _winapi.WaitForMultipleObjects(all_handles, 0, 10) print('result %r' % (result, )) if result == _winapi.WAIT_TIMEOUT: break handle = all_handles[result] fileobj = all_fileobj[result] if fileobj == self.stdout: try: text, err = _winapi.ReadFile(handle, 128, 0) if len(text) > 0: draining = True self.io_handlers._handleStdOut( text.decode(sys.getdefaultencoding())) except BrokenPipeError: self.stdout = None elif fileobj == self.stderr: try: text, err = _winapi.ReadFile(handle, 1024, 0) assert err == 0 if len(text) > 0: draining = True self.io_handlers._handleStdErr( text.decode(sys.getdefaultencoding())) except BrokenPipeError: self.stderr = None elif fileobj == self.stdin: written, err = _winapi.WriteFile( handle, self.io_handlers._getInput().encode( sys.getdefaultencoding()), False) self.io_handlers._dispatch() return rc
if os.name == "nt": from ctypes import windll from ctypes.wintypes import BOOL, HANDLE, WORD handle = _winapi.GetStdHandle(_winapi.STD_OUTPUT_HANDLE) kernel32 = windll.LoadLibrary("Kernel32.dll") SetConsoleAttribute = kernel32.SetConsoleTextAttribute SetConsoleAttribute.argtypes = (HANDLE, WORD) SetConsoleAttribute.restype = BOOL # FOREGROUND_INTENSITY|FOREGROUND_RED res: bool = SetConsoleAttribute(handle, 5) print(res) string = "Hello World!" _winapi.WriteFile(handle, string.encode("utf-8"), 0) else: pass # exit(0) color_print(94) color_print(41, 93) color_print(36, 40) print() color_print(48, *rgb(200, 100, 20), 34) color_print(48, *rgb(255, 161, 72)) color_print(48, *rgb(255, 161, 72), 38, *rgb(1, 50, 255)) color_print(48, *rgb(178, 112, 50), 38, *rgb(98, 96, 167)) print() color_print(48, *rgb(100, 10, 10), )
def sendMessage(fileHandle, message): winAPI.WriteFile(fileHandle, message, 0)