source: python/vendor/Python-2.7.6/Lib/subprocess.py

Last change on this file was 388, checked in by dmik, 11 years ago

python: Update vendor to 2.7.6.

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