1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# subprocess - Subprocesses with accessible I/O streams 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# For more information about this module, see PEP 324. 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This module should remain compatible with Python 2.2, see PEP 291. 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se> 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Licensed to PSF under a Contributor Agreement. 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# See http://www.python.org/2.4/license for licensing details. 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepr"""subprocess - Subprocesses with accessible I/O streams 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis module allows you to spawn processes, connect to their 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepinput/output/error pipes, and obtain their return codes. This module 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepintends to replace several other, older modules and functions, like: 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepos.system 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepos.spawn* 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepos.popen* 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppopen2.* 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcommands.* 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepInformation about how the subprocess module can be used to replace these 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmodules and functions can be found below. 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepUsing the subprocess module 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep=========================== 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis module defines one class called Popen: 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Popen(args, bufsize=0, executable=None, 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=None, stdout=None, stderr=None, 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep preexec_fn=None, close_fds=False, shell=False, 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd=None, env=None, universal_newlines=False, 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo=None, creationflags=0): 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepArguments are: 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepargs should be a string, or a sequence of program arguments. The 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprogram to execute is normally the first item in the args sequence or 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstring, but can be explicitly set by using the executable argument. 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn UNIX, with shell=False (default): In this case, the Popen class 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepuses os.execvp() to execute the child program. args should normally 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbe a sequence. A string will be treated as a sequence with the string 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepas the only item (the program to execute). 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn UNIX, with shell=True: If args is a string, it specifies the 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcommand string to execute through the shell. If args is a sequence, 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe first item specifies the command string, and any additional items 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepwill be treated as additional shell arguments. 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn Windows: the Popen class uses CreateProcess() to execute the child 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprogram, which operates on strings. If args is a sequence, it will be 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepconverted to a string using the list2cmdline method. Please note that 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnot all MS Windows applications interpret the command line the same 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepway: The list2cmdline is designed for applications using the same 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprules as the MS C runtime. 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbufsize, if given, has the same meaning as the corresponding argument 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepto the built-in open() function: 0 means unbuffered, 1 means line 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbuffered, any other positive value means use a buffer of 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(approximately) that size. A negative bufsize means to use the system 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdefault, which usually means fully buffered. The default value for 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbufsize is 0 (unbuffered). 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstdin, stdout and stderr specify the executed programs' standard 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepinput, standard output and standard error file handles, respectively. 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepValid values are PIPE, an existing file descriptor (a positive 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepinteger), an existing file object, and None. PIPE indicates that a 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepnew pipe to the child should be created. With None, no redirection 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepwill occur; the child's file handles will be inherited from the 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepparent. Additionally, stderr can be STDOUT, which indicates that the 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstderr data from the applications should be captured into the same 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfile handle as for stdout. 79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf preexec_fn is set to a callable object, this object will be called 81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepin the child process just before the child is executed. 82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf close_fds is true, all file descriptors except 0, 1 and 2 will be 84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclosed before the child process is executed. 85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif shell is true, the specified command will be executed through the 87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepshell. 88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf cwd is not None, the current directory will be changed to cwd 90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepbefore the child is executed. 91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf env is not None, it defines the environment variables for the new 93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprocess. 94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIf universal_newlines is true, the file objects stdout and stderr are 96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepopened as a text files, but lines may be terminated by any of '\n', 97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe Unix end-of-line convention, '\r', the Macintosh convention or 98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep'\r\n', the Windows convention. All of these external representations 99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepare seen as '\n' by the Python program. Note: This feature is only 100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepavailable if Python is built with universal newline support (the 101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdefault). Also, the newlines attribute of the file objects stdout, 102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstdin and stderr are not updated by the communicate() method. 103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe startupinfo and creationflags, if given, will be passed to the 105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepunderlying CreateProcess() function. They can specify things such as 106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepappearance of the main window and priority for the new process. 107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(Windows only) 108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThis module also defines some shortcut functions: 111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcall(*popenargs, **kwargs): 113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Run command with arguments. Wait for command to complete, then 114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return the returncode attribute. 115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep retcode = call(["ls", "-l"]) 119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcheck_call(*popenargs, **kwargs): 121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Run command with arguments. Wait for command to complete. If the 122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exit code was zero then return, otherwise raise 123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep CalledProcessError. The CalledProcessError object will have the 124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return code in the returncode attribute. 125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check_call(["ls", "-l"]) 129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcheck_output(*popenargs, **kwargs): 131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Run command with arguments and return its output as a byte string. 132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If the exit code was non-zero it raises a CalledProcessError. The 134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep CalledProcessError object will have the return code in the returncode 135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute and output in the output attribute. 136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep output = check_output(["ls", "-l", "/dev/null"]) 140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExceptions 143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep---------- 144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepExceptions raised in the child process, before the new program has 145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstarted to execute, will be re-raised in the parent. Additionally, 146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe exception object will have one extra attribute called 147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep'child_traceback', which is a string containing traceback information 148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom the childs point of view. 149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe most common exception raised is OSError. This occurs, for 151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexample, when trying to execute a non-existent file. Applications 152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepshould prepare for OSErrors. 153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepA ValueError will be raised if Popen is called with invalid arguments. 155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcheck_call() and check_output() will raise CalledProcessError, if the 157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcalled process returns a non-zero return code. 158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSecurity 161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep-------- 162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepUnlike some other popen functions, this implementation will never call 163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep/bin/sh implicitly. This means that all characters, including shell 164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmetacharacters, can safely be passed to child processes. 165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepPopen objects 168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep============= 169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepInstances of the Popen class have the following methods: 170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppoll() 172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Check if child process has terminated. Returns returncode 173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute. 174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepwait() 176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Wait for child process to terminate. Returns returncode attribute. 177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcommunicate(input=None) 179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Interact with process: Send data to stdin. Read data from stdout 180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep and stderr, until end-of-file is reached. Wait for process to 181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep terminate. The optional input argument should be a string to be 182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep sent to the child process, or None, if no data should be sent to 183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep the child. 184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep communicate() returns a tuple (stdout, stderr). 186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Note: The data read is buffered in memory, so do not use this 188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep method if the data size is large or unlimited. 189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe following attributes are also available: 191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstdin 193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If the stdin argument is PIPE, this attribute is a file object 194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep that provides input to the child process. Otherwise, it is None. 195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstdout 197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If the stdout argument is PIPE, this attribute is a file object 198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep that provides output from the child process. Otherwise, it is 199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep None. 200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepstderr 202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If the stderr argument is PIPE, this attribute is file object that 203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep provides error output from the child process. Otherwise, it is 204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep None. 205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppid 207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The process ID of the child process. 208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepreturncode 210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The child return code. A None value indicates that the process 211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep hasn't terminated yet. A negative value -N indicates that the 212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child was terminated by signal N (UNIX only). 213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing older functions with the subprocess module 216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==================================================== 217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIn this section, "a ==> b" means that b can be used as a replacement 218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfor a. 219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepNote: All functions in this section fail (more or less) silently if 221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe executed program cannot be found; this module raises an OSError 222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexception. 223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepIn the following examples, we assume that the subprocess module is 225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimported with "from subprocess import *". 226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing /bin/sh shell backquote 229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep--------------------------------- 230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepoutput=`mycmd myarg` 231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepoutput = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] 233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing shell pipe line 236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep------------------------- 237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepoutput=`dmesg | grep hda` 238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp1 = Popen(["dmesg"], stdout=PIPE) 240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) 241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepoutput = p2.communicate()[0] 242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing os.system() 245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep--------------------- 246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepsts = os.system("mycmd" + " myarg") 247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen("mycmd" + " myarg", shell=True) 249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppid, sts = os.waitpid(p.pid, 0) 250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepNote: 252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* Calling the program through the shell is usually not required. 254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* It's easier to look at the returncode attribute than the 256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exitstatus. 257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepA more real-world example would look like this: 259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry: 261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep retcode = call("mycmd" + " myarg", shell=True) 262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if retcode < 0: 263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print >>sys.stderr, "Child was terminated by signal", -retcode 264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print >>sys.stderr, "Child returned", retcode 266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept OSError, e: 267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print >>sys.stderr, "Execution failed:", e 268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing os.spawn* 271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep------------------- 272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepP_NOWAIT example: 273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg") 275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppid = Popen(["/bin/mycmd", "myarg"]).pid 277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepP_WAIT example: 280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepretcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg") 282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepretcode = call(["/bin/mycmd", "myarg"]) 284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepVector example: 287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepos.spawnvp(os.P_NOWAIT, path, args) 289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepPopen([path] + args[1:]) 291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepEnvironment example: 294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepos.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) 296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepPopen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"}) 298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing os.popen* 301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep------------------- 302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppipe = os.popen("cmd", mode='r', bufsize) 303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppipe = Popen("cmd", shell=True, bufsize=bufsize, stdout=PIPE).stdout 305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppipe = os.popen("cmd", mode='w', bufsize) 307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppipe = Popen("cmd", shell=True, bufsize=bufsize, stdin=PIPE).stdin 309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout) = os.popen2("cmd", mode, bufsize) 312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen("cmd", shell=True, bufsize=bufsize, 314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=PIPE, stdout=PIPE, close_fds=True) 315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout) = (p.stdin, p.stdout) 316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, 319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child_stdout, 320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child_stderr) = os.popen3("cmd", mode, bufsize) 321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen("cmd", shell=True, bufsize=bufsize, 323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) 324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, 325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child_stdout, 326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child_stderr) = (p.stdin, p.stdout, p.stderr) 327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout_and_stderr) = os.popen4("cmd", mode, 330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bufsize) 331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen("cmd", shell=True, bufsize=bufsize, 333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout) 335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn Unix, os.popen2, os.popen3 and os.popen4 also accept a sequence as 337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthe command to execute, in which case arguments will be passed 338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdirectly to the program without shell intervention. This usage can be 339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepreplaced as follows: 340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout) = os.popen2(["/bin/ls", "-l"], mode, 342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bufsize) 343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen(["/bin/ls", "-l"], bufsize=bufsize, stdin=PIPE, stdout=PIPE) 345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdin, child_stdout) = (p.stdin, p.stdout) 346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReturn code handling translates as follows: 348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeppipe = os.popen("cmd", 'w') 350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... 351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeprc = pipe.close() 352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif rc is not None and rc % 256: 353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "There were some errors" 354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprocess = Popen("cmd", 'w', shell=True, stdin=PIPE) 356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep... 357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprocess.stdin.close() 358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif process.wait() != 0: 359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "There were some errors" 360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepReplacing popen2.* 363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep------------------ 364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode) 365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen(["somestring"], shell=True, bufsize=bufsize 367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=PIPE, stdout=PIPE, close_fds=True) 368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdout, child_stdin) = (p.stdout, p.stdin) 369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepOn Unix, popen2 also accepts a sequence as the command to execute, in 371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepwhich case arguments will be passed directly to the program without 372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepshell intervention. This usage can be replaced as follows: 373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, 375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep mode) 376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep==> 377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp = Popen(["mycmd", "myarg"], bufsize=bufsize, 378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=PIPE, stdout=PIPE, close_fds=True) 379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep(child_stdout, child_stdin) = (p.stdout, p.stdin) 380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepThe popen2.Popen3 and popen2.Popen4 basically works as subprocess.Popen, 382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept that: 383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* subprocess.Popen raises an exception if the execution fails 385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* the capturestderr argument is replaced with the stderr argument. 386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* stdin=PIPE and stdout=PIPE must be specified. 387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep* popen2 closes all filedescriptors by default, but you have to specify 388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep close_fds=True with subprocess.Popen. 389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys 392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepmswindows = (sys.platform == "win32") 393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport os 395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport types 396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport traceback 397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport gc 398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport signal 399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport errno 400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Exception classes used by this module. 402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass CalledProcessError(Exception): 403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """This exception is raised when a process run by check_call() or 404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check_output() returns a non-zero exit status. 405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The exit status will be stored in the returncode attribute; 406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check_output() will also store the output in the output attribute. 407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, returncode, cmd, output=None): 409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = returncode 410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.cmd = cmd 411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.output = output 412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __str__(self): 413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) 414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif mswindows: 417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import threading 418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import msvcrt 419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import _subprocess 420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class STARTUPINFO: 421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep dwFlags = 0 422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep hStdInput = None 423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep hStdOutput = None 424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep hStdError = None 425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep wShowWindow = 0 426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep class pywintypes: 427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep error = IOError 428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepelse: 429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import select 430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _has_poll = hasattr(select, 'poll') 431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import fcntl 432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep import pickle 433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # When select or poll has indicated that the file is writable, 435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # we can write up to _PIPE_BUF bytes without risk of blocking. 436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # POSIX defines PIPE_BUF as >= 512. 437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _PIPE_BUF = getattr(select, 'PIPE_BUF', 512) 438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep__all__ = ["Popen", "PIPE", "STDOUT", "call", "check_call", 441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "check_output", "CalledProcessError"] 442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif mswindows: 444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep from _subprocess import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP, 445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, 446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep STD_ERROR_HANDLE, SW_HIDE, 447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep STARTF_USESTDHANDLES, STARTF_USESHOWWINDOW) 448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep __all__.extend(["CREATE_NEW_CONSOLE", "CREATE_NEW_PROCESS_GROUP", 450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "STD_INPUT_HANDLE", "STD_OUTPUT_HANDLE", 451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "STD_ERROR_HANDLE", "SW_HIDE", 452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "STARTF_USESTDHANDLES", "STARTF_USESHOWWINDOW"]) 453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoeptry: 454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep MAXFD = os.sysconf("SC_OPEN_MAX") 455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepexcept: 456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep MAXFD = 256 457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep_active = [] 459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _cleanup(): 461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for inst in _active[:]: 462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep res = inst._internal_poll(_deadstate=sys.maxint) 463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if res is not None: 464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _active.remove(inst) 466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except ValueError: 467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This can happen if two threads create a new Popen instance. 468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # It's harmless that it was already removed, so ignore. 469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepPIPE = -1 472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSTDOUT = -2 473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _eintr_retry_call(func, *args): 476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while True: 477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return func(*args) 479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except (OSError, IOError) as e: 480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno == errno.EINTR: 481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# XXX This function is only used by multiprocessing and the test suite, 486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# but it's here so that it can be imported when Python is compiled without 487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# threads. 488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _args_from_interpreter_flags(): 490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Return a list of command-line arguments reproducing the current 491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep settings in sys.flags and sys.warnoptions.""" 492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep flag_opt_map = { 493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'debug': 'd', 494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 'inspect': 'i', 495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 'interactive': 'i', 496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'optimize': 'O', 497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'dont_write_bytecode': 'B', 498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'no_user_site': 's', 499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'no_site': 'S', 500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'ignore_environment': 'E', 501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'verbose': 'v', 502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'bytes_warning': 'b', 503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'hash_randomization': 'R', 504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'py3k_warning': '3', 505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep } 506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = [] 507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for flag, opt in flag_opt_map.items(): 508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep v = getattr(sys.flags, flag) 509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if v > 0: 510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args.append('-' + opt * v) 511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for opt in sys.warnoptions: 512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args.append('-W' + opt) 513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return args 514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef call(*popenargs, **kwargs): 517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Run command with arguments. Wait for command to complete, then 518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return the returncode attribute. 519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep retcode = call(["ls", "-l"]) 523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return Popen(*popenargs, **kwargs).wait() 525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_call(*popenargs, **kwargs): 528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Run command with arguments. Wait for command to complete. If 529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep the exit code was zero then return, otherwise raise 530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep CalledProcessError. The CalledProcessError object will have the 531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return code in the returncode attribute. 532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep check_call(["ls", "-l"]) 536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep retcode = call(*popenargs, **kwargs) 538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if retcode: 539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cmd = kwargs.get("args") 540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if cmd is None: 541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cmd = popenargs[0] 542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise CalledProcessError(retcode, cmd) 543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 0 544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef check_output(*popenargs, **kwargs): 547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r"""Run command with arguments and return its output as a byte string. 548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep If the exit code was non-zero it raises a CalledProcessError. The 550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep CalledProcessError object will have the return code in the returncode 551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute and output in the output attribute. 552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The arguments are the same as for the Popen constructor. Example: 554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> check_output(["ls", "-l", "/dev/null"]) 556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' 557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep The stdout argument is not allowed as it is used internally. 559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep To capture standard error in the result, use stderr=STDOUT. 560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep >>> check_output(["/bin/sh", "-c", 562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... "ls -l non_existent_file ; exit 0"], 563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ... stderr=STDOUT) 564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 'ls: non_existent_file: No such file or directory\n' 565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if 'stdout' in kwargs: 567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError('stdout argument not allowed, it will be overridden.') 568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep process = Popen(stdout=PIPE, *popenargs, **kwargs) 569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep output, unused_err = process.communicate() 570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep retcode = process.poll() 571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if retcode: 572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cmd = kwargs.get("args") 573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if cmd is None: 574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cmd = popenargs[0] 575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise CalledProcessError(retcode, cmd, output=output) 576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return output 577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef list2cmdline(seq): 580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep Translate a sequence of arguments into a command line 582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep string, using the same rules as the MS C runtime: 583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1) Arguments are delimited by white space, which is either a 585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep space or a tab. 586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 2) A string surrounded by double quotation marks is 588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep interpreted as a single argument, regardless of white space 589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep contained within. A quoted string can be embedded in an 590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep argument. 591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 3) A double quotation mark preceded by a backslash is 593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep interpreted as a literal double quotation mark. 594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 4) Backslashes are interpreted literally, unless they 596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep immediately precede a double quotation mark. 597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 5) If backslashes immediately precede a double quotation mark, 599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep every pair of backslashes is interpreted as a literal 600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep backslash. If the number of backslashes is odd, the last 601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep backslash escapes the next double quotation mark as 602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep described in rule 3. 603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # See 606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # http://msdn.microsoft.com/en-us/library/17w5ykft.aspx 607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # or search http://msdn.microsoft.com for 608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # "Parsing C++ Command-Line Arguments" 609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result = [] 610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep needquote = False 611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for arg in seq: 612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bs_buf = [] 613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Add a space to separate this argument from the others 615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if result: 616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append(' ') 617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep needquote = (" " in arg) or ("\t" in arg) or not arg 619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if needquote: 620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append('"') 621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for c in arg: 623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c == '\\': 624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Don't know if we need to double yet. 625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bs_buf.append(c) 626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif c == '"': 627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Double backslashes. 628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append('\\' * len(bs_buf)*2) 629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bs_buf = [] 630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append('\\"') 631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Normal char 633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if bs_buf: 634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.extend(bs_buf) 635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bs_buf = [] 636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append(c) 637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Add remaining backslashes, if any. 639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if bs_buf: 640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.extend(bs_buf) 641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if needquote: 643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.extend(bs_buf) 644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep result.append('"') 645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return ''.join(result) 647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Popen(object): 650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __init__(self, args, bufsize=0, executable=None, 651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdin=None, stdout=None, stderr=None, 652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep preexec_fn=None, close_fds=False, shell=False, 653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd=None, env=None, universal_newlines=False, 654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo=None, creationflags=0): 655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Create new Popen instance.""" 656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _cleanup() 657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._child_created = False 659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not isinstance(bufsize, (int, long)): 660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise TypeError("bufsize must be an integer") 661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mswindows: 663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if preexec_fn is not None: 664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError("preexec_fn is not supported on Windows " 665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "platforms") 666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if close_fds and (stdin is not None or stdout is not None or 667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr is not None): 668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError("close_fds is not supported on Windows " 669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "platforms if you redirect stdin/stdout/stderr") 670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # POSIX 672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if startupinfo is not None: 673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError("startupinfo is only supported on Windows " 674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "platforms") 675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if creationflags != 0: 676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError("creationflags is only supported on Windows " 677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "platforms") 678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin = None 680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdout = None 681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stderr = None 682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.pid = None 683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = None 684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.universal_newlines = universal_newlines 685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Input and output objects. The general principle is like 687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # this: 688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Parent Child 690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ------ ----- 691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # p2cwrite ---stdin---> p2cread 692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # c2pread <--stdout--- c2pwrite 693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # errread <--stderr--- errwrite 694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # On POSIX, the child objects are file descriptors. On 696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Windows, these are Windows file handles. The parent objects 697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # are file descriptors on both platforms. The parent objects 698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # are None when not using PIPEs. The child objects are None 699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # when not redirecting. 700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep (p2cread, p2cwrite, 702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite) = self._get_handles(stdin, stdout, stderr) 704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._execute_child(args, executable, preexec_fn, close_fds, 707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd, env, universal_newlines, 708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo, creationflags, shell, 709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite, 710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite) 712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except Exception: 713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Preserve original exception in case os.close raises. 714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exc_type, exc_value, exc_trace = sys.exc_info() 715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep to_close = [] 717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Only close the pipes we created. 718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdin == PIPE: 719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep to_close.extend((p2cread, p2cwrite)) 720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout == PIPE: 721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep to_close.extend((c2pread, c2pwrite)) 722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr == PIPE: 723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep to_close.extend((errread, errwrite)) 724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for fd in to_close: 726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(fd) 728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except EnvironmentError: 729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise exc_type, exc_value, exc_trace 732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mswindows: 734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cwrite is not None: 735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cwrite = msvcrt.open_osfhandle(p2cwrite.Detach(), 0) 736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pread is not None: 737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread = msvcrt.open_osfhandle(c2pread.Detach(), 0) 738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errread is not None: 739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread = msvcrt.open_osfhandle(errread.Detach(), 0) 740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cwrite is not None: 742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin = os.fdopen(p2cwrite, 'wb', bufsize) 743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pread is not None: 744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if universal_newlines: 745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdout = os.fdopen(c2pread, 'rU', bufsize) 746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdout = os.fdopen(c2pread, 'rb', bufsize) 748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errread is not None: 749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if universal_newlines: 750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stderr = os.fdopen(errread, 'rU', bufsize) 751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stderr = os.fdopen(errread, 'rb', bufsize) 753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _translate_newlines(self, data): 756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = data.replace("\r\n", "\n") 757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = data.replace("\r", "\n") 758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return data 759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def __del__(self, _maxint=sys.maxint, _active=_active): 762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # If __init__ hasn't had a chance to execute (e.g. if it 763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # was passed an undeclared keyword argument), we don't 764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # have a _child_created attribute at all. 765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not getattr(self, '_child_created', False): 766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # We didn't get to successfully create a child process. 767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return 768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # In case the child hasn't been waited on, check if it's done. 769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._internal_poll(_deadstate=_maxint) 770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.returncode is None and _active is not None: 771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Child is still running, keep us alive until we can wait on it. 772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _active.append(self) 773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def communicate(self, input=None): 776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Interact with process: Send data to stdin. Read data from 777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout and stderr, until end-of-file is reached. Wait for 778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep process to terminate. The optional input argument should be a 779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep string to be sent to the child process, or None, if no data 780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep should be sent to the child. 781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep communicate() returns a tuple (stdout, stderr).""" 783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Optimization: If we are only using one pipe, or no pipe at 785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # all, using select() or threads is unnecessary. 786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if [self.stdin, self.stdout, self.stderr].count(None) >= 2: 787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = None 788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = None 789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin: 790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if input: 791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.write(input) 793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except IOError as e: 794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno != errno.EPIPE and e.errno != errno.EINVAL: 795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.close() 797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif self.stdout: 798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = _eintr_retry_call(self.stdout.read) 799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdout.close() 800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif self.stderr: 801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = _eintr_retry_call(self.stderr.read) 802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stderr.close() 803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.wait() 804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (stdout, stderr) 805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self._communicate(input) 807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def poll(self): 810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self._internal_poll() 811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mswindows: 814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Windows methods 816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _get_handles(self, stdin, stdout, stderr): 818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Construct and return tuple with IO objects: 819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite 820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdin is None and stdout is None and stderr is None: 822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (None, None, None, None, None, None) 823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite = None, None 825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite = None, None 826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite = None, None 827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdin is None: 829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = _subprocess.GetStdHandle(_subprocess.STD_INPUT_HANDLE) 830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cread is None: 831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, _ = _subprocess.CreatePipe(None, 0) 832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stdin == PIPE: 833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite = _subprocess.CreatePipe(None, 0) 834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stdin, int): 835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = msvcrt.get_osfhandle(stdin) 836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = msvcrt.get_osfhandle(stdin.fileno()) 839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = self._make_inheritable(p2cread) 840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout is None: 842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = _subprocess.GetStdHandle(_subprocess.STD_OUTPUT_HANDLE) 843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pwrite is None: 844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _, c2pwrite = _subprocess.CreatePipe(None, 0) 845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stdout == PIPE: 846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite = _subprocess.CreatePipe(None, 0) 847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stdout, int): 848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = msvcrt.get_osfhandle(stdout) 849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) 852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = self._make_inheritable(c2pwrite) 853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr is None: 855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = _subprocess.GetStdHandle(_subprocess.STD_ERROR_HANDLE) 856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errwrite is None: 857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _, errwrite = _subprocess.CreatePipe(None, 0) 858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stderr == PIPE: 859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite = _subprocess.CreatePipe(None, 0) 860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stderr == STDOUT: 861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = c2pwrite 862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stderr, int): 863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = msvcrt.get_osfhandle(stderr) 864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = msvcrt.get_osfhandle(stderr.fileno()) 867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = self._make_inheritable(errwrite) 868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (p2cread, p2cwrite, 870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite) 872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _make_inheritable(self, handle): 875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Return a duplicate of handle, which is inheritable""" 876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return _subprocess.DuplicateHandle(_subprocess.GetCurrentProcess(), 877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep handle, _subprocess.GetCurrentProcess(), 0, 1, 878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _subprocess.DUPLICATE_SAME_ACCESS) 879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _find_w9xpopen(self): 882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Find and return absolut path to w9xpopen.exe""" 883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep w9xpopen = os.path.join( 884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.path.dirname(_subprocess.GetModuleFileName(0)), 885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "w9xpopen.exe") 886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not os.path.exists(w9xpopen): 887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Eeek - file-not-found - possibly an embedding 888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # situation - see if we can locate it in sys.exec_prefix 889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix), 890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "w9xpopen.exe") 891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not os.path.exists(w9xpopen): 892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError("Cannot locate w9xpopen.exe, which is " 893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "needed for Popen to work with your " 894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep "shell or platform.") 895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return w9xpopen 896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _execute_child(self, args, executable, preexec_fn, close_fds, 899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd, env, universal_newlines, 900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo, creationflags, shell, 901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite, 902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite): 904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Execute program (MS Windows version)""" 905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not isinstance(args, types.StringTypes): 907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = list2cmdline(args) 908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Process startup details 910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if startupinfo is None: 911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo = STARTUPINFO() 912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if None not in (p2cread, c2pwrite, errwrite): 913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.dwFlags |= _subprocess.STARTF_USESTDHANDLES 914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.hStdInput = p2cread 915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.hStdOutput = c2pwrite 916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.hStdError = errwrite 917edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 918edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if shell: 919edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW 920edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo.wShowWindow = _subprocess.SW_HIDE 921edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep comspec = os.environ.get("COMSPEC", "cmd.exe") 922edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = '{} /c "{}"'.format (comspec, args) 923edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if (_subprocess.GetVersion() >= 0x80000000 or 924edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.path.basename(comspec).lower() == "command.com"): 925edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Win9x, or using command.com on NT. We need to 926edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # use the w9xpopen intermediate program. For more 927edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # information, see KB Q150956 928edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp) 929edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep w9xpopen = self._find_w9xpopen() 930edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = '"%s" %s' % (w9xpopen, args) 931edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Not passing CREATE_NEW_CONSOLE has been known to 932edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # cause random failures on win9x. Specifically a 933edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # dialog: "Your program accessed mem currently in 934edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # use at xxx" and a hopeful warning about the 935edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # stability of your system. Cost is Ctrl+C wont 936edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # kill children. 937edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep creationflags |= _subprocess.CREATE_NEW_CONSOLE 938edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 939edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Start the process 940edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 941edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep hp, ht, pid, tid = _subprocess.CreateProcess(executable, args, 942edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # no special security 943edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep None, None, 944edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep int(not close_fds), 945edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep creationflags, 946edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep env, 947edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd, 948edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo) 949edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except pywintypes.error, e: 950edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Translate pywintypes.error to WindowsError, which is 951edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # a subclass of OSError. FIXME: We should really 952edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # translate errno using _sys_errlist (or similar), but 953edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # how can this be done from Python? 954edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise WindowsError(*e.args) 955edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep finally: 956edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Child is launched. Close the parent's copy of those pipe 957edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # handles that only the child should have open. You need 958edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # to make sure that no handles to the write end of the 959edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # output pipe are maintained in this process or else the 960edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # pipe will not close when the child process exits and the 961edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ReadFile will hang. 962edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cread is not None: 963edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread.Close() 964edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pwrite is not None: 965edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite.Close() 966edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errwrite is not None: 967edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite.Close() 968edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 969edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Retain the process handle, but close the thread handle 970edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._child_created = True 971edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._handle = hp 972edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.pid = pid 973edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ht.Close() 974edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 975edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _internal_poll(self, _deadstate=None, 976edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _WaitForSingleObject=_subprocess.WaitForSingleObject, 977edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _WAIT_OBJECT_0=_subprocess.WAIT_OBJECT_0, 978edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _GetExitCodeProcess=_subprocess.GetExitCodeProcess): 979edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Check if child process has terminated. Returns returncode 980edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute. 981edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 982edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep This method is called by __del__, so it can only refer to objects 983edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep in its local scope. 984edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 985edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 986edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.returncode is None: 987edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0: 988edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = _GetExitCodeProcess(self._handle) 989edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.returncode 990edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 991edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 992edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def wait(self): 993edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Wait for child process to terminate. Returns returncode 994edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute.""" 995edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.returncode is None: 996edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _subprocess.WaitForSingleObject(self._handle, 997edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _subprocess.INFINITE) 998edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = _subprocess.GetExitCodeProcess(self._handle) 999edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.returncode 1000edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1001edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1002edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _readerthread(self, fh, buffer): 1003edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep buffer.append(fh.read()) 1004edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1005edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1006edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _communicate(self, input): 1007edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = None # Return 1008edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = None # Return 1009edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1010edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdout: 1011edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = [] 1012edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout_thread = threading.Thread(target=self._readerthread, 1013edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args=(self.stdout, stdout)) 1014edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout_thread.setDaemon(True) 1015edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout_thread.start() 1016edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stderr: 1017edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = [] 1018edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr_thread = threading.Thread(target=self._readerthread, 1019edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args=(self.stderr, stderr)) 1020edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr_thread.setDaemon(True) 1021edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr_thread.start() 1022edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1023edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin: 1024edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if input is not None: 1025edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1026edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.write(input) 1027edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except IOError as e: 1028edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno != errno.EPIPE: 1029edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1030edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.close() 1031edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1032edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdout: 1033edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout_thread.join() 1034edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stderr: 1035edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr_thread.join() 1036edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1037edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # All data exchanged. Translate lists into strings. 1038edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout is not None: 1039edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = stdout[0] 1040edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr is not None: 1041edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = stderr[0] 1042edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1043edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Translate newlines, if requested. We cannot let the file 1044edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # object do the translation: It is based on stdio, which is 1045edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # impossible to combine with select (unless forcing no 1046edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # buffering). 1047edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.universal_newlines and hasattr(file, 'newlines'): 1048edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout: 1049edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = self._translate_newlines(stdout) 1050edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr: 1051edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = self._translate_newlines(stderr) 1052edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1053edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.wait() 1054edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (stdout, stderr) 1055edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1056edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def send_signal(self, sig): 1057edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Send a signal to the process 1058edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1059edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if sig == signal.SIGTERM: 1060edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.terminate() 1061edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif sig == signal.CTRL_C_EVENT: 1062edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.kill(self.pid, signal.CTRL_C_EVENT) 1063edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif sig == signal.CTRL_BREAK_EVENT: 1064edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.kill(self.pid, signal.CTRL_BREAK_EVENT) 1065edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1066edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise ValueError("Unsupported signal: {}".format(sig)) 1067edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1068edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def terminate(self): 1069edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Terminates the process 1070edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1071edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1072edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _subprocess.TerminateProcess(self._handle, 1) 1073edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError as e: 1074edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # ERROR_ACCESS_DENIED (winerror 5) is received when the 1075edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # process already died. 1076edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.winerror != 5: 1077edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1078edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep rc = _subprocess.GetExitCodeProcess(self._handle) 1079edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if rc == _subprocess.STILL_ACTIVE: 1080edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1081edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = rc 1082edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1083edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep kill = terminate 1084edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1085edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1086edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1087edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # POSIX methods 1088edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1089edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _get_handles(self, stdin, stdout, stderr): 1090edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Construct and return tuple with IO objects: 1091edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite 1092edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1093edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite = None, None 1094edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite = None, None 1095edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite = None, None 1096edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1097edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdin is None: 1098edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 1099edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stdin == PIPE: 1100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite = self.pipe_cloexec() 1101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stdin, int): 1102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = stdin 1103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 1105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread = stdin.fileno() 1106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout is None: 1108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 1109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stdout == PIPE: 1110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite = self.pipe_cloexec() 1111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stdout, int): 1112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = stdout 1113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 1115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = stdout.fileno() 1116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr is None: 1118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 1119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stderr == PIPE: 1120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite = self.pipe_cloexec() 1121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif stderr == STDOUT: 1122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = c2pwrite 1123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif isinstance(stderr, int): 1124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = stderr 1125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Assuming file-like object 1127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = stderr.fileno() 1128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (p2cread, p2cwrite, 1130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 1131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite) 1132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _set_cloexec_flag(self, fd, cloexec=True): 1135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cloexec_flag = fcntl.FD_CLOEXEC 1137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except AttributeError: 1138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cloexec_flag = 1 1139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep old = fcntl.fcntl(fd, fcntl.F_GETFD) 1141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if cloexec: 1142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag) 1143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fcntl.fcntl(fd, fcntl.F_SETFD, old & ~cloexec_flag) 1145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def pipe_cloexec(self): 1148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Create a pipe with FDs set CLOEXEC.""" 1149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Pipes' FDs are set CLOEXEC by default because we don't want them 1150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # to be inherited by other subprocesses: the CLOEXEC flag is removed 1151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # from the child's FDs by _dup2(), between fork() and exec(). 1152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This is not atomic: we would need the pipe2() syscall for that. 1153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep r, w = os.pipe() 1154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._set_cloexec_flag(r) 1155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._set_cloexec_flag(w) 1156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return r, w 1157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _close_fds(self, but): 1160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if hasattr(os, 'closerange'): 1161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.closerange(3, but) 1162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.closerange(but + 1, MAXFD) 1163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in xrange(3, MAXFD): 1165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if i == but: 1166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 1167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(i) 1169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except: 1170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pass 1171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _execute_child(self, args, executable, preexec_fn, close_fds, 1174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep cwd, env, universal_newlines, 1175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startupinfo, creationflags, shell, 1176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2cread, p2cwrite, 1177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pread, c2pwrite, 1178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errread, errwrite): 1179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Execute program (POSIX version)""" 1180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if isinstance(args, types.StringTypes): 1182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = [args] 1183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = list(args) 1185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if shell: 1187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args = ["/bin/sh", "-c"] + args 1188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if executable: 1189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep args[0] = executable 1190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if executable is None: 1192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep executable = args[0] 1193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # For transferring possible exec failure from child to parent 1195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # The first char specifies the exception type: 0 means 1196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # OSError, 1 means some other error. 1197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errpipe_read, errpipe_write = self.pipe_cloexec() 1198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep gc_was_enabled = gc.isenabled() 1201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Disable gc to avoid bug where gc -> file_dealloc -> 1202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # write to stderr -> hang. http://bugs.python.org/issue1336 1203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep gc.disable() 1204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.pid = os.fork() 1206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except: 1207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if gc_was_enabled: 1208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep gc.enable() 1209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._child_created = True 1211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.pid == 0: 1212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Child 1213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Close parent's pipe ends 1215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cwrite is not None: 1216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(p2cwrite) 1217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pread is not None: 1218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(c2pread) 1219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errread is not None: 1220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(errread) 1221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(errpipe_read) 1222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # When duping fds, if there arises a situation 1224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # where one of the fds is either 0, 1 or 2, it 1225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # is possible that it is overwritten (#12607). 1226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pwrite == 0: 1227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep c2pwrite = os.dup(c2pwrite) 1228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errwrite == 0 or errwrite == 1: 1229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errwrite = os.dup(errwrite) 1230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Dup fds for child 1232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _dup2(a, b): 1233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # dup2() removes the CLOEXEC flag but 1234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # we must do it ourselves if dup2() 1235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # would be a no-op (issue #10806). 1236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if a == b: 1237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._set_cloexec_flag(a, False) 1238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif a is not None: 1239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.dup2(a, b) 1240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _dup2(p2cread, 0) 1241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _dup2(c2pwrite, 1) 1242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _dup2(errwrite, 2) 1243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Close pipe fds. Make sure we don't close the 1245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # same fd more than once, or standard fds. 1246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep closed = { None } 1247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for fd in [p2cread, c2pwrite, errwrite]: 1248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if fd not in closed and fd > 2: 1249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(fd) 1250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep closed.add(fd) 1251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Close all other fds, if asked for 1253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if close_fds: 1254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._close_fds(but=errpipe_write) 1255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if cwd is not None: 1257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.chdir(cwd) 1258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if preexec_fn: 1260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep preexec_fn() 1261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if env is None: 1263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.execvp(executable, args) 1264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.execvpe(executable, args, env) 1266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except: 1268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exc_type, exc_value, tb = sys.exc_info() 1269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Save the traceback and attach it to the exception object 1270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exc_lines = traceback.format_exception(exc_type, 1271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exc_value, 1272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep tb) 1273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep exc_value.child_traceback = ''.join(exc_lines) 1274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.write(errpipe_write, pickle.dumps(exc_value)) 1275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This exitcode won't be reported to applications, so it 1277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # really doesn't matter what we return. 1278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os._exit(255) 1279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Parent 1281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if gc_was_enabled: 1282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep gc.enable() 1283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep finally: 1284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # be sure the FD is closed no matter what 1285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(errpipe_write) 1286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if p2cread is not None and p2cwrite is not None: 1288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(p2cread) 1289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if c2pwrite is not None and c2pread is not None: 1290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(c2pwrite) 1291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if errwrite is not None and errread is not None: 1292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(errwrite) 1293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Wait for exec to fail or succeed; possibly raising exception 1295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Exception limited to 1M 1296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = _eintr_retry_call(os.read, errpipe_read, 1048576) 1297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep finally: 1298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # be sure the FD is closed no matter what 1299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.close(errpipe_read) 1300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if data != "": 1302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _eintr_retry_call(os.waitpid, self.pid, 0) 1304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError as e: 1305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno != errno.ECHILD: 1306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep child_exception = pickle.loads(data) 1308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise child_exception 1309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _handle_exitstatus(self, sts, _WIFSIGNALED=os.WIFSIGNALED, 1312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED, 1313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _WEXITSTATUS=os.WEXITSTATUS): 1314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This method is called (indirectly) by __del__, so it cannot 1315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # refer to anything outside of its local scope.""" 1316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if _WIFSIGNALED(sts): 1317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = -_WTERMSIG(sts) 1318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif _WIFEXITED(sts): 1319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = _WEXITSTATUS(sts) 1320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Should never happen 1322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise RuntimeError("Unknown child exit status!") 1323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid, 1326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _WNOHANG=os.WNOHANG, _os_error=os.error, _ECHILD=errno.ECHILD): 1327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Check if child process has terminated. Returns returncode 1328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute. 1329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep This method is called by __del__, so it cannot reference anything 1331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep outside of the local scope (nor can any methods it calls). 1332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.returncode is None: 1335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pid, sts = _waitpid(self.pid, _WNOHANG) 1337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if pid == self.pid: 1338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._handle_exitstatus(sts) 1339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except _os_error as e: 1340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if _deadstate is not None: 1341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = _deadstate 1342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno == _ECHILD: 1343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This happens if SIGCLD is set to be ignored or 1344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # waiting for child processes has otherwise been 1345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # disabled for our process. This child is dead, we 1346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # can't get the status. 1347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # http://bugs.python.org/issue15756 1348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.returncode = 0 1349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.returncode 1350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def wait(self): 1353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Wait for child process to terminate. Returns returncode 1354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep attribute.""" 1355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while self.returncode is None: 1356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0) 1358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError as e: 1359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno != errno.ECHILD: 1360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # This happens if SIGCLD is set to be ignored or waiting 1362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # for child processes has otherwise been disabled for our 1363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # process. This child is dead, we can't get the status. 1364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep pid = self.pid 1365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep sts = 0 1366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Check the pid and loop as waitpid has been known to return 1367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 0 even without WNOHANG in odd situations. issue14396. 1368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if pid == self.pid: 1369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self._handle_exitstatus(sts) 1370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return self.returncode 1371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _communicate(self, input): 1374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin: 1375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Flush stdio buffer. This might block, if the user has 1376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # been writing to .stdin in an uncontrolled fashion. 1377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.flush() 1378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not input: 1379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.close() 1380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if _has_poll: 1382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout, stderr = self._communicate_with_poll(input) 1383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout, stderr = self._communicate_with_select(input) 1385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # All data exchanged. Translate lists into strings. 1387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout is not None: 1388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = ''.join(stdout) 1389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr is not None: 1390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = ''.join(stderr) 1391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Translate newlines, if requested. We cannot let the file 1393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # object do the translation: It is based on stdio, which is 1394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # impossible to combine with select (unless forcing no 1395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # buffering). 1396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.universal_newlines and hasattr(file, 'newlines'): 1397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stdout: 1398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = self._translate_newlines(stdout) 1399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if stderr: 1400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = self._translate_newlines(stderr) 1401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.wait() 1403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (stdout, stderr) 1404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _communicate_with_poll(self, input): 1407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = None # Return 1408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = None # Return 1409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2file = {} 1410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2output = {} 1411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep poller = select.poll() 1413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def register_and_append(file_obj, eventmask): 1414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep poller.register(file_obj.fileno(), eventmask) 1415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2file[file_obj.fileno()] = file_obj 1416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def close_unregister_and_remove(fd): 1418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep poller.unregister(fd) 1419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2file[fd].close() 1420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2file.pop(fd) 1421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin and input: 1423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep register_and_append(self.stdin, select.POLLOUT) 1424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI 1426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdout: 1427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep register_and_append(self.stdout, select_POLLIN_POLLPRI) 1428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2output[self.stdout.fileno()] = stdout = [] 1429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stderr: 1430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep register_and_append(self.stderr, select_POLLIN_POLLPRI) 1431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2output[self.stderr.fileno()] = stderr = [] 1432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep input_offset = 0 1434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while fd2file: 1435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ready = poller.poll() 1437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except select.error, e: 1438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.args[0] == errno.EINTR: 1439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 1440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for fd, mode in ready: 1443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mode & select.POLLOUT: 1444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep chunk = input[input_offset : input_offset + _PIPE_BUF] 1445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep input_offset += os.write(fd, chunk) 1447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError as e: 1448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno == errno.EPIPE: 1449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep close_unregister_and_remove(fd) 1450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if input_offset >= len(input): 1454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep close_unregister_and_remove(fd) 1455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep elif mode & select_POLLIN_POLLPRI: 1456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = os.read(fd, 4096) 1457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if not data: 1458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep close_unregister_and_remove(fd) 1459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep fd2output[fd].append(data) 1460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Ignore hang up or errors. 1462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep close_unregister_and_remove(fd) 1463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (stdout, stderr) 1465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def _communicate_with_select(self, input): 1468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep read_set = [] 1469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep write_set = [] 1470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = None # Return 1471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = None # Return 1472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin and input: 1474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep write_set.append(self.stdin) 1475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdout: 1476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep read_set.append(self.stdout) 1477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout = [] 1478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stderr: 1479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep read_set.append(self.stderr) 1480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr = [] 1481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep input_offset = 0 1483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep while read_set or write_set: 1484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep rlist, wlist, xlist = select.select(read_set, write_set, []) 1486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except select.error, e: 1487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.args[0] == errno.EINTR: 1488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep continue 1489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdin in wlist: 1492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep chunk = input[input_offset : input_offset + _PIPE_BUF] 1493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep bytes_written = os.write(self.stdin.fileno(), chunk) 1495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError as e: 1496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno == errno.EPIPE: 1497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.close() 1498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep write_set.remove(self.stdin) 1499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep raise 1501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep input_offset += bytes_written 1503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if input_offset >= len(input): 1504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdin.close() 1505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep write_set.remove(self.stdin) 1506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stdout in rlist: 1508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = os.read(self.stdout.fileno(), 1024) 1509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if data == "": 1510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stdout.close() 1511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep read_set.remove(self.stdout) 1512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stdout.append(data) 1513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if self.stderr in rlist: 1515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep data = os.read(self.stderr.fileno(), 1024) 1516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if data == "": 1517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.stderr.close() 1518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep read_set.remove(self.stderr) 1519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep stderr.append(data) 1520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep return (stdout, stderr) 1522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def send_signal(self, sig): 1525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Send a signal to the process 1526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep os.kill(self.pid, sig) 1528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def terminate(self): 1530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Terminate the process with SIGTERM 1531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.send_signal(signal.SIGTERM) 1533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def kill(self): 1535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """Kill the process with SIGKILL 1536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep """ 1537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.send_signal(signal.SIGKILL) 1538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _demo_posix(): 1541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 1: Simple redirection: Get process list 1543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep plist = Popen(["ps"], stdout=PIPE).communicate()[0] 1545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Process list:" 1546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print plist 1547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 2: Change uid before executing child 1550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if os.getuid() == 0: 1552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p = Popen(["id"], preexec_fn=lambda: os.setuid(100)) 1553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p.wait() 1554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 3: Connecting several subprocesses 1557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Looking for 'hda'..." 1559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p1 = Popen(["dmesg"], stdout=PIPE) 1560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) 1561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print repr(p2.communicate()[0]) 1562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 4: Catch execution error 1565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print 1567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Trying a weird file..." 1568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 1569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print Popen(["/this/path/does/not/exist"]).communicate() 1570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except OSError, e: 1571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if e.errno == errno.ENOENT: 1572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "The file didn't exist. I thought so..." 1573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Child traceback:" 1574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print e.child_traceback 1575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Error", e.errno 1577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print >>sys.stderr, "Gosh. No error." 1579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef _demo_windows(): 1582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 1: Connecting several subprocesses 1584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Looking for 'PROMPT' in set output..." 1586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p1 = Popen("set", stdout=PIPE, shell=True) 1587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE) 1588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print repr(p2.communicate()[0]) 1589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # Example 2: Simple execution of program 1592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep # 1593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print "Executing calc..." 1594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p = Popen("calc") 1595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep p.wait() 1596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 1598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 1599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if mswindows: 1600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _demo_windows() 1601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 1602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep _demo_posix() 1603