@@ -448,6 +448,16 @@ underlying :class:`Popen` interface can be used directly.
448448 that returned a non-zero exit status. This is similar to the shell's
449449 ``pipefail `` behavior.
450450
451+ :exc: `PipelineError ` is a *sibling * of :exc: `CalledProcessError `, not a
452+ subclass: a pipeline carries N commands and N return codes, so there is
453+ no single ``returncode `` or ``cmd `` value an existing
454+ ``except CalledProcessError: `` handler could be shown without being
455+ misleading. To handle both single-command and pipeline failures with
456+ one ``except `` clause, catch :exc: `SubprocessError ` (which is also the
457+ common base of :exc: `TimeoutExpired `). Retry helpers and decorators
458+ that match on :exc: `CalledProcessError ` will not catch pipeline
459+ failures by default.
460+
451461 .. attribute :: commands
452462
453463 List of commands that were used in the pipeline.
@@ -1606,24 +1616,32 @@ Replacing shell pipeline
16061616
16071617 becomes::
16081618
1609- p1 = Popen(["dmesg"], stdout=PIPE)
1610- p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
1611- p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
1612- output = p2.communicate()[0]
1619+ result = run_pipeline(["dmesg"], ["grep", "hda"],
1620+ capture_output=True, check=True)
1621+ output = result.stdout
16131622
1614- The ``p1.stdout.close() `` call after starting the p2 is important in order for
1615- p1 to receive a SIGPIPE if p2 exits before p1.
1623+ :func: `run_pipeline ` connects the stages, closes the parent's copies of the
1624+ intermediate pipe ends, waits for every process, and (with ``check=True ``)
1625+ raises :exc: `PipelineError ` if *any * stage fails -- equivalent to the
1626+ shell's ``set -o pipefail `` without needing a shell.
16161627
1617- Alternatively, for trusted input, the shell's own pipeline support may still
1618- be used directly:
1628+ If you need to read the final stage's output incrementally rather than
1629+ waiting for the whole pipeline to finish (for example, streaming a large
1630+ decompressed file), chain :class: `Popen ` instances directly::
16191631
1620- .. code-block :: bash
1621-
1622- output=$( dmesg | grep hda)
1623-
1624- becomes::
1625-
1626- output = check_output("dmesg | grep hda", shell=True)
1632+ p1 = Popen(["dmesg"], stdout=PIPE)
1633+ p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
1634+ p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
1635+ for line in p2.stdout:
1636+ ...
1637+ p2.wait()
1638+ p1.wait()
1639+ if p1.returncode or p2.returncode:
1640+ ... # handle failure in either stage
1641+
1642+ The ``p1.stdout.close() `` call after starting p2 is important in order for
1643+ p1 to receive a SIGPIPE if p2 exits before p1. Each process must be
1644+ waited on and its return code checked to detect failure in any stage.
16271645
16281646
16291647Replacing :func: `os.system `
0 commit comments