"""Support routines for subprocess module.Currently, this extension module is only required when using thesubprocess module on Windows."""# Declare external Win32 functionsimportctypes_kernel32=ctypes.WinDLL('kernel32')_CloseHandle=_kernel32.CloseHandle_CloseHandle.argtypes=[ctypes.c_int]_CloseHandle.restype=ctypes.c_int_CreatePipe=_kernel32.CreatePipe_CreatePipe.argtypes=[ctypes.POINTER(ctypes.c_int),ctypes.POINTER(ctypes.c_int),ctypes.c_void_p,ctypes.c_int]_CreatePipe.restype=ctypes.c_int_GetCurrentProcess=_kernel32.GetCurrentProcess_GetCurrentProcess.argtypes=[]_GetCurrentProcess.restype=ctypes.c_intGetVersion=_kernel32.GetVersionGetVersion.argtypes=[]GetVersion.restype=ctypes.c_int_DuplicateHandle=_kernel32.DuplicateHandle_DuplicateHandle.argtypes=[ctypes.c_int,ctypes.c_int,ctypes.c_int,ctypes.POINTER(ctypes.c_int),ctypes.c_int,ctypes.c_int,ctypes.c_int]_DuplicateHandle.restype=ctypes.c_int_WaitForSingleObject=_kernel32.WaitForSingleObject_WaitForSingleObject.argtypes=[ctypes.c_int,ctypes.c_uint]_WaitForSingleObject.restype=ctypes.c_int_GetExitCodeProcess=_kernel32.GetExitCodeProcess_GetExitCodeProcess.argtypes=[ctypes.c_int,ctypes.POINTER(ctypes.c_int)]_GetExitCodeProcess.restype=ctypes.c_int_TerminateProcess=_kernel32.TerminateProcess_TerminateProcess.argtypes=[ctypes.c_int,ctypes.c_int]_TerminateProcess.restype=ctypes.c_int_GetStdHandle=_kernel32.GetStdHandle_GetStdHandle.argtypes=[ctypes.c_int]_GetStdHandle.restype=ctypes.c_intclass_STARTUPINFO(ctypes.Structure):_fields_=[('cb',ctypes.c_int),('lpReserved',ctypes.c_void_p),('lpDesktop',ctypes.c_char_p),('lpTitle',ctypes.c_char_p),('dwX',ctypes.c_int),('dwY',ctypes.c_int),('dwXSize',ctypes.c_int),('dwYSize',ctypes.c_int),('dwXCountChars',ctypes.c_int),('dwYCountChars',ctypes.c_int),("dwFillAttribute",ctypes.c_int),("dwFlags",ctypes.c_int),("wShowWindow",ctypes.c_short),("cbReserved2",ctypes.c_short),("lpReserved2",ctypes.c_void_p),("hStdInput",ctypes.c_int),("hStdOutput",ctypes.c_int),("hStdError",ctypes.c_int)]class_PROCESS_INFORMATION(ctypes.Structure):_fields_=[("hProcess",ctypes.c_int),("hThread",ctypes.c_int),("dwProcessID",ctypes.c_int),("dwThreadID",ctypes.c_int)]_CreateProcess=_kernel32.CreateProcessA_CreateProcess.argtypes=[ctypes.c_char_p,ctypes.c_char_p,ctypes.c_void_p,ctypes.c_void_p,ctypes.c_int,ctypes.c_int,ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(_STARTUPINFO),ctypes.POINTER(_PROCESS_INFORMATION)]_CreateProcess.restype=ctypes.c_intdelctypes# Now the _subprocess module implementation fromctypesimportc_intas_c_int,byrefas_byref,WinErroras_WinErrorclass_handle:def__init__(self,handle):self.handle=handledef__int__(self):returnself.handledef__del__(self):ifself.handleisnotNone:_CloseHandle(self.handle)defDetach(self):handle,self.handle=self.handle,NonereturnhandledefClose(self):ifself.handlenotin(-1,None):_CloseHandle(self.handle)self.handle=NonedefCreatePipe(attributes,size):read=_c_int()write=_c_int()res=_CreatePipe(_byref(read),_byref(write),None,size)ifnotres:raise_WinError()return_handle(read.value),_handle(write.value)defGetCurrentProcess():return_handle(_GetCurrentProcess())defDuplicateHandle(source_process,source,target_process,access,inherit,options=0):target=_c_int()res=_DuplicateHandle(int(source_process),int(source),int(target_process),_byref(target),access,inherit,options)ifnotres:raise_WinError()return_handle(target.value)DUPLICATE_SAME_ACCESS=2defCreateProcess(name,command_line,process_attr,thread_attr,inherit,flags,env,start_dir,startup_info):si=_STARTUPINFO()ifstartup_infoisnotNone:si.dwFlags=startup_info.dwFlagssi.wShowWindow=startup_info.wShowWindowifstartup_info.hStdInput:si.hStdInput=int(startup_info.hStdInput)ifstartup_info.hStdOutput:si.hStdOutput=int(startup_info.hStdOutput)ifstartup_info.hStdError:si.hStdError=int(startup_info.hStdError)pi=_PROCESS_INFORMATION()ifenvisnotNone:envbuf=""fork,vinenv.iteritems():envbuf+="%s=%s\0"%(k,v)envbuf+='\0'else:envbuf=Noneres=_CreateProcess(name,command_line,None,None,inherit,flags,envbuf,start_dir,_byref(si),_byref(pi))ifnotres:raise_WinError()return_handle(pi.hProcess),_handle(pi.hThread),pi.dwProcessID,pi.dwThreadIDSTARTF_USESHOWWINDOW=0x001STARTF_USESTDHANDLES=0x100SW_HIDE=0CREATE_NEW_CONSOLE=0x010CREATE_NEW_PROCESS_GROUP=0x200defWaitForSingleObject(handle,milliseconds):res=_WaitForSingleObject(int(handle),milliseconds)ifres<0:raise_WinError()returnresINFINITE=0xffffffffWAIT_OBJECT_0=0defGetExitCodeProcess(handle):code=_c_int()res=_GetExitCodeProcess(int(handle),_byref(code))ifnotres:raise_WinError()returncode.valuedefTerminateProcess(handle,exitcode):res=_TerminateProcess(int(handle),exitcode)ifnotres:raise_WinError()defGetStdHandle(stdhandle):res=_GetStdHandle(stdhandle)ifnotres:returnNoneelse:returnresSTD_INPUT_HANDLE=-10STD_OUTPUT_HANDLE=-11STD_ERROR_HANDLE=-12