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 |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 375 | from win32con import DUPLICATE_SAME_ACCESS |
| 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, \ |
| 379 | 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 | |
| 676 | if shell: |
| 677 | comspec = os.environ.get("COMSPEC", "cmd.exe") |
| 678 | args = comspec + " /c " + args |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 679 | if (GetVersion() >= 0x80000000L or |
| 680 | os.path.basename(comspec).lower() == "command.com"): |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 681 | # Win9x, or using command.com on NT. We need to |
| 682 | # use the w9xpopen intermediate program. For more |
| 683 | # information, see KB Q150956 |
| 684 | # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/Q150/9/56.asp) |
| 685 | w9xpopen = self._find_w9xpopen() |
| 686 | args = '"%s" %s' % (w9xpopen, args) |
| 687 | # Not passing CREATE_NEW_CONSOLE has been known to |
| 688 | # cause random failures on win9x. Specifically a |
| 689 | # dialog: "Your program accessed mem currently in |
| 690 | # use at xxx" and a hopeful warning about the |
| 691 | # stability of your system. Cost is Ctrl+C wont |
| 692 | # kill children. |
| 693 | creationflags |= CREATE_NEW_CONSOLE |
| 694 | |
| 695 | # Process startup details |
| 696 | if startupinfo == None: |
| 697 | startupinfo = STARTUPINFO() |
| 698 | if not None in (p2cread, c2pwrite, errwrite): |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 699 | startupinfo.dwFlags |= STARTF_USESTDHANDLES |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 700 | startupinfo.hStdInput = p2cread |
| 701 | startupinfo.hStdOutput = c2pwrite |
| 702 | startupinfo.hStdError = errwrite |
| 703 | |
| 704 | # Start the process |
| 705 | try: |
| 706 | hp, ht, pid, tid = CreateProcess(executable, args, |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 707 | # no special security |
| 708 | None, None, |
| 709 | # must inherit handles to pass std |
| 710 | # handles |
| 711 | 1, |
| 712 | creationflags, |
| 713 | env, |
| 714 | cwd, |
| 715 | startupinfo) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 716 | except pywintypes.error, e: |
| 717 | # Translate pywintypes.error to WindowsError, which is |
| 718 | # a subclass of OSError. FIXME: We should really |
| 719 | # translate errno using _sys_errlist (or simliar), but |
| 720 | # how can this be done from Python? |
| 721 | raise WindowsError(*e.args) |
| 722 | |
| 723 | # Retain the process handle, but close the thread handle |
| 724 | self._handle = hp |
| 725 | self.pid = pid |
| 726 | ht.Close() |
| 727 | |
Andrew M. Kuchling | 51ee66e | 2004-10-12 16:38:42 +0000 | [diff] [blame] | 728 | # Child is launched. Close the parent's copy of those pipe |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 729 | # handles that only the child should have open. You need |
| 730 | # to make sure that no handles to the write end of the |
| 731 | # output pipe are maintained in this process or else the |
| 732 | # pipe will not close when the child process exits and the |
| 733 | # ReadFile will hang. |
| 734 | if p2cread != None: |
| 735 | p2cread.Close() |
| 736 | if c2pwrite != None: |
| 737 | c2pwrite.Close() |
| 738 | if errwrite != None: |
| 739 | errwrite.Close() |
| 740 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 741 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 742 | def poll(self): |
| 743 | """Check if child process has terminated. Returns returncode |
| 744 | attribute.""" |
| 745 | if self.returncode == None: |
| 746 | if WaitForSingleObject(self._handle, 0) == WAIT_OBJECT_0: |
| 747 | self.returncode = GetExitCodeProcess(self._handle) |
| 748 | _active.remove(self) |
| 749 | return self.returncode |
| 750 | |
| 751 | |
| 752 | def wait(self): |
| 753 | """Wait for child process to terminate. Returns returncode |
| 754 | attribute.""" |
| 755 | if self.returncode == None: |
| 756 | obj = WaitForSingleObject(self._handle, INFINITE) |
| 757 | self.returncode = GetExitCodeProcess(self._handle) |
| 758 | _active.remove(self) |
| 759 | return self.returncode |
| 760 | |
| 761 | |
| 762 | def _readerthread(self, fh, buffer): |
| 763 | buffer.append(fh.read()) |
| 764 | |
| 765 | |
| 766 | def communicate(self, input=None): |
| 767 | """Interact with process: Send data to stdin. Read data from |
| 768 | stdout and stderr, until end-of-file is reached. Wait for |
| 769 | process to terminate. The optional input argument should be a |
| 770 | string to be sent to the child process, or None, if no data |
| 771 | should be sent to the child. |
| 772 | |
| 773 | communicate() returns a tuple (stdout, stderr).""" |
| 774 | stdout = None # Return |
| 775 | stderr = None # Return |
| 776 | |
| 777 | if self.stdout: |
| 778 | stdout = [] |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 779 | stdout_thread = threading.Thread(target=self._readerthread, |
| 780 | args=(self.stdout, stdout)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 781 | stdout_thread.setDaemon(True) |
| 782 | stdout_thread.start() |
| 783 | if self.stderr: |
| 784 | stderr = [] |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 785 | stderr_thread = threading.Thread(target=self._readerthread, |
| 786 | args=(self.stderr, stderr)) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 787 | stderr_thread.setDaemon(True) |
| 788 | stderr_thread.start() |
| 789 | |
| 790 | if self.stdin: |
| 791 | if input != None: |
| 792 | self.stdin.write(input) |
| 793 | self.stdin.close() |
| 794 | |
| 795 | if self.stdout: |
| 796 | stdout_thread.join() |
| 797 | if self.stderr: |
| 798 | stderr_thread.join() |
| 799 | |
| 800 | # All data exchanged. Translate lists into strings. |
| 801 | if stdout != None: |
| 802 | stdout = stdout[0] |
| 803 | if stderr != None: |
| 804 | stderr = stderr[0] |
| 805 | |
| 806 | # Translate newlines, if requested. We cannot let the file |
| 807 | # object do the translation: It is based on stdio, which is |
| 808 | # impossible to combine with select (unless forcing no |
| 809 | # buffering). |
| 810 | if self.universal_newlines and hasattr(open, 'newlines'): |
| 811 | if stdout: |
| 812 | stdout = self._translate_newlines(stdout) |
| 813 | if stderr: |
| 814 | stderr = self._translate_newlines(stderr) |
| 815 | |
| 816 | self.wait() |
| 817 | return (stdout, stderr) |
| 818 | |
| 819 | else: |
| 820 | # |
| 821 | # POSIX methods |
| 822 | # |
| 823 | def _get_handles(self, stdin, stdout, stderr): |
| 824 | """Construct and return tupel with IO objects: |
| 825 | p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite |
| 826 | """ |
| 827 | p2cread, p2cwrite = None, None |
| 828 | c2pread, c2pwrite = None, None |
| 829 | errread, errwrite = None, None |
| 830 | |
| 831 | if stdin == None: |
| 832 | pass |
| 833 | elif stdin == PIPE: |
| 834 | p2cread, p2cwrite = os.pipe() |
| 835 | elif type(stdin) == types.IntType: |
| 836 | p2cread = stdin |
| 837 | else: |
| 838 | # Assuming file-like object |
| 839 | p2cread = stdin.fileno() |
| 840 | |
| 841 | if stdout == None: |
| 842 | pass |
| 843 | elif stdout == PIPE: |
| 844 | c2pread, c2pwrite = os.pipe() |
| 845 | elif type(stdout) == types.IntType: |
| 846 | c2pwrite = stdout |
| 847 | else: |
| 848 | # Assuming file-like object |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 849 | c2pwrite = stdout.fileno() |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 850 | |
| 851 | if stderr == None: |
| 852 | pass |
| 853 | elif stderr == PIPE: |
| 854 | errread, errwrite = os.pipe() |
| 855 | elif stderr == STDOUT: |
| 856 | errwrite = c2pwrite |
| 857 | elif type(stderr) == types.IntType: |
| 858 | errwrite = stderr |
| 859 | else: |
| 860 | # Assuming file-like object |
| 861 | errwrite = stderr.fileno() |
| 862 | |
| 863 | return (p2cread, p2cwrite, |
| 864 | c2pread, c2pwrite, |
| 865 | errread, errwrite) |
| 866 | |
| 867 | |
| 868 | def _set_cloexec_flag(self, fd): |
| 869 | try: |
| 870 | cloexec_flag = fcntl.FD_CLOEXEC |
| 871 | except AttributeError: |
| 872 | cloexec_flag = 1 |
| 873 | |
| 874 | old = fcntl.fcntl(fd, fcntl.F_GETFD) |
| 875 | fcntl.fcntl(fd, fcntl.F_SETFD, old | cloexec_flag) |
| 876 | |
| 877 | |
| 878 | def _close_fds(self, but): |
| 879 | for i in range(3, MAXFD): |
| 880 | if i == but: |
| 881 | continue |
| 882 | try: |
| 883 | os.close(i) |
| 884 | except: |
| 885 | pass |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 886 | |
| 887 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 888 | def _execute_child(self, args, executable, preexec_fn, close_fds, |
| 889 | cwd, env, universal_newlines, |
| 890 | startupinfo, creationflags, shell, |
| 891 | p2cread, p2cwrite, |
| 892 | c2pread, c2pwrite, |
| 893 | errread, errwrite): |
| 894 | """Execute program (POSIX version)""" |
| 895 | |
| 896 | if isinstance(args, types.StringTypes): |
| 897 | args = [args] |
| 898 | |
| 899 | if shell: |
| 900 | args = ["/bin/sh", "-c"] + args |
| 901 | |
| 902 | if executable == None: |
| 903 | executable = args[0] |
| 904 | |
| 905 | # For transferring possible exec failure from child to parent |
| 906 | # The first char specifies the exception type: 0 means |
| 907 | # OSError, 1 means some other error. |
| 908 | errpipe_read, errpipe_write = os.pipe() |
| 909 | self._set_cloexec_flag(errpipe_write) |
| 910 | |
| 911 | self.pid = os.fork() |
| 912 | if self.pid == 0: |
| 913 | # Child |
| 914 | try: |
| 915 | # Close parent's pipe ends |
| 916 | if p2cwrite: |
| 917 | os.close(p2cwrite) |
| 918 | if c2pread: |
| 919 | os.close(c2pread) |
| 920 | if errread: |
| 921 | os.close(errread) |
| 922 | os.close(errpipe_read) |
| 923 | |
| 924 | # Dup fds for child |
| 925 | if p2cread: |
| 926 | os.dup2(p2cread, 0) |
| 927 | if c2pwrite: |
| 928 | os.dup2(c2pwrite, 1) |
| 929 | if errwrite: |
| 930 | os.dup2(errwrite, 2) |
| 931 | |
| 932 | # Close pipe fds. Make sure we doesn't close the same |
| 933 | # fd more than once. |
| 934 | if p2cread: |
| 935 | os.close(p2cread) |
| 936 | if c2pwrite and c2pwrite not in (p2cread,): |
| 937 | os.close(c2pwrite) |
| 938 | if errwrite and errwrite not in (p2cread, c2pwrite): |
| 939 | os.close(errwrite) |
| 940 | |
| 941 | # Close all other fds, if asked for |
| 942 | if close_fds: |
| 943 | self._close_fds(but=errpipe_write) |
| 944 | |
| 945 | if cwd != None: |
| 946 | os.chdir(cwd) |
| 947 | |
| 948 | if preexec_fn: |
| 949 | apply(preexec_fn) |
| 950 | |
| 951 | if env == None: |
| 952 | os.execvp(executable, args) |
| 953 | else: |
| 954 | os.execvpe(executable, args, env) |
| 955 | |
| 956 | except: |
| 957 | exc_type, exc_value, tb = sys.exc_info() |
| 958 | # Save the traceback and attach it to the exception object |
Tim Peters | e8374a5 | 2004-10-13 03:15:00 +0000 | [diff] [blame] | 959 | exc_lines = traceback.format_exception(exc_type, |
| 960 | exc_value, |
| 961 | tb) |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 962 | exc_value.child_traceback = ''.join(exc_lines) |
| 963 | os.write(errpipe_write, pickle.dumps(exc_value)) |
| 964 | |
| 965 | # This exitcode won't be reported to applications, so it |
| 966 | # really doesn't matter what we return. |
| 967 | os._exit(255) |
| 968 | |
| 969 | # Parent |
| 970 | os.close(errpipe_write) |
| 971 | if p2cread and p2cwrite: |
| 972 | os.close(p2cread) |
| 973 | if c2pwrite and c2pread: |
| 974 | os.close(c2pwrite) |
| 975 | if errwrite and errread: |
| 976 | os.close(errwrite) |
| 977 | |
| 978 | # Wait for exec to fail or succeed; possibly raising exception |
| 979 | data = os.read(errpipe_read, 1048576) # Exceptions limited to 1 MB |
| 980 | os.close(errpipe_read) |
| 981 | if data != "": |
| 982 | child_exception = pickle.loads(data) |
| 983 | raise child_exception |
| 984 | |
| 985 | |
| 986 | def _handle_exitstatus(self, sts): |
| 987 | if os.WIFSIGNALED(sts): |
| 988 | self.returncode = -os.WTERMSIG(sts) |
| 989 | elif os.WIFEXITED(sts): |
| 990 | self.returncode = os.WEXITSTATUS(sts) |
| 991 | else: |
| 992 | # Should never happen |
| 993 | raise RuntimeError("Unknown child exit status!") |
| 994 | |
| 995 | _active.remove(self) |
| 996 | |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 997 | |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 998 | def poll(self): |
| 999 | """Check if child process has terminated. Returns returncode |
| 1000 | attribute.""" |
| 1001 | if self.returncode == None: |
| 1002 | try: |
| 1003 | pid, sts = os.waitpid(self.pid, os.WNOHANG) |
| 1004 | if pid == self.pid: |
| 1005 | self._handle_exitstatus(sts) |
| 1006 | except os.error: |
| 1007 | pass |
| 1008 | return self.returncode |
| 1009 | |
| 1010 | |
| 1011 | def wait(self): |
| 1012 | """Wait for child process to terminate. Returns returncode |
| 1013 | attribute.""" |
| 1014 | if self.returncode == None: |
| 1015 | pid, sts = os.waitpid(self.pid, 0) |
| 1016 | self._handle_exitstatus(sts) |
| 1017 | return self.returncode |
| 1018 | |
| 1019 | |
| 1020 | def communicate(self, input=None): |
| 1021 | """Interact with process: Send data to stdin. Read data from |
| 1022 | stdout and stderr, until end-of-file is reached. Wait for |
| 1023 | process to terminate. The optional input argument should be a |
| 1024 | string to be sent to the child process, or None, if no data |
| 1025 | should be sent to the child. |
| 1026 | |
| 1027 | communicate() returns a tuple (stdout, stderr).""" |
| 1028 | read_set = [] |
| 1029 | write_set = [] |
| 1030 | stdout = None # Return |
| 1031 | stderr = None # Return |
| 1032 | |
| 1033 | if self.stdin: |
| 1034 | # Flush stdio buffer. This might block, if the user has |
| 1035 | # been writing to .stdin in an uncontrolled fashion. |
| 1036 | self.stdin.flush() |
| 1037 | if input: |
| 1038 | write_set.append(self.stdin) |
| 1039 | else: |
| 1040 | self.stdin.close() |
| 1041 | if self.stdout: |
| 1042 | read_set.append(self.stdout) |
Tim Peters | e718f61 | 2004-10-12 21:51:32 +0000 | [diff] [blame] | 1043 | stdout = [] |
Fredrik Lundh | 5b3687d | 2004-10-12 15:26:28 +0000 | [diff] [blame] | 1044 | if self.stderr: |
| 1045 | read_set.append(self.stderr) |
| 1046 | stderr = [] |
| 1047 | |
| 1048 | while read_set or write_set: |
| 1049 | rlist, wlist, xlist = select.select(read_set, write_set, []) |
| 1050 | |
| 1051 | if self.stdin in wlist: |
| 1052 | # When select has indicated that the file is writable, |
| 1053 | # we can write up to PIPE_BUF bytes without risk |
| 1054 | # blocking. POSIX defines PIPE_BUF >= 512 |
| 1055 | bytes_written = os.write(self.stdin.fileno(), input[:512]) |
| 1056 | input = input[bytes_written:] |
| 1057 | if not input: |
| 1058 | self.stdin.close() |
| 1059 | write_set.remove(self.stdin) |
| 1060 | |
| 1061 | if self.stdout in rlist: |
| 1062 | data = os.read(self.stdout.fileno(), 1024) |
| 1063 | if data == "": |
| 1064 | self.stdout.close() |
| 1065 | read_set.remove(self.stdout) |
| 1066 | stdout.append(data) |
| 1067 | |
| 1068 | if self.stderr in rlist: |
| 1069 | data = os.read(self.stderr.fileno(), 1024) |
| 1070 | if data == "": |
| 1071 | self.stderr.close() |
| 1072 | read_set.remove(self.stderr) |
| 1073 | stderr.append(data) |
| 1074 | |
| 1075 | # All data exchanged. Translate lists into strings. |
| 1076 | if stdout != None: |
| 1077 | stdout = ''.join(stdout) |
| 1078 | if stderr != None: |
| 1079 | stderr = ''.join(stderr) |
| 1080 | |
| 1081 | # Translate newlines, if requested. We cannot let the file |
| 1082 | # object do the translation: It is based on stdio, which is |
| 1083 | # impossible to combine with select (unless forcing no |
| 1084 | # buffering). |
| 1085 | if self.universal_newlines and hasattr(open, 'newlines'): |
| 1086 | if stdout: |
| 1087 | stdout = self._translate_newlines(stdout) |
| 1088 | if stderr: |
| 1089 | stderr = self._translate_newlines(stderr) |
| 1090 | |
| 1091 | self.wait() |
| 1092 | return (stdout, stderr) |
| 1093 | |
| 1094 | |
| 1095 | def _demo_posix(): |
| 1096 | # |
| 1097 | # Example 1: Simple redirection: Get process list |
| 1098 | # |
| 1099 | plist = Popen(["ps"], stdout=PIPE).communicate()[0] |
| 1100 | print "Process list:" |
| 1101 | print plist |
| 1102 | |
| 1103 | # |
| 1104 | # Example 2: Change uid before executing child |
| 1105 | # |
| 1106 | if os.getuid() == 0: |
| 1107 | p = Popen(["id"], preexec_fn=lambda: os.setuid(100)) |
| 1108 | p.wait() |
| 1109 | |
| 1110 | # |
| 1111 | # Example 3: Connecting several subprocesses |
| 1112 | # |
| 1113 | print "Looking for 'hda'..." |
| 1114 | p1 = Popen(["dmesg"], stdout=PIPE) |
| 1115 | p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) |
| 1116 | print repr(p2.communicate()[0]) |
| 1117 | |
| 1118 | # |
| 1119 | # Example 4: Catch execution error |
| 1120 | # |
| 1121 | print |
| 1122 | print "Trying a weird file..." |
| 1123 | try: |
| 1124 | print Popen(["/this/path/does/not/exist"]).communicate() |
| 1125 | except OSError, e: |
| 1126 | if e.errno == errno.ENOENT: |
| 1127 | print "The file didn't exist. I thought so..." |
| 1128 | print "Child traceback:" |
| 1129 | print e.child_traceback |
| 1130 | else: |
| 1131 | print "Error", e.errno |
| 1132 | else: |
| 1133 | print >>sys.stderr, "Gosh. No error." |
| 1134 | |
| 1135 | |
| 1136 | def _demo_windows(): |
| 1137 | # |
| 1138 | # Example 1: Connecting several subprocesses |
| 1139 | # |
| 1140 | print "Looking for 'PROMPT' in set output..." |
| 1141 | p1 = Popen("set", stdout=PIPE, shell=True) |
| 1142 | p2 = Popen('find "PROMPT"', stdin=p1.stdout, stdout=PIPE) |
| 1143 | print repr(p2.communicate()[0]) |
| 1144 | |
| 1145 | # |
| 1146 | # Example 2: Simple execution of program |
| 1147 | # |
| 1148 | print "Executing calc..." |
| 1149 | p = Popen("calc") |
| 1150 | p.wait() |
| 1151 | |
| 1152 | |
| 1153 | if __name__ == "__main__": |
| 1154 | if mswindows: |
| 1155 | _demo_windows() |
| 1156 | else: |
| 1157 | _demo_posix() |