def validate_handle(handle): try: win32api.GetHandleInformation(handle) except win32api.error as ex: if ex.winerror == 6: # ERROR_INVALID_HANDLE return False else: raise else: return True
def get_socket_inherit(socket): ''' Returns True if the socket has been set to allow inheritance across forks and execs to child processes, otherwise False ''' try: if iswindows: import win32api, win32con flags = win32api.GetHandleInformation(socket.fileno()) return bool(flags & win32con.HANDLE_FLAG_INHERIT) else: import fcntl flags = fcntl.fcntl(socket.fileno(), fcntl.F_GETFD) return not bool(flags & fcntl.FD_CLOEXEC) except: import traceback traceback.print_exc()
def assert_not_inheritable(f): if win32api.GetHandleInformation(msvcrt.get_osfhandle( f.fileno())) & 0b1: raise SystemExit('File handle is inheritable!')