|
31 | 31 | __all__ = ['Popen', 'PIPE', 'STDOUT', 'check_output', 'CalledProcessError'] |
32 | 32 |
|
33 | 33 |
|
| 34 | + |
34 | 35 | if hasattr(subprocess, 'Popen'): |
35 | 36 | Popen = subprocess.Popen |
36 | 37 | # Assume that it also has the other constants. |
37 | 38 | PIPE = subprocess.PIPE |
38 | 39 | STDOUT = subprocess.STDOUT |
39 | 40 | CalledProcessError = subprocess.CalledProcessError |
| 41 | + check_output = subprocess.check_output |
40 | 42 | else: |
41 | 43 | # In restricted environments (such as Google App Engine), these are |
42 | 44 | # non-existent. Replace them with dummy versions that always raise OSError. |
43 | 45 | def Popen(*args, **kwargs): |
44 | 46 | raise OSError("subprocess.Popen is not supported") |
| 47 | + |
| 48 | + def check_output(*args, **kwargs): |
| 49 | + raise OSError("subprocess.check_output is not supported") |
45 | 50 | PIPE = -1 |
46 | 51 | STDOUT = -2 |
47 | 52 | # There is no need to catch CalledProcessError. These stubs cannot raise |
48 | 53 | # it. None in an except clause will simply not match any exceptions. |
49 | 54 | CalledProcessError = None |
50 | | - |
51 | | - |
52 | | -def _check_output(*popenargs, **kwargs): |
53 | | - r"""Run command with arguments and return its output as a byte |
54 | | - string. |
55 | | -
|
56 | | - If the exit code was non-zero it raises a CalledProcessError. The |
57 | | - CalledProcessError object will have the return code in the |
58 | | - returncode |
59 | | - attribute and output in the output attribute. |
60 | | -
|
61 | | - The arguments are the same as for the Popen constructor. Example:: |
62 | | -
|
63 | | - >>> check_output(["ls", "-l", "/dev/null"]) |
64 | | - 'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' |
65 | | -
|
66 | | - The stdout argument is not allowed as it is used internally. |
67 | | - To capture standard error in the result, use stderr=STDOUT.:: |
68 | | -
|
69 | | - >>> check_output(["/bin/sh", "-c", |
70 | | - ... "ls -l non_existent_file ; exit 0"], |
71 | | - ... stderr=STDOUT) |
72 | | - 'ls: non_existent_file: No such file or directory\n' |
73 | | - """ |
74 | | - if 'stdout' in kwargs: |
75 | | - raise ValueError('stdout argument not allowed, it will be overridden.') |
76 | | - process = Popen(stdout=PIPE, *popenargs, **kwargs) |
77 | | - output, unused_err = process.communicate() |
78 | | - retcode = process.poll() |
79 | | - if retcode: |
80 | | - cmd = kwargs.get("args") |
81 | | - if cmd is None: |
82 | | - cmd = popenargs[0] |
83 | | - raise subprocess.CalledProcessError(retcode, cmd, output=output) |
84 | | - return output |
85 | | - |
86 | | - |
87 | | -# python2.7's subprocess provides a check_output method |
88 | | -if hasattr(subprocess, 'check_output'): |
89 | | - check_output = subprocess.check_output |
90 | | -else: |
91 | | - check_output = _check_output |
0 commit comments