diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-04-22 17:20:54 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-04-22 17:20:54 (GMT) |
commit | fcd9f222385c7855241a3f6bfe84b454e2373cdf (patch) | |
tree | 3ffb181b96970f4907a3f56c83f178988d267a5a /Lib/subprocess.py | |
parent | 1859fe80c424cd2c8211c90f1c46a64606cb8ccb (diff) | |
download | cpython-fcd9f222385c7855241a3f6bfe84b454e2373cdf.zip cpython-fcd9f222385c7855241a3f6bfe84b454e2373cdf.tar.gz cpython-fcd9f222385c7855241a3f6bfe84b454e2373cdf.tar.bz2 |
Issue #16624: `subprocess.check_output` now accepts an `input` argument,
allowing the subprocess's stdin to be provided as a (byte) string.
Patch by Zack Weinberg.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index f69b42a..78907e0 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -175,6 +175,9 @@ check_output(*popenargs, **kwargs): >>> output = subprocess.check_output(["ls", "-l", "/dev/null"]) + There is an additional optional argument, "input", allowing you to + pass a string to the subprocess's stdin. If you use this argument + you may not also use the Popen constructor's "stdin" argument. Exceptions ---------- @@ -563,14 +566,31 @@ def check_output(*popenargs, timeout=None, **kwargs): ... stderr=STDOUT) b'ls: non_existent_file: No such file or directory\n' + There is an additional optional argument, "input", allowing you to + pass a string to the subprocess's stdin. If you use this argument + you may not also use the Popen constructor's "stdin" argument, as + it too will be used internally. Example: + + >>> check_output(["sed", "-e", "s/foo/bar/"], + ... input=b"when in the course of fooman events\n") + b'when in the course of barman events\n' + If universal_newlines=True is passed, the return value will be a string rather than bytes. """ if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') + if 'input' in kwargs: + if 'stdin' in kwargs: + raise ValueError('stdin and input arguments may not both be used.') + inputdata = kwargs['input'] + del kwargs['input'] + kwargs['stdin'] = PIPE + else: + inputdata = None with Popen(*popenargs, stdout=PIPE, **kwargs) as process: try: - output, unused_err = process.communicate(timeout=timeout) + output, unused_err = process.communicate(inputdata, timeout=timeout) except TimeoutExpired: process.kill() output, unused_err = process.communicate() |