Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e3a2fbe
Add subprocess.run_pipeline() for command pipe chaining
gpshead Nov 27, 2025
4feb2a8
Add documentation for subprocess.run_pipeline()
gpshead Nov 27, 2025
2a11d4b
Refactor run_pipeline() to use multiplexed I/O
gpshead Nov 28, 2025
2470e14
Add deadlock prevention tests for run_pipeline()
gpshead Nov 28, 2025
e22d1da
Simplify _communicate_streams() to only accept file objects
gpshead Nov 28, 2025
a3e98a7
Improve test_pipeline_large_data_with_stderr to use large stderr
gpshead Nov 28, 2025
3c28ed6
Remove obsolete XXX comment about non-blocking I/O
gpshead Nov 29, 2025
9f53a8e
Refactor POSIX communicate I/O into shared _communicate_io_posix()
gpshead Nov 29, 2025
d420f29
Fix _communicate_streams_windows to avoid blocking with large input
gpshead Nov 29, 2025
df8f082
Fix memoryview and closed stdin handling in _communicate_streams_posix
gpshead Nov 29, 2025
978cd76
Factor out _flush_stdin() and _make_input_view() helpers
gpshead Nov 29, 2025
15f8a93
Support universal_newlines and use _translate_newlines in run_pipeline
gpshead Nov 29, 2025
e6a78a9
Merge remote-tracking branch 'origin/main' into claude/subprocess-pip…
gpshead Apr 25, 2026
5bc5be4
gh-47798: Fix run_pipeline text-mode breaking the Windows backend
gpshead Apr 25, 2026
ae3ee12
gh-47798: Fix TimeoutExpired.output picking wrong stream in run_pipeline
gpshead Apr 25, 2026
11ada45
gh-47798: Reject stdin=PIPE in run_pipeline()
gpshead Apr 25, 2026
51e1ae2
gh-47798: Reject close_fds=False in run_pipeline()
gpshead Apr 25, 2026
c218959
gh-47798: run_pipeline: small correctness cleanups
gpshead Apr 25, 2026
a348976
gh-47798: run_pipeline: kill all children before waiting on any
gpshead Apr 25, 2026
8b953b0
gh-47798: run_pipeline: small style cleanups
gpshead Apr 25, 2026
5175866
gh-47798: Document stderr=STDOUT behavior and add NEWS entry
gpshead Apr 25, 2026
bbdbd93
gh-47798: run_pipeline: cover gaps in test coverage
gpshead Apr 25, 2026
499ea81
gh-47798: Rename PipelineResult to CompletedPipeline
gpshead Apr 25, 2026
fd0231e
gh-47798: run_pipeline: small style and consistency nits
gpshead Apr 25, 2026
6e53bb9
gh-47798: run_pipeline: documentation gap fixes
gpshead Apr 25, 2026
56bbd4a
gh-47798: Restore close-on-EOF in Popen._communicate via factored helper
gpshead Apr 25, 2026
7ea824a
gh-47798: Test Popen.communicate resume after partial-write timeout
gpshead Apr 25, 2026
0091aa8
gh-47798: run_pipeline: cover shell, env, cwd, pass_fds forwarding
gpshead Apr 25, 2026
208c4ae
gh-47798: run_pipeline: cover stderr=PIPE success, errors=replace, br…
gpshead Apr 25, 2026
f3e0fea
gh-47798: Fix flake in test_pipeline_pass_fds
gpshead Apr 25, 2026
9c484e6
gh-47798: Assert TimeoutExpired output/stderr types on partial timeout
gpshead Apr 25, 2026
757f158
gh-47798: Tighten run_pipeline comments to standing invariants
gpshead Apr 26, 2026
86a1b19
gh-47798: Note Stage(cmd, **overrides) as a run_pipeline extension point
gpshead Apr 26, 2026
3f599d9
gh-47798: Scope _communicate_io_posix to POSIX; close read_streams on…
gpshead Apr 26, 2026
2fddfb2
gh-47798: PipelineError.__str__: render signal deaths, drop 3-entry cap
gpshead Apr 26, 2026
6ef0292
gh-47798: run_pipeline tests: hoist imports, fix docstring, un-skip t…
gpshead Apr 26, 2026
af28f82
gh-47798: Test PipelineError signal __str__ and mid-pipeline spawn fa…
gpshead Apr 26, 2026
5c97eac
Merge remote-tracking branch 'upstream/main' into claude/subprocess-p…
gpshead Apr 26, 2026
33fec2c
gh-47798: run_pipeline: reject shell=True and executable=
gpshead Apr 26, 2026
e267731
gh-47798: Docs: lead with run_pipeline for shell pipes; whatsnew entry
gpshead Apr 26, 2026
84c9ec9
gh-47798: Add subprocess.PipelineCommand for per-command run_pipeline…
gpshead Apr 26, 2026
eff75cc
gh-47798: run_pipeline: reject session/group kwargs and pipeline-leve…
gpshead Apr 26, 2026
6dd8dad
gh-47798: run_pipeline: identify the failing command in error output
gpshead Apr 26, 2026
3169b93
gh-47798: Prefer double quotes in run_pipeline code, tests, and doc e…
gpshead Apr 26, 2026
7b87748
Merge remote-tracking branch 'upstream/main' into claude/subprocess-p…
gpshead Apr 27, 2026
ae5a6b2
gh-47798: Reorganize subprocess docs around run and run_pipeline subs…
gpshead Apr 27, 2026
03c2da4
gh-47798: Fix flaky pipeline tests where downstream exits without rea…
gpshead Apr 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
329 changes: 305 additions & 24 deletions Doc/library/subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ Using the :mod:`!subprocess` Module
-----------------------------------

The recommended approach to invoking subprocesses is to use the :func:`run`
function for all use cases it can handle. For more advanced use cases, the
underlying :class:`Popen` interface can be used directly.
function for all use cases it can handle. For pipelines, look to the
:func:`run_pipeline` function. For more advanced use cases, the underlying
:class:`Popen` interface can be used directly.

The :func:`!run` function
^^^^^^^^^^^^^^^^^^^^^^^^^

.. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,\
capture_output=False, shell=False, cwd=None, timeout=None, \
Expand Down Expand Up @@ -160,6 +163,9 @@ underlying :class:`Popen` interface can be used directly.

.. versionadded:: 3.5

Constants and base exceptions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. data:: DEVNULL

Special value that can be used as the *stdin*, *stdout* or *stderr* argument
Expand Down Expand Up @@ -723,6 +729,267 @@ functions.
with a non-zero :attr:`~Popen.returncode`.


The :func:`!run_pipeline` function
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. function:: run_pipeline(*commands, stdin=None, input=None, \
stdout=None, stderr=None, capture_output=False, \
timeout=None, check=False, encoding=None, \
errors=None, text=None, env=None, \
**other_popen_kwargs)

Run a pipeline of commands connected via pipes, similar to shell pipelines.
Wait for all commands to complete, then return a :class:`CompletedPipeline`
instance.

Each positional argument should be a command: either a sequence of
program arguments, or a :class:`PipelineCommand` wrapping one with
per-command overrides. Bare sequences are wrapped in a
:class:`PipelineCommand` on entry. The standard output of each
command is connected to the standard input of the next command in the
pipeline.

This function requires at least two commands. For a single command, use
:func:`run` instead.

If *capture_output* is true, the standard output of the final command and
the standard error of all commands will be captured (see *Standard
error handling* below). The *stdout* and *stderr* arguments may not be
supplied at the same time as *capture_output*.

A *timeout* may be specified in seconds. If the timeout expires, all
child processes will be killed and waited for, and then a
:exc:`TimeoutExpired` exception will be raised.

The *input* argument is passed to the first command's stdin. If used, it
must be a byte sequence, or a string if *encoding* or *errors* is specified
or *text* is true.

If *check* is true, and any process in the pipeline exits with a non-zero
exit code, a :exc:`PipelineError` exception will be raised. This behavior
is similar to the shell's ``pipefail`` option.

If *encoding* or *errors* are specified, or *text* is true, file objects
are opened in text mode using the specified encoding and errors.

If *stdin* is specified, it is connected to the first command's standard
input. If *stdout* is specified, it is connected to the last command's
standard output. When *stdout* is :data:`PIPE`, the output is available
in the returned :class:`CompletedPipeline`'s
:attr:`~CompletedPipeline.stdout` attribute.

Other keyword arguments are passed to every command's :class:`Popen`
call. ``close_fds=False`` is rejected because inherited copies of the
inter-process pipe ends in sibling children would prevent EOF from
being signaled and cause deadlocks. ``shell=True`` and ``executable=``
are also rejected: the pipeline itself replaces the shell, and
per-command shell interpretation would re-introduce the quoting and
injection surface this function exists to avoid. When one command
genuinely needs shell interpretation (a glob, or a shell builtin),
wrap it in a :class:`PipelineCommand` with ``shell=True``.
``start_new_session`` and ``process_group`` are also rejected: each
command is spawned as a sibling child of the calling process, so
applying these per command does not produce a single process group
spanning the pipeline. ``stderr=STDOUT`` at the pipeline level is
rejected because it would merge each non-final command's stderr into
the next command's stdin; use a :class:`PipelineCommand` with
``stderr=STDOUT`` for the one command that needs it, or
``capture_output=True`` to capture stderr from every command.

.. rubric:: Standard error handling

When stderr is captured (via ``capture_output=True`` or ``stderr=PIPE``),
every command in the pipeline writes to a single shared pipe, and the
captured :attr:`~CompletedPipeline.stderr` is the interleaved output of
all of them. Two consequences follow from the shared pipe:

* In text mode the interleaving can split multi-byte characters across
writes from different processes. If that is a concern, capture in
binary mode and decode yourself, or pass ``errors="replace"`` or
``errors="backslashreplace"``.

* If any child spawns a grandchild process that keeps the inherited
stderr file descriptor open after the child itself exits, the
parent's read on the stderr pipe will not see EOF and
:func:`run_pipeline` will block. Either do not capture stderr, or
ensure such grandchildren fully detach (closing inherited fds) before
daemonizing.

To exempt one command from the shared stderr pipe, wrap it in a
:class:`PipelineCommand` with ``stderr=DEVNULL`` (discard) or
``stderr=STDOUT`` (merge into that command's stdout).
``stderr=STDOUT`` at the *pipeline* level is rejected.

Examples::

>>> import subprocess
>>> # Equivalent to: echo "hello world" | tr a-z A-Z
>>> result = subprocess.run_pipeline(
... ["echo", "hello world"],
... ["tr", "a-z", "A-Z"],
... capture_output=True, text=True
... )
>>> result.stdout
'HELLO WORLD\n'
>>> result.returncodes
(0, 0)

>>> # Pipeline with three commands
>>> result = subprocess.run_pipeline(
... ["echo", "one\ntwo\nthree"],
... ["sort"],
... ["head", "-n", "2"],
... capture_output=True, text=True
... )
>>> result.stdout
'one\nthree\n'

>>> # Using input parameter
>>> result = subprocess.run_pipeline(
... ["cat"],
... ["wc", "-l"],
... input="line1\nline2\nline3\n",
... capture_output=True, text=True
... )
>>> result.stdout.strip()
'3'

>>> # Error handling with check=True
>>> subprocess.run_pipeline(
... ["echo", "hello"],
... ["false"], # exits with status 1
... check=True
... )
Traceback (most recent call last):
...
subprocess.PipelineError: Pipeline failed: ['false'] (commands[1]) returned 1

.. versionadded:: next


.. class:: CompletedPipeline

The return value from :func:`run_pipeline`, representing a pipeline of
processes that have finished.

.. attribute:: commands

The commands used to launch the pipeline, as a tuple of
:class:`PipelineCommand` instances. Bare argv sequences passed to
:func:`run_pipeline` are wrapped, so every element has ``.args``
and the override attributes.

.. attribute:: returncodes

Tuple of exit status codes for each command in the pipeline. Typically,
an exit status of 0 indicates that the command ran successfully.

A negative value ``-N`` indicates that the command was terminated by
signal ``N`` (POSIX only).

.. attribute:: stdout

Captured stdout from the final command in the pipeline. A bytes sequence,
or a string if :func:`run_pipeline` was called with an encoding, errors,
or ``text=True``. ``None`` if stdout was not captured.

.. attribute:: stderr

Captured stderr from all commands in the pipeline, combined. A bytes
sequence, or a string if :func:`run_pipeline` was called with an
encoding, errors, or ``text=True``. ``None`` if stderr was not captured.

.. method:: check_returncodes()

If any element of :attr:`returncodes` is non-zero, raise a
:exc:`PipelineError`.

.. versionadded:: next


.. class:: PipelineCommand(args, /, *, stderr=None, env=None, cwd=None, \
shell=False)

One command in a :func:`run_pipeline` call. :func:`run_pipeline`
wraps each bare argv sequence it receives in a :class:`PipelineCommand`,
so :attr:`CompletedPipeline.commands` and :attr:`PipelineError.commands`
always hold instances of this class.

Construct one explicitly when a single command needs different stderr
handling, a different *env* or *cwd*, or shell interpretation. Any
override left at its default means the corresponding
:func:`run_pipeline` keyword applies to this command as it would to a
bare argv sequence.

*args* is a sequence of program arguments, or a string if *shell* is
true. Passing a string with ``shell=False`` (or a sequence with
``shell=True``) raises :exc:`TypeError`.

*stderr* may be ``None`` (use the pipeline's shared stderr handling),
:data:`DEVNULL` (discard this command's stderr), or :data:`STDOUT`
(merge this command's stderr into its stdout stream). Any other value
raises :exc:`ValueError`.

*env* and *cwd*, if given, replace the pipeline-level *env* and *cwd*
for this command only.

*shell*, if true, runs this command's *args* through the shell.

Example -- discard the noisy stderr of one command while the rest
keep the pipeline's stderr handling::

>>> from subprocess import run_pipeline, PipelineCommand, DEVNULL
>>> with open("out.gz", "wb") as f:
... result = run_pipeline(
... PipelineCommand(["dd", "if=infile", "bs=1M"], stderr=DEVNULL),
... ["pigz"],
... stdout=f, check=True,
... )

.. versionadded:: next


.. exception:: PipelineError

Subclass of :exc:`SubprocessError`, raised when a pipeline run by
:func:`run_pipeline` (with ``check=True``) contains one or more commands
that returned a non-zero exit status. This is similar to the shell's
``pipefail`` behavior.

:exc:`PipelineError` is a sibling of :exc:`CalledProcessError`, not a
subclass. To handle both single-command and pipeline failures with
one ``except`` clause, catch :exc:`SubprocessError` (which is also
the common base of :exc:`TimeoutExpired`). Retry helpers and
decorators that match on :exc:`CalledProcessError` will not catch
pipeline failures by default.

.. attribute:: commands

The commands used in the pipeline, as a tuple of
:class:`PipelineCommand` instances.

.. attribute:: returncodes

Tuple of exit status codes for each command in the pipeline.

.. attribute:: stdout

Output of the final command if it was captured. Otherwise, ``None``.

.. attribute:: stderr

Combined stderr output of all commands if it was captured.
Otherwise, ``None``.

.. attribute:: failed

Tuple of ``(index, command, returncode)`` triples for each command
that returned a non-zero exit status. The *index* is the position
of the command in the pipeline (0-based).

.. versionadded:: next


Exceptions
^^^^^^^^^^

Expand All @@ -736,16 +1003,21 @@ will be raised by the child only if the selected shell itself was not found.
To determine if the shell failed to find the requested application, it is
necessary to check the return code or output from the subprocess.

A :exc:`ValueError` will be raised if :class:`Popen` is called with invalid
arguments.
A :exc:`ValueError` will be raised if :class:`Popen` and related functions are
called with invalid arguments.

:func:`check_call` and :func:`check_output` will raise
:exc:`CalledProcessError` if the called process returns a non-zero return
code.
:func:`check_call`, :func:`check_output`, :func:`run` with ``check=True``, or
:meth:`CompletedProcess.check_returncode` will raise :exc:`CalledProcessError`
if the called process had a non-zero return code.

:func:`run_pipeline` with ``check=True`` or
:meth:`CompletedPipeline.check_returncodes` will raise :exc:`PipelineError` if
any process in the pipeline had a non-zero return code.

All of the functions and methods that accept a *timeout* parameter, such as
:func:`run` and :meth:`Popen.communicate` will raise :exc:`TimeoutExpired` if
the timeout expires before the process exits.
:func:`run`, :func:`run_pipeline`, and :meth:`Popen.communicate` will raise
:exc:`TimeoutExpired` if the timeout expires before the process or processes
exit.

Exceptions defined in this module all inherit from :exc:`SubprocessError`.

Expand Down Expand Up @@ -1393,24 +1665,33 @@ Replacing shell pipeline

becomes::

p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

The ``p1.stdout.close()`` call after starting the p2 is important in order for
p1 to receive a SIGPIPE if p2 exits before p1.
result = run_pipeline(["dmesg"], ["grep", "hda"],
capture_output=True, check=True)
output = result.stdout

Alternatively, for trusted input, the shell's own pipeline support may still
be used directly:
:func:`run_pipeline` connects the commands, closes the parent's copies of
the intermediate pipe ends, waits for every process, and (with
``check=True``) raises :exc:`PipelineError` if *any* command fails --
equivalent to the
shell's ``set -o pipefail`` without needing a shell.

.. code-block:: bash
If you need to read the final command's output incrementally rather than
waiting for the whole pipeline to finish (for example, streaming a large
decompressed file), chain :class:`Popen` instances directly::

output=$(dmesg | grep hda)

becomes::

output = check_output("dmesg | grep hda", shell=True)
p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
for line in p2.stdout:
...
p2.wait()
p1.wait()
if p1.returncode or p2.returncode:
... # handle failure in either command

The ``p1.stdout.close()`` call after starting p2 is important in order for
p1 to receive a SIGPIPE if p2 exits before p1. Each process must be
waited on and its return code checked to detect failure in any command.


Replacing :func:`os.system`
Expand Down
14 changes: 14 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,20 @@ ssl
subprocess
----------

* Added :func:`subprocess.run_pipeline` for running a sequence of commands
connected stdout-to-stdin, similar to a shell ``a | b | c`` pipeline,
without invoking a shell. It returns a
:class:`~subprocess.CompletedPipeline` carrying the return codes of every
command; passing ``check=True`` raises :exc:`~subprocess.PipelineError`
if any command fails (pipefail semantics). Individual commands may be
wrapped in a :class:`~subprocess.PipelineCommand` to override *stderr*,
*env*, *cwd*, or *shell* for that command only. This replaces the
manual
:class:`~subprocess.Popen`-chaining recipe and the
``bash -c "set -o pipefail; ..."`` workaround previously needed to detect
failures in non-final commands.
(Contributed by Gregory P. Smith in :gh:`47798`.)

* :meth:`subprocess.Popen.wait`: when ``timeout`` is not ``None`` and the
platform supports it, an efficient event-driven mechanism is used to wait for
process termination:
Expand Down
Loading
Loading