|
""" |
|
Derived from |
|
|
|
> https://github.com/rudolfwalter/pygdbmi/blob/0.7.4.2/pygdbmi/gdbcontroller.py |
|
> MIT License https://github.com/rudolfwalter/pygdbmi/blob/master/LICENSE |
|
> Copyright (c) 2016 Chad Smith <grassfedcode <at> gmail.com> |
|
""" |
|
|
|
import os |
|
|
|
if os.name == "nt": |
|
import msvcrt |
|
from ctypes import POINTER, WinError, byref, windll, wintypes |
|
from ctypes.wintypes import BOOL, DWORD, HANDLE |
|
else: |
|
import fcntl |
|
|
|
|
|
def make_non_blocking(file_obj): |
|
""" |
|
make file object non-blocking |
|
|
|
Windows doesn't have the fcntl module, but someone on |
|
stack overflow supplied this code as an answer, and it works |
|
http://stackoverflow.com/a/34504971/2893090 |
|
""" |
|
|
|
if os.name == "nt": |
|
LPDWORD = POINTER(DWORD) |
|
PIPE_NOWAIT = wintypes.DWORD(0x00000001) |
|
|
|
SetNamedPipeHandleState = windll.kernel32.SetNamedPipeHandleState |
|
SetNamedPipeHandleState.argtypes = [HANDLE, LPDWORD, LPDWORD, LPDWORD] |
|
SetNamedPipeHandleState.restype = BOOL |
|
|
|
h = msvcrt.get_osfhandle(file_obj.fileno()) |
|
|
|
res = windll.kernel32.SetNamedPipeHandleState(h, byref(PIPE_NOWAIT), None, None) |
|
if res == 0: |
|
raise ValueError(WinError()) |
|
|
|
else: |
|
|
|
|
|
|
|
fcntl.fcntl(file_obj, fcntl.F_SETFL, os.O_NONBLOCK) |
|
|