--- subprocess.py.273 2012-12-05 17:44:45.970021863 -0500 +++ subprocess.py 2012-12-05 17:53:01.878331857 -0500 @@ -138,6 +138,9 @@ check_output(*popenargs, **kwargs): output = 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 ---------- @@ -531,11 +534,29 @@ def check_output(*popenargs, **kwargs): ... "ls -l non_existent_file ; exit 0"], ... stderr=STDOUT) '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="when in the course of fooman events\n") + 'when in the course of barman events\n' + """ 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 be used together.') + inputdata = kwargs['input'] + del kwargs['input'] + kwargs['stdin'] = PIPE + else: + inputdata = None process = Popen(stdout=PIPE, *popenargs, **kwargs) - output, unused_err = process.communicate() + output, unused_err = process.communicate(inputdata) retcode = process.poll() if retcode: cmd = kwargs.get("args")