Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1 | # subprocess - Subprocesses with accessible I/O streams |
| 2 | # |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 3 | # For more information about this module, see PEP 324. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 4 | # |
| 5 | # Copyright (c) 2003-2004 by Peter Astrand <astrand@lysator.liu.se> |
| 6 | # |
| 7 | # By obtaining, using, and/or copying this software and/or its |
| 8 | # associated documentation, you agree that you have read, understood, |
| 9 | # and will comply with the following terms and conditions: |
| 10 | # |
| 11 | # Permission to use, copy, modify, and distribute this software and |
| 12 | # its associated documentation for any purpose and without fee is |
| 13 | # hereby granted, provided that the above copyright notice appears in |
| 14 | # all copies, and that both that copyright notice and this permission |
| 15 | # notice appear in supporting documentation, and that the name of the |
| 16 | # author not be used in advertising or publicity pertaining to |
| 17 | # distribution of the software without specific, written prior |
| 18 | # permission. |
| 19 | # |
| 20 | # THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 21 | # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. |
| 22 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 23 | # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
| 24 | # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 25 | # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 26 | # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 27 | |
Raymond Hettinger | 837dd93 | 2004-10-17 16:36:53 +0000 | [diff] [blame] | 28 | r"""subprocess - Subprocesses with accessible I/O streams |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 29 | |
Fredrik Lundh | 15aaacc | 2004-10-17 14:47:05 +0000 | [diff] [blame] | 30 | This module allows you to spawn processes, connect to their |
| 31 | input/output/error pipes, and obtain their return codes. This module |
| 32 | intends to replace several other, older modules and functions, like: |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 33 | |
| 34 | os.system |
| 35 | os.spawn* |
| 36 | os.popen* |
| 37 | popen2.* |
| 38 | commands.* |
| 39 | |
| 40 | Information about how the subprocess module can be used to replace these |
| 41 | modules and functions can be found below. |
| 42 | |
| 43 | |
| 44 | |
| 45 | Using the subprocess module |
| 46 | =========================== |
| 47 | This module defines one class called Popen: |
| 48 | |
| 49 | class Popen(args, bufsize=0, executable=None, |
| 50 | stdin=None, stdout=None, stderr=None, |
| 51 | preexec_fn=None, close_fds=False, shell=False, |
| 52 | cwd=None, env=None, universal_newlines=False, |
| 53 | startupinfo=None, creationflags=0): |
| 54 | |
| 55 | |
| 56 | Arguments are: |
| 57 | |
| 58 | args should be a string, or a sequence of program arguments. The |
| 59 | program to execute is normally the first item in the args sequence or |
| 60 | string, but can be explicitly set by using the executable argument. |
| 61 | |
| 62 | On UNIX, with shell=False (default): In this case, the Popen class |
| 63 | uses os.execvp() to execute the child program. args should normally |
| 64 | be a sequence. A string will be treated as a sequence with the string |
| 65 | as the only item (the program to execute). |
| 66 | |
| 67 | On UNIX, with shell=True: If args is a string, it specifies the |
| 68 | command string to execute through the shell. If args is a sequence, |
| 69 | the first item specifies the command string, and any additional items |
| 70 | will be treated as additional shell arguments. |
| 71 | |
| 72 | On Windows: the Popen class uses CreateProcess() to execute the child |
| 73 | program, which operates on strings. If args is a sequence, it will be |
| 74 | converted to a string using the list2cmdline method. Please note that |
| 75 | not all MS Windows applications interpret the command line the same |
| 76 | way: The list2cmdline is designed for applications using the same |
| 77 | rules as the MS C runtime. |
| 78 | |
| 79 | bufsize, if given, has the same meaning as the corresponding argument |
| 80 | to the built-in open() function: 0 means unbuffered, 1 means line |
| 81 | buffered, any other positive value means use a buffer of |
| 82 | (approximately) that size. A negative bufsize means to use the system |
| 83 | default, which usually means fully buffered. The default value for |
| 84 | bufsize is 0 (unbuffered). |
| 85 | |
| 86 | stdin, stdout and stderr specify the executed programs' standard |
| 87 | input, standard output and standard error file handles, respectively. |
| 88 | Valid values are PIPE, an existing file descriptor (a positive |
| 89 | integer), an existing file object, and None. PIPE indicates that a |
| 90 | new pipe to the child should be created. With None, no redirection |
| 91 | will occur; the child's file handles will be inherited from the |
| 92 | parent. Additionally, stderr can be STDOUT, which indicates that the |
| 93 | stderr data from the applications should be captured into the same |
| 94 | file handle as for stdout. |
| 95 | |
| 96 | If preexec_fn is set to a callable object, this object will be called |
| 97 | in the child process just before the child is executed. |
| 98 | |
| 99 | If close_fds is true, all file descriptors except 0, 1 and 2 will be |
| 100 | closed before the child process is executed. |
| 101 | |
| 102 | if shell is true, the specified command will be executed through the |
| 103 | shell. |
| 104 | |
| 105 | If cwd is not None, the current directory will be changed to cwd |
| 106 | before the child is executed. |
| 107 | |
| 108 | If env is not None, it defines the environment variables for the new |
| 109 | process. |
| 110 | |
| 111 | If universal_newlines is true, the file objects stdout and stderr are |
| 112 | opened as a text files, but lines may be terminated by any of '\n', |
| 113 | the Unix end-of-line convention, '\r', the Macintosh convention or |
| 114 | '\r\n', the Windows convention. All of these external representations |
| 115 | are seen as '\n' by the Python program. Note: This feature is only |
| 116 | available if Python is built with universal newline support (the |
| 117 | default). Also, the newlines attribute of the file objects stdout, |
| 118 | stdin and stderr are not updated by the communicate() method. |
| 119 | |
| 120 | The startupinfo and creationflags, if given, will be passed to the |
| 121 | underlying CreateProcess() function. They can specify things such as |
| 122 | appearance of the main window and priority for the new process. |
| 123 | (Windows only) |
| 124 | |
| 125 | |
| 126 | This module also defines two shortcut functions: |
| 127 | |
| 128 | call(*args, **kwargs): |
| 129 | Run command with arguments. Wait for command to complete, then |
| 130 | return the returncode attribute. |
| 131 | |
| 132 | The arguments are the same as for the Popen constructor. Example: |
| 133 | |
| 134 | retcode = call(["ls", "-l"]) |
| 135 | |
| 136 | |
| 137 | Exceptions |
| 138 | ---------- |
| 139 | Exceptions raised in the child process, before the new program has |
| 140 | started to execute, will be re-raised in the parent. Additionally, |
| 141 | the exception object will have one extra attribute called |
| 142 | 'child_traceback', which is a string containing traceback information |
| 143 | from the childs point of view. |
| 144 | |
| 145 | The most common exception raised is OSError. This occurs, for |
| 146 | example, when trying to execute a non-existent file. Applications |
| 147 | should prepare for OSErrors. |
| 148 | |
| 149 | A ValueError will be raised if Popen is called with invalid arguments. |
| 150 | |
| 151 | |
| 152 | Security |
| 153 | -------- |
| 154 | Unlike some other popen functions, this implementation will never call |
| 155 | /bin/sh implicitly. This means that all characters, including shell |
| 156 | metacharacters, can safely be passed to child processes. |
| 157 | |
| 158 | |
| 159 | Popen objects |
| 160 | ============= |
| 161 | Instances of the Popen class have the following methods: |
| 162 | |
| 163 | poll() |
| 164 | Check if child process has terminated. Returns returncode |
| 165 | attribute. |
| 166 | |
| 167 | wait() |
| 168 | Wait for child process to terminate. Returns returncode attribute. |
| 169 | |
| 170 | communicate(input=None) |
| 171 | Interact with process: Send data to stdin. Read data from stdout |
| 172 | and stderr, until end-of-file is reached. Wait for process to |
| 173 | terminate. The optional stdin argument should be a string to be |
| 174 | sent to the child process, or None, if no data should be sent to |
| 175 | the child. |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 176 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 177 | communicate() returns a tuple (stdout, stderr). |
| 178 | |
| 179 | Note: The data read is buffered in memory, so do not use this |
| 180 | method if the data size is large or unlimited. |
| 181 | |
| 182 | The following attributes are also available: |
| 183 | |
| 184 | stdin |
| 185 | If the stdin argument is PIPE, this attribute is a file object |
| 186 | that provides input to the child process. Otherwise, it is None. |
| 187 | |
| 188 | stdout |
| 189 | If the stdout argument is PIPE, this attribute is a file object |
| 190 | that provides output from the child process. Otherwise, it is |
| 191 | None. |
| 192 | |
| 193 | stderr |
| 194 | If the stderr argument is PIPE, this attribute is file object that |
| 195 | provides error output from the child process. Otherwise, it is |
| 196 | None. |
| 197 | |
| 198 | pid |
| 199 | The process ID of the child process. |
| 200 | |
| 201 | returncode |
| 202 | The child return code. A None value indicates that the process |
| 203 | hasn't terminated yet. A negative value -N indicates that the |
| 204 | child was terminated by signal N (UNIX only). |
| 205 | |
| 206 | |
| 207 | Replacing older functions with the subprocess module |
| 208 | ==================================================== |
| 209 | In this section, "a ==> b" means that b can be used as a replacement |
| 210 | for a. |
| 211 | |
| 212 | Note: All functions in this section fail (more or less) silently if |
| 213 | the executed program cannot be found; this module raises an OSError |
| 214 | exception. |
| 215 | |
| 216 | In the following examples, we assume that the subprocess module is |
| 217 | imported with "from subprocess import *". |
| 218 | |
| 219 | |
| 220 | Replacing /bin/sh shell backquote |
| 221 | --------------------------------- |
| 222 | output=`mycmd myarg` |
| 223 | ==> |
| 224 | output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] |
| 225 | |
| 226 | |
| 227 | Replacing shell pipe line |
| 228 | ------------------------- |
| 229 | output=`dmesg | grep hda` |
| 230 | ==> |
| 231 | p1 = Popen(["dmesg"], stdout=PIPE) |
| 232 | p2 = Popen(["grep", "hda"], stdin=p1.stdout) |
| 233 | output = p2.communicate()[0] |
| 234 | |
| 235 | |
| 236 | Replacing os.system() |
| 237 | --------------------- |
| 238 | sts = os.system("mycmd" + " myarg") |
| 239 | ==> |
| 240 | p = Popen("mycmd" + " myarg", shell=True) |
| 241 | sts = os.waitpid(p.pid, 0) |
| 242 | |
| 243 | Note: |
| 244 | |
| 245 | * Calling the program through the shell is usually not required. |
| 246 | |
| 247 | * It's easier to look at the returncode attribute than the |
| 248 | exitstatus. |
| 249 | |
| 250 | A more real-world example would look like this: |
| 251 | |
| 252 | try: |
| 253 | retcode = call("mycmd" + " myarg", shell=True) |
| 254 | if retcode < 0: |
| 255 | print >>sys.stderr, "Child was terminated by signal", -retcode |
| 256 | else: |
| 257 | print >>sys.stderr, "Child returned", retcode |
| 258 | except OSError, e: |
| 259 | print >>sys.stderr, "Execution failed:", e |
| 260 | |
| 261 | |
| 262 | Replacing os.spawn* |
| 263 | ------------------- |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 264 | P_NOWAIT example: |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 265 | |
| 266 | pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg") |
| 267 | ==> |
| 268 | pid = Popen(["/bin/mycmd", "myarg"]).pid |
| 269 | |
| 270 | |
| 271 | P_WAIT example: |
| 272 | |
| 273 | retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg") |
| 274 | ==> |
| 275 | retcode = call(["/bin/mycmd", "myarg"]) |
| 276 | |
| 277 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 278 | Vector example: |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 279 | |
| 280 | os.spawnvp(os.P_NOWAIT, path, args) |
| 281 | ==> |
| 282 | Popen([path] + args[1:]) |
| 283 | |
| 284 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 285 | Environment example: |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 286 | |
| 287 | os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) |
| 288 | ==> |
| 289 | Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"}) |
| 290 | |
| 291 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 292 | Replacing os.popen* |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 293 | ------------------- |
| 294 | pipe = os.popen(cmd, mode='r', bufsize) |
| 295 | ==> |
| 296 | pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout |
| 297 | |
| 298 | pipe = os.popen(cmd, mode='w', bufsize) |
| 299 | ==> |
| 300 | pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin |
| 301 | |
| 302 | |
| 303 | (child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize) |
| 304 | ==> |
| 305 | p = Popen(cmd, shell=True, bufsize=bufsize, |
| 306 | stdin=PIPE, stdout=PIPE, close_fds=True) |
| 307 | (child_stdin, child_stdout) = (p.stdin, p.stdout) |
| 308 | |
| 309 | |
| 310 | (child_stdin, |
| 311 | child_stdout, |
| 312 | child_stderr) = os.popen3(cmd, mode, bufsize) |
| 313 | ==> |
| 314 | p = Popen(cmd, shell=True, bufsize=bufsize, |
| 315 | stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) |
| 316 | (child_stdin, |
| 317 | child_stdout, |
| 318 | child_stderr) = (p.stdin, p.stdout, p.stderr) |
| 319 | |
| 320 | |
| 321 | (child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize) |
| 322 | ==> |
| 323 | p = Popen(cmd, shell=True, bufsize=bufsize, |
| 324 | stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) |
| 325 | (child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout) |
| 326 | |
| 327 | |
| 328 | Replacing popen2.* |
| 329 | ------------------ |
| 330 | Note: If the cmd argument to popen2 functions is a string, the command |
| 331 | is executed through /bin/sh. If it is a list, the command is directly |
| 332 | executed. |
| 333 | |
| 334 | (child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode) |
| 335 | ==> |
| 336 | p = Popen(["somestring"], shell=True, bufsize=bufsize |
| 337 | stdin=PIPE, stdout=PIPE, close_fds=True) |
| 338 | (child_stdout, child_stdin) = (p.stdout, p.stdin) |
| 339 | |
| 340 | |
| 341 | (child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode) |
| 342 | ==> |
| 343 | p = Popen(["mycmd", "myarg"], bufsize=bufsize, |
| 344 | stdin=PIPE, stdout=PIPE, close_fds=True) |
| 345 | (child_stdout, child_stdin) = (p.stdout, p.stdin) |
| 346 | |
| 347 | The popen2.Popen3 and popen3.Popen4 basically works as subprocess.Popen, |
| 348 | except that: |
| 349 | |
| 350 | * subprocess.Popen raises an exception if the execution fails |
| 351 | * the capturestderr argument is replaced with the stderr argument. |
| 352 | * stdin=PIPE and stdout=PIPE must be specified. |
| 353 | * popen2 closes all filedescriptors by default, but you have to specify |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 354 | close_fds=True with subprocess.Popen. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 355 | |
| 356 | |
| 357 | """ |
| 358 | |
| 359 | import sys |
| 360 | mswindows = (sys.platform == "win32") |
| 361 | |
| 362 | import os |
| 363 | import types |
| 364 | import traceback |
| 365 | |
| 366 | if mswindows: |
| 367 | import threading |
| 368 | import msvcrt |
Fredrik Lundh | 3e73a01 | 2004-10-13 18:19:18 +0000 | [diff] [blame] | 369 | if 0: # <-- change this to use pywin32 instead of the _subprocess driver |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 370 | import pywintypes |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 371 | from win32api import GetStdHandle, STD_INPUT_HANDLE, \ |
| 372 | STD_OUTPUT_HANDLE, STD_ERROR_HANDLE |
| 373 | from win32api import GetCurrentProcess, DuplicateHandle, \ |
| 374 | GetModuleFileName, GetVersion |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 375 | from win32con import DUPLICATE_SAME_ACCESS, SW_HIDE |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 376 | from win32pipe import CreatePipe |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 377 | from win32process import CreateProcess, STARTUPINFO, \ |
| 378 | GetExitCodeProcess, STARTF_USESTDHANDLES, \ |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 379 | STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 380 | from win32event import WaitForSingleObject, INFINITE, WAIT_OBJECT_0 |
Fredrik Lundh | 3e73a01 | 2004-10-13 18:19:18 +0000 | [diff] [blame] | 381 | else: |
| 382 | from _subprocess import * |
| 383 | class STARTUPINFO: |
| 384 | dwFlags = 0 |
| 385 | hStdInput = None |
| 386 | hStdOutput = None |
| 387 | hStdError = None |
| 388 | class pywintypes: |
| 389 | error = IOError |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 390 | else: |
| 391 | import select |
| 392 | import errno |
| 393 | import fcntl |
| 394 | import pickle |
| 395 | |
| 396 | __all__ = ["Popen", "PIPE", "STDOUT", "call"] |
| 397 | |
| 398 | try: |
| 399 | MAXFD = os.sysconf("SC_OPEN_MAX") |
| 400 | except: |
| 401 | MAXFD = 256 |
| 402 | |
| 403 | # True/False does not exist on 2.2.0 |
| 404 | try: |
| 405 | False |
| 406 | except NameError: |
| 407 | False = 0 |
| 408 | True = 1 |
| 409 | |
| 410 | _active = [] |
| 411 | |
| 412 | def _cleanup(): |
| 413 | for inst in _active[:]: |
| 414 | inst.poll() |
| 415 | |
| 416 | PIPE = -1 |
| 417 | STDOUT = -2 |
| 418 | |
| 419 | |
| 420 | def call(*args, **kwargs): |
| 421 | """Run command with arguments. Wait for command to complete, then |
| 422 | return the returncode attribute. |
| 423 | |
| 424 | The arguments are the same as for the Popen constructor. Example: |
| 425 | |
| 426 | retcode = call(["ls", "-l"]) |
| 427 | """ |
| 428 | return Popen(*args, **kwargs).wait() |
| 429 | |
| 430 | |
| 431 | def list2cmdline(seq): |
| 432 | """ |
| 433 | Translate a sequence of arguments into a command line |
| 434 | string, using the same rules as the MS C runtime: |
| 435 | |
| 436 | 1) Arguments are delimited by white space, which is either a |
| 437 | space or a tab. |
| 438 | |
| 439 | 2) A string surrounded by double quotation marks is |
| 440 | interpreted as a single argument, regardless of white space |
| 441 | contained within. A quoted string can be embedded in an |
| 442 | argument. |
| 443 | |
| 444 | 3) A double quotation mark preceded by a backslash is |
| 445 | interpreted as a literal double quotation mark. |
| 446 | |
| 447 | 4) Backslashes are interpreted literally, unless they |
| 448 | immediately precede a double quotation mark. |
| 449 | |
| 450 | 5) If backslashes immediately precede a double quotation mark, |
| 451 | every pair of backslashes is interpreted as a literal |
| 452 | backslash. If the number of backslashes is odd, the last |
| 453 | backslash escapes the next double quotation mark as |
| 454 | described in rule 3. |
| 455 | """ |
| 456 | |
| 457 | # See |
| 458 | # http://msdn.microsoft.com/library/en-us/vccelng/htm/progs_12.asp |
| 459 | result = [] |
| 460 | needquote = False |
| 461 | for arg in seq: |
| 462 | bs_buf = [] |
| 463 | |
| 464 | # Add a space to separate this argument from the others |
| 465 | if result: |
| 466 | result.append(' ') |
| 467 | |
| 468 | needquote = (" " in arg) or ("\t" in arg) |
| 469 | if needquote: |
| 470 | result.append('"') |
| 471 | |
| 472 | for c in arg: |
| 473 | if c == '\\': |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 474 | # Don't know if we need to double yet. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 475 | bs_buf.append(c) |
| 476 | elif c == '"': |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 477 | # Double backspaces. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 478 | result.append('\\' * len(bs_buf)*2) |
| 479 | bs_buf = [] |
| 480 | result.append('\\"') |
| 481 | else: |
| 482 | # Normal char |
| 483 | if bs_buf: |
| 484 | result.extend(bs_buf) |
| 485 | bs_buf = [] |
| 486 | result.append(c) |
| 487 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 488 | # Add remaining backspaces, if any. |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 489 | if bs_buf: |
| 490 | result.extend(bs_buf) |
| 491 | |
| 492 | if needquote: |
| 493 | result.append('"') |
| 494 | |
| 495 | return ''.join(result) |
| 496 | |
| 497 | |
| 498 | class Popen(object): |
| 499 | def __init__(self, args, bufsize=0, executable=None, |
| 500 | stdin=None, stdout=None, stderr=None, |
| 501 | preexec_fn=None, close_fds=False, shell=False, |
| 502 | cwd=None, env=None, universal_newlines=False, |
| 503 | startupinfo=None, creationflags=0): |
| 504 | """Create new Popen instance.""" |
| 505 | _cleanup() |
| 506 | |
| 507 | if mswindows: |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 508 | if preexec_fn is not None: |
| 509 | raise ValueError("preexec_fn is not supported on Windows " |
| 510 | "platforms") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 511 | if close_fds: |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 512 | raise ValueError("close_fds is not supported on Windows " |
| 513 | "platforms") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 514 | else: |
| 515 | # POSIX |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 516 | if startupinfo is not None: |
| 517 | raise ValueError("startupinfo is only supported on Windows " |
| 518 | "platforms") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 519 | if creationflags != 0: |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 520 | raise ValueError("creationflags is only supported on Windows " |
| 521 | "platforms") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 522 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 523 | self.stdin = None |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 524 | self.stdout = None |
| 525 | self.stderr = None |
| 526 | self.pid = None |
| 527 | self.returncode = None |
| 528 | self.universal_newlines = universal_newlines |
| 529 | |
| 530 | # Input and output objects. The general principle is like |
| 531 | # this: |
| 532 | # |
| 533 | # Parent Child |
| 534 | # ------ ----- |
| 535 | # p2cwrite ---stdin---> p2cread |
| 536 | # c2pread <--stdout--- c2pwrite |
| 537 | # errread <--stderr--- errwrite |
| 538 | # |
| 539 | # On POSIX, the child objects are file descriptors. On |
| 540 | # Windows, these are Windows file handles. The parent objects |
| 541 | # are file descriptors on both platforms. The parent objects |
| 542 | # are None when not using PIPEs. The child objects are None |
| 543 | # when not redirecting. |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 544 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 545 | (p2cread, p2cwrite, |
| 546 | c2pread, c2pwrite, |
| 547 | errread, errwrite) = self._get_handles(stdin, stdout, stderr) |
| 548 | |
| 549 | self._execute_child(args, executable, preexec_fn, close_fds, |
| 550 | cwd, env, universal_newlines, |
| 551 | startupinfo, creationflags, shell, |
| 552 | p2cread, p2cwrite, |
| 553 | c2pread, c2pwrite, |
| 554 | errread, errwrite) |
| 555 | |
| 556 | if p2cwrite: |
| 557 | self.stdin = os.fdopen(p2cwrite, 'wb', bufsize) |
| 558 | if c2pread: |
| 559 | if universal_newlines: |
| 560 | self.stdout = os.fdopen(c2pread, 'rU', bufsize) |
| 561 | else: |
| 562 | self.stdout = os.fdopen(c2pread, 'rb', bufsize) |
| 563 | if errread: |
| 564 | if universal_newlines: |
| 565 | self.stderr = os.fdopen(errread, 'rU', bufsize) |
| 566 | else: |
| 567 | self.stderr = os.fdopen(errread, 'rb', bufsize) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 568 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 569 | _active.append(self) |
| 570 | |
| 571 | |
| 572 | def _translate_newlines(self, data): |
| 573 | data = data.replace("\r\n", "\n") |
| 574 | data = data.replace("\r", "\n") |
| 575 | return data |
| 576 | |
| 577 | |
| 578 | if mswindows: |
| 579 | # |
| 580 | # Windows methods |
| 581 | # |
| 582 | def _get_handles(self, stdin, stdout, stderr): |
| 583 | """Construct and return tupel with IO objects: |
| 584 | p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite |
| 585 | """ |
| 586 | if stdin == None and stdout == None and stderr == None: |
| 587 | return (None, None, None, None, None, None) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 588 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 589 | p2cread, p2cwrite = None, None |
| 590 | c2pread, c2pwrite = None, None |
| 591 | errread, errwrite = None, None |
| 592 | |
| 593 | if stdin == None: |
| 594 | p2cread = GetStdHandle(STD_INPUT_HANDLE) |
| 595 | elif stdin == PIPE: |
| 596 | p2cread, p2cwrite = CreatePipe(None, 0) |
| 597 | # Detach and turn into fd |
| 598 | p2cwrite = p2cwrite.Detach() |
| 599 | p2cwrite = msvcrt.open_osfhandle(p2cwrite, 0) |
| 600 | elif type(stdin) == types.IntType: |
| 601 | p2cread = msvcrt.get_osfhandle(stdin) |
| 602 | else: |
| 603 | # Assuming file-like object |
| 604 | p2cread = msvcrt.get_osfhandle(stdin.fileno()) |
| 605 | p2cread = self._make_inheritable(p2cread) |
| 606 | |
| 607 | if stdout == None: |
| 608 | c2pwrite = GetStdHandle(STD_OUTPUT_HANDLE) |
| 609 | elif stdout == PIPE: |
| 610 | c2pread, c2pwrite = CreatePipe(None, 0) |
| 611 | # Detach and turn into fd |
| 612 | c2pread = c2pread.Detach() |
| 613 | c2pread = msvcrt.open_osfhandle(c2pread, 0) |
| 614 | elif type(stdout) == types.IntType: |
| 615 | c2pwrite = msvcrt.get_osfhandle(stdout) |
| 616 | else: |
| 617 | # Assuming file-like object |
| 618 | c2pwrite = msvcrt.get_osfhandle(stdout.fileno()) |
| 619 | c2pwrite = self._make_inheritable(c2pwrite) |
| 620 | |
| 621 | if stderr == None: |
| 622 | errwrite = GetStdHandle(STD_ERROR_HANDLE) |
| 623 | elif stderr == PIPE: |
| 624 | errread, errwrite = CreatePipe(None, 0) |
| 625 | # Detach and turn into fd |
| 626 | errread = errread.Detach() |
| 627 | errread = msvcrt.open_osfhandle(errread, 0) |
| 628 | elif stderr == STDOUT: |
| 629 | errwrite = c2pwrite |
| 630 | elif type(stderr) == types.IntType: |
| 631 | errwrite = msvcrt.get_osfhandle(stderr) |
| 632 | else: |
| 633 | # Assuming file-like object |
| 634 | errwrite = msvcrt.get_osfhandle(stderr.fileno()) |
| 635 | errwrite = self._make_inheritable(errwrite) |
| 636 | |
| 637 | return (p2cread, p2cwrite, |
| 638 | c2pread, c2pwrite, |
| 639 | errread, errwrite) |
| 640 | |
| 641 | |
| 642 | def _make_inheritable(self, handle): |
| 643 | """Return a duplicate of handle, which is inheritable""" |
| 644 | return DuplicateHandle(GetCurrentProcess(), handle, |
| 645 | GetCurrentProcess(), 0, 1, |
| 646 | DUPLICATE_SAME_ACCESS) |
| 647 | |
| 648 | |
| 649 | def _find_w9xpopen(self): |
| 650 | """Find and return absolut path to w9xpopen.exe""" |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 651 | w9xpopen = os.path.join(os.path.dirname(GetModuleFileName(0)), |
| 652 | "w9xpopen.exe") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 653 | if not os.path.exists(w9xpopen): |
| 654 | # Eeek - file-not-found - possibly an embedding |
| 655 | # situation - see if we can locate it in sys.exec_prefix |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 656 | w9xpopen = os.path.join(os.path.dirname(sys.exec_prefix), |
| 657 | "w9xpopen.exe") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 658 | if not os.path.exists(w9xpopen): |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 659 | raise RuntimeError("Cannot locate w9xpopen.exe, which is " |
| 660 | "needed for Popen to work with your " |
| 661 | "shell or platform.") |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 662 | return w9xpopen |
| 663 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 664 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 665 | def _execute_child(self, args, executable, preexec_fn, close_fds, |
| 666 | cwd, env, universal_newlines, |
| 667 | startupinfo, creationflags, shell, |
| 668 | p2cread, p2cwrite, |
| 669 | c2pread, c2pwrite, |
| 670 | errread, errwrite): |
| 671 | """Execute program (MS Windows version)""" |
| 672 | |
| 673 | if not isinstance(args, types.StringTypes): |
| 674 | args = list2cmdline(args) |
| 675 | |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 676 | # Process startup details |
| 677 | default_startupinfo = STARTUPINFO() |
| 678 | if startupinfo == None: |
| 679 | startupinfo = default_startupinfo |
| 680 | if not None in (p2cread, c2pwrite, errwrite): |
| 681 | startupinfo.dwFlags |= STARTF_USESTDHANDLES |
| 682 | startupinfo.hStdInput = p2cread |
| 683 | startupinfo.hStdOutput = c2pwrite |
| 684 | startupinfo.hStdError = errwrite |
| 685 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 686 | if shell: |
Peter Astrand | c1d6536 | 2004-11-07 14:30:34 +0000 | [diff] [blame] | 687 | default_startupinfo.dwFlags |= STARTF_USESHOWWINDOW |
| 688 | default_startupinfo.wShowWindow = SW_HIDE |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 689 | comspec = os.environ.get("COMSPEC", "cmd.exe") |
| 690 | args = comspec + " /c " + args |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 691 | if (GetVersion() >= 0x80000000L or |
| 692 | os.path.basename(comspec).lower() == "command.com"): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 693 | # Win9x, or using command.com on NT. We need to |
| 694 | # use the w9xpopen intermediate program. For more |
| 695 | # information, see KB Q150956 |
| 696 | # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp) |
| 697 | w9xpopen = self._find_w9xpopen() |
| 698 | args = '"%s" %s' % (w9xpopen, args) |
| 699 | # Not passing CREATE_NEW_CONSOLE has been known to |
| 700 | # cause random failures on win9x. Specifically a |
| 701 | # dialog: "Your program accessed mem currently in |
| 702 | # use at xxx" and a hopeful warning about the |
| 703 | # stability of your system. Cost is Ctrl+C wont |
| 704 | # kill children. |
| 705 | creationflags |= CREATE_NEW_CONSOLE |
| 706 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 707 | # Start the process |
| 708 | try: |
| 709 | hp, ht, pid, tid = CreateProcess(executable, args, |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 710 | # no special security |
| 711 | None, None, |
| 712 | # must inherit handles to pass std |
| 713 | # handles |
| 714 | 1, |
| 715 | creationflags, |
| 716 | env, |
| 717 | cwd, |
| 718 | startupinfo) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 719 | except pywintypes.error, e: |
| 720 | # Translate pywintypes.error to WindowsError, which is |
| 721 | # a subclass of OSError. FIXME: We should really |
| 722 | # translate errno using _sys_errlist (or simliar), but |
| 723 | # how can this be done from Python? |
| 724 | raise WindowsError(*e.args) |
| 725 | |
| 726 | # Retain the process handle, but close the thread handle |
| 727 | self._handle = hp |
| 728 | self.pid = pid |
| 729 | ht.Close() |
| 730 | |
Andrew M. Kuchling | 51ee66e | 2004-10-12 16:38:42 +0000 | [diff] [blame] | 731 | # Child is launched. Close the parent's copy of those pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 732 | # handles that only the child should have open. You need |
| 733 | # to make sure that no handles to the write end of the |
| 734 | # output pipe are maintained in this process or else the |
| 735 | # pipe will not close when the child process exits and the |
| 736 | # ReadFile will hang. |
| 737 | if p2cread != None: |
| 738 | p2cread.Close() |
| 739 | if c2pwrite != None: |
| 740 | c2pwrite.Close() |
| 741 | if errwrite != None: |
| 742 | errwrite.Close() |
| 743 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 744 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 745 | def poll(self): |
| 746 | """Check if child process has terminated. Returns returncode |
| 747 | attribute.""" |
| 748 | if self.returncode == None: |
| 749 | if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0: |
| 750 | self.returncode = GetExitCodeProcess(self._handle) |
| 751 | _active.remove(self) |
| 752 | return self.returncode |
| 753 | |
| 754 | |
| 755 | def wait(self): |
| 756 | """Wait for child process to terminate. Returns returncode |
| 757 | attribute.""" |
| 758 | if self.returncode == None: |
| 759 | obj = WaitForSingleObject(self._handle, INFINITE) |
| 760 | self.returncode = GetExitCodeProcess(self._handle) |
| 761 | _active.remove(self) |
| 762 | return self.returncode |
| 763 | |
| 764 | |
| 765 | def _readerthread(self, fh, buffer): |
| 766 | buffer.append(fh.read()) |
| 767 | |
| 768 | |
| 769 | def communicate(self, input=None): |
| 770 | """Interact with process: Send data to stdin. Read data from |
| 771 | stdout and stderr, until end-of-file is reached. Wait for |
| 772 | process to terminate. The optional input argument should be a |
| 773 | string to be sent to the child process, or None, if no data |
| 774 | should be sent to the child. |
| 775 | |
| 776 | communicate() returns a tuple (stdout, stderr).""" |
| 777 | stdout = None # Return |
| 778 | stderr = None # Return |
| 779 | |
| 780 | if self.stdout: |
| 781 | stdout = [] |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 782 | stdout_thread = threading.Thread(target=self._readerthread, |
| 783 | args=(self.stdout, stdout)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 784 | stdout_thread.setDaemon(True) |
| 785 | stdout_thread.start() |
| 786 | if self.stderr: |
| 787 | stderr = [] |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 788 | stderr_thread = threading.Thread(target=self._readerthread, |
| 789 | args=(self.stderr, stderr)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 790 | stderr_thread.setDaemon(True) |
| 791 | stderr_thread.start() |
| 792 | |
| 793 | if self.stdin: |
| 794 | if input != None: |
| 795 | self.stdin.write(input) |
| 796 | self.stdin.close() |
| 797 | |
| 798 | if self.stdout: |
| 799 | stdout_thread.join() |
| 800 | if self.stderr: |
| 801 | stderr_thread.join() |
| 802 | |
| 803 | # All data exchanged. Translate lists into strings. |
| 804 | if stdout != None: |
| 805 | stdout = stdout[0] |
| 806 | if stderr != None: |
| 807 | stderr = stderr[0] |
| 808 | |
| 809 | # Translate newlines, if requested. We cannot let the file |
| 810 | # object do the translation: It is based on stdio, which is |
| 811 | # impossible to combine with select (unless forcing no |
| 812 | # buffering). |
| 813 | if self.universal_newlines and hasattr(open, 'newlines'): |
| 814 | if stdout: |
| 815 | stdout = self._translate_newlines(stdout) |
| 816 | if stderr: |
| 817 | stderr = self._translate_newlines(stderr) |
| 818 | |
| 819 | self.wait() |
| 820 | return (stdout, stderr) |
| 821 | |
| 822 | else: |
| 823 | # |
| 824 | # POSIX methods |
| 825 | # |
| 826 | def _get_handles(self, stdin, stdout, stderr): |
| 827 | """Construct and return tupel with IO objects: |
| 828 | p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite |
| 829 | """ |
| 830 | p2cread, p2cwrite = None, None |
| 831 | c2pread, c2pwrite = None, None |
| 832 | errread, errwrite = None, None |
| 833 | |
| 834 | if stdin == None: |
| 835 | pass |
| 836 | elif stdin == PIPE: |
| 837 | p2cread, p2cwrite = os.pipe() |
| 838 | elif type(stdin) == types.IntType: |
| 839 | p2cread = stdin |
| 840 | else: |
| 841 | # Assuming file-like object |
| 842 | p2cread = stdin.fileno() |
| 843 | |
| 844 | if stdout == None: |
| 845 | pass |
| 846 | elif stdout == PIPE: |
| 847 | c2pread, c2pwrite = os.pipe() |
| 848 | elif type(stdout) == types.IntType: |
| 849 | c2pwrite = stdout |
| 850 | else: |
| 851 | # Assuming file-like object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 852 | c2pwrite = stdout.fileno() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 853 | |
| 854 | if stderr == None: |
| 855 | pass |
| 856 | elif stderr == PIPE: |
| 857 | errread, errwrite = os.pipe() |
| 858 | elif stderr == STDOUT: |
| 859 | errwrite = c2pwrite |
| 860 | elif type(stderr) == types.IntType: |
| 861 | errwrite = stderr |
| 862 | else: |
| 863 | # Assuming file-like object |
| 864 | errwrite = stderr.fileno() |
| 865 | |
| 866 | return (p2cread, p2cwrite, |
| 867 | c2pread, c2pwrite, |
| 868 | errread, errwrite) |
| 869 | |
| 870 | |
| 871 | def _set_cloexec_flag(self, fd): |
| 872 | try: |
| 873 | cloexec_flag = fcntl.FD_CLOEXEC |
| 874 | except AttributeError: |
| 875 | cloexec_flag = 1 |
| 876 | |
| 877 | old = fcntl.fcntl(fd, fcntl.F_GETFD) |
| 878 | fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag) |
| 879 | |
| 880 | |
| 881 | def _close_fds(self, but): |
| 882 | for i in range(3, MAXFD): |
| 883 | if i == but: |
| 884 | continue |
| 885 | try: |
| 886 | os.close(i) |
| 887 | except: |
| 888 | pass |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 889 | |
| 890 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 891 | def _execute_child(self, args, executable, preexec_fn, close_fds, |
| 892 | cwd, env, universal_newlines, |
| 893 | startupinfo, creationflags, shell, |
| 894 | p2cread, p2cwrite, |
| 895 | c2pread, c2pwrite, |
| 896 | errread, errwrite): |
| 897 | """Execute program (POSIX version)""" |
| 898 | |
| 899 | if isinstance(args, types.StringTypes): |
| 900 | args = [args] |
| 901 | |
| 902 | if shell: |
| 903 | args = ["/bin/sh", "-c"] + args |
| 904 | |
| 905 | if executable == None: |
| 906 | executable = args[0] |
| 907 | |
| 908 | # For transferring possible exec failure from child to parent |
| 909 | # The first char specifies the exception type: 0 means |
| 910 | # OSError, 1 means some other error. |
| 911 | errpipe_read, errpipe_write = os.pipe() |
| 912 | self._set_cloexec_flag(errpipe_write) |
| 913 | |
| 914 | self.pid = os.fork() |
| 915 | if self.pid == 0: |
| 916 | # Child |
| 917 | try: |
| 918 | # Close parent's pipe ends |
| 919 | if p2cwrite: |
| 920 | os.close(p2cwrite) |
| 921 | if c2pread: |
| 922 | os.close(c2pread) |
| 923 | if errread: |
| 924 | os.close(errread) |
| 925 | os.close(errpipe_read) |
| 926 | |
| 927 | # Dup fds for child |
| 928 | if p2cread: |
| 929 | os.dup2(p2cread, 0) |
| 930 | if c2pwrite: |
| 931 | os.dup2(c2pwrite, 1) |
| 932 | if errwrite: |
| 933 | os.dup2(errwrite, 2) |
| 934 | |
| 935 | # Close pipe fds. Make sure we doesn't close the same |
| 936 | # fd more than once. |
| 937 | if p2cread: |
| 938 | os.close(p2cread) |
| 939 | if c2pwrite and c2pwrite not in (p2cread,): |
| 940 | os.close(c2pwrite) |
| 941 | if errwrite and errwrite not in (p2cread, c2pwrite): |
| 942 | os.close(errwrite) |
| 943 | |
| 944 | # Close all other fds, if asked for |
| 945 | if close_fds: |
| 946 | self._close_fds(but=errpipe_write) |
| 947 | |
| 948 | if cwd != None: |
| 949 | os.chdir(cwd) |
| 950 | |
| 951 | if preexec_fn: |
| 952 | apply(preexec_fn) |
| 953 | |
| 954 | if env == None: |
| 955 | os.execvp(executable, args) |
| 956 | else: |
| 957 | os.execvpe(executable, args, env) |
| 958 | |
| 959 | except: |
| 960 | exc_type, exc_value, tb = sys.exc_info() |
| 961 | # Save the traceback and attach it to the exception object |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 962 | exc_lines = traceback.format_exception(exc_type, |
| 963 | exc_value, |
| 964 | tb) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 965 | exc_value.child_traceback = ''.join(exc_lines) |
| 966 | os.write(errpipe_write, pickle.dumps(exc_value)) |
| 967 | |
| 968 | # This exitcode won't be reported to applications, so it |
| 969 | # really doesn't matter what we return. |
| 970 | os._exit(255) |
| 971 | |
| 972 | # Parent |
| 973 | os.close(errpipe_write) |
| 974 | if p2cread and p2cwrite: |
| 975 | os.close(p2cread) |
| 976 | if c2pwrite and c2pread: |
| 977 | os.close(c2pwrite) |
| 978 | if errwrite and errread: |
| 979 | os.close(errwrite) |
| 980 | |
| 981 | # Wait for exec to fail or succeed; possibly raising exception |
| 982 | data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB |
| 983 | os.close(errpipe_read) |
| 984 | if data != "": |
| 985 | child_exception = pickle.loads(data) |
| 986 | raise child_exception |
| 987 | |
| 988 | |
| 989 | def _handle_exitstatus(self, sts): |
| 990 | if os.WIFSIGNALED(sts): |
| 991 | self.returncode = -os.WTERMSIG(sts) |
| 992 | elif os.WIFEXITED(sts): |
| 993 | self.returncode = os.WEXITSTATUS(sts) |
| 994 | else: |
| 995 | # Should never happen |
| 996 | raise RuntimeError("Unknown child exit status!") |
| 997 | |
| 998 | _active.remove(self) |
| 999 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1000 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1001 | def poll(self): |
| 1002 | """Check if child process has terminated. Returns returncode |
| 1003 | attribute.""" |
| 1004 | if self.returncode == None: |
| 1005 | try: |
| 1006 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
| 1007 | if pid == self.pid: |
| 1008 | self._handle_exitstatus(sts) |
| 1009 | except os.error: |
| 1010 | pass |
| 1011 | return self.returncode |
| 1012 | |
| 1013 | |
| 1014 | def wait(self): |
| 1015 | """Wait for child process to terminate. Returns returncode |
| 1016 | attribute.""" |
| 1017 | if self.returncode == None: |
| 1018 | pid, sts = os.waitpid(self.pid, 0) |
| 1019 | self._handle_exitstatus(sts) |
| 1020 | return self.returncode |
| 1021 | |
| 1022 | |
| 1023 | def communicate(self, input=None): |
| 1024 | """Interact with process: Send data to stdin. Read data from |
| 1025 | stdout and stderr, until end-of-file is reached. Wait for |
| 1026 | process to terminate. The optional input argument should be a |
| 1027 | string to be sent to the child process, or None, if no data |
| 1028 | should be sent to the child. |
| 1029 | |
| 1030 | communicate() returns a tuple (stdout, stderr).""" |
| 1031 | read_set = [] |
| 1032 | write_set = [] |
| 1033 | stdout = None # Return |
| 1034 | stderr = None # Return |
| 1035 | |
| 1036 | if self.stdin: |
| 1037 | # Flush stdio buffer. This might block, if the user has |
| 1038 | # been writing to .stdin in an uncontrolled fashion. |
| 1039 | self.stdin.flush() |
| 1040 | if input: |
| 1041 | write_set.append(self.stdin) |
| 1042 | else: |
| 1043 | self.stdin.close() |
| 1044 | if self.stdout: |
| 1045 | read_set.append(self.stdout) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1046 | stdout = [] |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1047 | if self.stderr: |
| 1048 | read_set.append(self.stderr) |
| 1049 | stderr = [] |
| 1050 | |
| 1051 | while read_set or write_set: |
| 1052 | rlist, wlist, xlist = select.select(read_set, write_set, []) |
| 1053 | |
| 1054 | if self.stdin in wlist: |
| 1055 | # When select has indicated that the file is writable, |
| 1056 | # we can write up to PIPE_BUF bytes without risk |
| 1057 | # blocking. POSIX defines PIPE_BUF >= 512 |
| 1058 | bytes_written = os.write(self.stdin.fileno(), input[:512]) |
| 1059 | input = input[bytes_written:] |
| 1060 | if not input: |
| 1061 | self.stdin.close() |
| 1062 | write_set.remove(self.stdin) |
| 1063 | |
| 1064 | if self.stdout in rlist: |
| 1065 | data = os.read(self.stdout.fileno(), 1024) |
| 1066 | if data == "": |
| 1067 | self.stdout.close() |
| 1068 | read_set.remove(self.stdout) |
| 1069 | stdout.append(data) |
| 1070 | |
| 1071 | if self.stderr in rlist: |
| 1072 | data = os.read(self.stderr.fileno(), 1024) |
| 1073 | if data == "": |
| 1074 | self.stderr.close() |
| 1075 | read_set.remove(self.stderr) |
| 1076 | stderr.append(data) |
| 1077 | |
| 1078 | # All data exchanged. Translate lists into strings. |
| 1079 | if stdout != None: |
| 1080 | stdout = ''.join(stdout) |
| 1081 | if stderr != None: |
| 1082 | stderr = ''.join(stderr) |
| 1083 | |
| 1084 | # Translate newlines, if requested. We cannot let the file |
| 1085 | # object do the translation: It is based on stdio, which is |
| 1086 | # impossible to combine with select (unless forcing no |
| 1087 | # buffering). |
| 1088 | if self.universal_newlines and hasattr(open, 'newlines'): |
| 1089 | if stdout: |
| 1090 | stdout = self._translate_newlines(stdout) |
| 1091 | if stderr: |
| 1092 | stderr = self._translate_newlines(stderr) |
| 1093 | |
| 1094 | self.wait() |
| 1095 | return (stdout, stderr) |
| 1096 | |
| 1097 | |
| 1098 | def _demo_posix(): |
| 1099 | # |
| 1100 | # Example 1: Simple redirection: Get process list |
| 1101 | # |
| 1102 | plist = Popen(["ps"], stdout=PIPE).communicate()[0] |
| 1103 | print "Process list:" |
| 1104 | print plist |
| 1105 | |
| 1106 | # |
| 1107 | # Example 2: Change uid before executing child |
| 1108 | # |
| 1109 | if os.getuid() == 0: |
| 1110 | p = Popen(["id"], preexec_fn=lambda: os.setuid(100)) |
| 1111 | p.wait() |
| 1112 | |
| 1113 | # |
| 1114 | # Example 3: Connecting several subprocesses |
| 1115 | # |
| 1116 | print "Looking for 'hda'..." |
| 1117 | p1 = Popen(["dmesg"], stdout=PIPE) |
| 1118 | p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) |
| 1119 | print repr(p2.communicate()[0]) |
| 1120 | |
| 1121 | # |
| 1122 | # Example 4: Catch execution error |
| 1123 | # |
| 1124 | print |
| 1125 | print "Trying a weird file..." |
| 1126 | try: |
| 1127 | print Popen(["/this/path/does/not/exist"]).communicate() |
| 1128 | except OSError, e: |
| 1129 | if e.errno == errno.ENOENT: |
| 1130 | print "The file didn't exist. I thought so..." |
| 1131 | print "Child traceback:" |
| 1132 | print e.child_traceback |
| 1133 | else: |
| 1134 | print "Error", e.errno |
| 1135 | else: |
| 1136 | print >>sys.stderr, "Gosh. No error." |
| 1137 | |
| 1138 | |
| 1139 | def _demo_windows(): |
| 1140 | # |
| 1141 | # Example 1: Connecting several subprocesses |
| 1142 | # |
| 1143 | print "Looking for 'PROMPT' in set output..." |
| 1144 | p1 = Popen("set", stdout=PIPE, shell=True) |
| 1145 | p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE) |
| 1146 | print repr(p2.communicate()[0]) |
| 1147 | |
| 1148 | # |
| 1149 | # Example 2: Simple execution of program |
| 1150 | # |
| 1151 | print "Executing calc..." |
| 1152 | p = Popen("calc") |
| 1153 | p.wait() |
| 1154 | |
| 1155 | |
| 1156 | if __name__ == "__main__": |
| 1157 | if mswindows: |
| 1158 | _demo_windows() |
| 1159 | else: |
| 1160 | _demo_posix() |