2007-08-15 11:28:22 -03:00
|
|
|
:mod:`subprocess` --- Subprocess management
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
.. module:: subprocess
|
|
|
|
:synopsis: Subprocess management.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. moduleauthor:: Peter Åstrand <astrand@lysator.liu.se>
|
|
|
|
.. sectionauthor:: Peter Åstrand <astrand@lysator.liu.se>
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/subprocess.py`
|
|
|
|
|
|
|
|
--------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The :mod:`subprocess` module allows you to spawn new processes, connect to their
|
|
|
|
input/output/error pipes, and obtain their return codes. This module intends to
|
2014-03-12 23:41:35 -03:00
|
|
|
replace several older modules and functions::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
os.system
|
|
|
|
os.spawn*
|
|
|
|
|
|
|
|
Information about how the :mod:`subprocess` module can be used to replace these
|
|
|
|
modules and functions can be found in the following sections.
|
|
|
|
|
Merged revisions 64475,64544-64545,64550,64557-64558,64565,64570,64577,64582-64583,64585,64590,64592-64593,64625,64630,64638,64647,64655-64656,64663-64664 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64475 | raymond.hettinger | 2008-06-22 22:29:28 -0500 (Sun, 22 Jun 2008) | 1 line
Issue 3161: Missing import and test.
........
r64544 | georg.brandl | 2008-06-26 16:12:55 -0500 (Thu, 26 Jun 2008) | 2 lines
Use newer versions of externals.
........
r64545 | benjamin.peterson | 2008-06-26 16:23:30 -0500 (Thu, 26 Jun 2008) | 1 line
add a htmlview directive
........
r64550 | brett.cannon | 2008-06-26 19:32:16 -0500 (Thu, 26 Jun 2008) | 2 lines
Ignore .pyc and .pyo files.
........
r64557 | mark.dickinson | 2008-06-27 05:11:52 -0500 (Fri, 27 Jun 2008) | 3 lines
Remove trailing 'L's from numerator and denominator in the
repr() of a Fraction instance.
........
r64558 | mark.dickinson | 2008-06-27 06:03:21 -0500 (Fri, 27 Jun 2008) | 2 lines
Add Jean Brouwers for his work on math.sum
........
r64565 | raymond.hettinger | 2008-06-27 16:34:24 -0500 (Fri, 27 Jun 2008) | 1 line
Fix whitespace in example code.
........
r64570 | hyeshik.chang | 2008-06-27 20:04:31 -0500 (Fri, 27 Jun 2008) | 8 lines
Give information for compililation of _multiprocessing.SemLock on FreeBSD:
FreeBSD's P1003.1b semaphore support is highly experimental and
it's disabled by default. Even if a user loads the experimental
kernel module manually, _multiprocessing doesn't work correctly due
to several known incompatibilities around sem_unlink and sem_getvalue,
yet.
........
r64577 | raymond.hettinger | 2008-06-28 17:16:53 -0500 (Sat, 28 Jun 2008) | 1 line
Issue 3230: Do not the set specific size macro.
........
r64582 | benjamin.peterson | 2008-06-28 18:06:05 -0500 (Sat, 28 Jun 2008) | 2 lines
convert test_audioop to unittest. Thanks to Giampaolo Rodola.
........
r64583 | benjamin.peterson | 2008-06-28 18:06:49 -0500 (Sat, 28 Jun 2008) | 1 line
rewrap
........
r64585 | benjamin.peterson | 2008-06-28 18:35:31 -0500 (Sat, 28 Jun 2008) | 1 line
fix typo
........
r64590 | benjamin.peterson | 2008-06-29 08:43:07 -0500 (Sun, 29 Jun 2008) | 1 line
reinstate the ending backtick. thanks Nick :)
........
r64592 | vinay.sajip | 2008-06-29 16:25:28 -0500 (Sun, 29 Jun 2008) | 2 lines
Removed out-of-date comment in _install_handlers and
used issubclass in place of equality comparison of classes.
........
r64593 | vinay.sajip | 2008-06-29 16:27:15 -0500 (Sun, 29 Jun 2008) | 1 line
Updated to reflect change in logging.config to remove out-of-date comment in _install_handlers and the use of issubclass in place of equality comparison of classes.
........
r64625 | georg.brandl | 2008-07-01 14:59:00 -0500 (Tue, 01 Jul 2008) | 2 lines
Add a link to PEP 324.
........
r64630 | georg.brandl | 2008-07-01 15:18:10 -0500 (Tue, 01 Jul 2008) | 2 lines
#3216: fix Execute's parameter description.
........
r64638 | georg.brandl | 2008-07-01 15:50:02 -0500 (Tue, 01 Jul 2008) | 2 lines
#1410739: add a footnote about "is" and "unusual" behavior.
........
r64647 | benjamin.peterson | 2008-07-01 18:33:06 -0500 (Tue, 01 Jul 2008) | 1 line
add ABC to the glossary
........
r64655 | mark.dickinson | 2008-07-02 04:37:01 -0500 (Wed, 02 Jul 2008) | 7 lines
Replace occurrences of '\d' with '[0-9]' in Decimal regex, to make sure
that the behaviour of Decimal doesn't change if/when re.UNICODE becomes
assumed in Python 3.0.
Also add a check that alternative Unicode digits (e.g. u'\N{FULLWIDTH
DIGIT ONE}') are *not* accepted in a numeric string.
........
r64656 | nick.coghlan | 2008-07-02 08:09:19 -0500 (Wed, 02 Jul 2008) | 1 line
Issue 3190: pydoc now hides module __package__ attributes
........
r64663 | jesse.noller | 2008-07-02 11:44:09 -0500 (Wed, 02 Jul 2008) | 1 line
Reenable the manager tests with Amaury's threading fix
........
r64664 | facundo.batista | 2008-07-02 11:52:55 -0500 (Wed, 02 Jul 2008) | 4 lines
Issue #449227: Now with the rlcompleter module, callable objects are
added a '(' when completed.
........
2008-07-02 17:22:54 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:pep:`324` -- PEP proposing the subprocess module
|
|
|
|
|
2022-08-02 16:00:41 -03:00
|
|
|
.. include:: ../includes/wasm-notavail.rst
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-11-08 04:07:10 -04:00
|
|
|
Using the :mod:`subprocess` Module
|
|
|
|
----------------------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
The recommended approach to invoking subprocesses is to use the :func:`run`
|
2015-04-14 23:12:14 -03:00
|
|
|
function for all use cases it can handle. For more advanced use cases, the
|
|
|
|
underlying :class:`Popen` interface can be used directly.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
.. function:: run(args, *, stdin=None, input=None, stdout=None, stderr=None,\
|
2018-08-09 17:01:54 -03:00
|
|
|
capture_output=False, shell=False, cwd=None, timeout=None, \
|
2018-11-11 23:40:42 -04:00
|
|
|
check=False, encoding=None, errors=None, text=None, env=None, \
|
2020-05-17 07:52:47 -03:00
|
|
|
universal_newlines=None, **other_popen_kwargs)
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
Run the command described by *args*. Wait for command to complete, then
|
2015-04-14 20:14:25 -03:00
|
|
|
return a :class:`CompletedProcess` instance.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
The arguments shown above are merely the most common ones, described below
|
2011-11-08 08:11:21 -04:00
|
|
|
in :ref:`frequently-used-arguments` (hence the use of keyword-only notation
|
|
|
|
in the abbreviated signature). The full function signature is largely the
|
2018-01-30 02:40:39 -04:00
|
|
|
same as that of the :class:`Popen` constructor - most of the arguments to
|
|
|
|
this function are passed through to that interface. (*timeout*, *input*,
|
|
|
|
*check*, and *capture_output* are not.)
|
2011-11-08 08:11:21 -04:00
|
|
|
|
2018-01-30 02:40:39 -04:00
|
|
|
If *capture_output* is true, stdout and stderr will be captured.
|
|
|
|
When used, the internal :class:`Popen` object is automatically created with
|
|
|
|
``stdout=PIPE`` and ``stderr=PIPE``. The *stdout* and *stderr* arguments may
|
2019-05-14 16:33:17 -03:00
|
|
|
not be supplied at the same time as *capture_output*. If you wish to capture
|
|
|
|
and combine both streams into one, use ``stdout=PIPE`` and ``stderr=STDOUT``
|
|
|
|
instead of *capture_output*.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2023-05-25 17:14:09 -03:00
|
|
|
A *timeout* may be specified in seconds, it is internally passed on to
|
|
|
|
:meth:`Popen.communicate`. If the timeout expires, the child process will be
|
|
|
|
killed and waited for. The :exc:`TimeoutExpired` exception will be
|
|
|
|
re-raised after the child process has terminated. The initial process
|
|
|
|
creation itself cannot be interrupted on many platform APIs so you are not
|
|
|
|
guaranteed to see a timeout exception until at least after however long
|
|
|
|
process creation takes.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
The *input* argument is passed to :meth:`Popen.communicate` and thus to the
|
|
|
|
subprocess's stdin. If used it must be a byte sequence, or a string if
|
2017-10-22 23:01:19 -03:00
|
|
|
*encoding* or *errors* is specified or *text* is true. When
|
2016-09-07 00:16:17 -03:00
|
|
|
used, the internal :class:`Popen` object is automatically created with
|
|
|
|
``stdin=PIPE``, and the *stdin* argument may not be used as well.
|
2011-11-08 08:11:21 -04:00
|
|
|
|
2016-10-19 12:30:05 -03:00
|
|
|
If *check* is true, and the process exits with a non-zero exit code, a
|
2015-04-14 20:14:25 -03:00
|
|
|
:exc:`CalledProcessError` exception will be raised. Attributes of that
|
|
|
|
exception hold the arguments, the exit code, and stdout and stderr if they
|
|
|
|
were captured.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2017-10-22 23:01:19 -03:00
|
|
|
If *encoding* or *errors* are specified, or *text* is true,
|
2016-09-07 00:16:17 -03:00
|
|
|
file objects for stdin, stdout and stderr are opened in text mode using the
|
|
|
|
specified *encoding* and *errors* or the :class:`io.TextIOWrapper` default.
|
2017-10-22 23:01:19 -03:00
|
|
|
The *universal_newlines* argument is equivalent to *text* and is provided
|
|
|
|
for backwards compatibility. By default, file objects are opened in binary mode.
|
2016-09-07 00:16:17 -03:00
|
|
|
|
2018-06-05 08:41:42 -03:00
|
|
|
If *env* is not ``None``, it must be a mapping that defines the environment
|
|
|
|
variables for the new process; these are used instead of the default
|
2022-11-08 17:00:31 -04:00
|
|
|
behavior of inheriting the current process' environment. It is passed
|
|
|
|
directly to :class:`Popen`. This mapping can be str to str on any platform
|
|
|
|
or bytes to bytes on POSIX platforms much like :data:`os.environ` or
|
|
|
|
:data:`os.environb`.
|
2018-06-05 08:41:42 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
Examples::
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
>>> subprocess.run(["ls", "-l"]) # doesn't capture output
|
|
|
|
CompletedProcess(args=['ls', '-l'], returncode=0)
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
>>> subprocess.run("exit 1", shell=True, check=True)
|
2011-11-08 06:49:23 -04:00
|
|
|
Traceback (most recent call last):
|
2015-04-14 20:14:25 -03:00
|
|
|
...
|
2011-11-08 06:49:23 -04:00
|
|
|
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
|
|
|
|
|
2018-01-30 02:40:39 -04:00
|
|
|
>>> subprocess.run(["ls", "-l", "/dev/null"], capture_output=True)
|
2015-04-14 20:14:25 -03:00
|
|
|
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
|
2018-01-30 02:40:39 -04:00
|
|
|
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n', stderr=b'')
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. versionadded:: 3.5
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2016-09-07 00:16:17 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
|
|
|
|
Added *encoding* and *errors* parameters
|
|
|
|
|
2017-10-22 23:01:19 -03:00
|
|
|
.. versionchanged:: 3.7
|
|
|
|
|
2018-01-30 02:40:39 -04:00
|
|
|
Added the *text* parameter, as a more understandable alias of *universal_newlines*.
|
|
|
|
Added the *capture_output* parameter.
|
2017-10-22 23:01:19 -03:00
|
|
|
|
2023-02-09 13:36:24 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-02-08 18:12:19 -04:00
|
|
|
|
|
|
|
Changed Windows shell search order for ``shell=True``. The current
|
|
|
|
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
|
|
|
|
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
|
|
|
|
malicious program named ``cmd.exe`` into a current directory no
|
|
|
|
longer works.
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. class:: CompletedProcess
|
2011-11-08 08:11:21 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
The return value from :func:`run`, representing a process that has finished.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: args
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
The arguments used to launch the process. This may be a list or a string.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: returncode
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
Exit status of the child process. Typically, an exit status of 0 indicates
|
|
|
|
that it ran successfully.
|
2011-11-08 08:11:21 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
A negative value ``-N`` indicates that the child was terminated by signal
|
|
|
|
``N`` (POSIX only).
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: stdout
|
2013-04-22 14:20:54 -03:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
Captured stdout from the child process. A bytes sequence, or a string if
|
2017-10-22 23:01:19 -03:00
|
|
|
:func:`run` was called with an encoding, errors, or text=True.
|
|
|
|
``None`` if stdout was not captured.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
If you ran the process with ``stderr=subprocess.STDOUT``, stdout and
|
|
|
|
stderr will be combined in this attribute, and :attr:`stderr` will be
|
2016-10-19 10:29:26 -03:00
|
|
|
``None``.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: stderr
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
Captured stderr from the child process. A bytes sequence, or a string if
|
2017-10-22 23:01:19 -03:00
|
|
|
:func:`run` was called with an encoding, errors, or text=True.
|
|
|
|
``None`` if stderr was not captured.
|
2013-04-22 14:20:54 -03:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. method:: check_returncode()
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
If :attr:`returncode` is non-zero, raise a :exc:`CalledProcessError`.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. versionadded:: 3.5
|
2011-11-08 08:11:21 -04:00
|
|
|
|
|
|
|
.. data:: DEVNULL
|
|
|
|
|
|
|
|
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
|
|
|
|
to :class:`Popen` and indicates that the special file :data:`os.devnull`
|
|
|
|
will be used.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
.. data:: PIPE
|
|
|
|
|
|
|
|
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
|
|
|
|
to :class:`Popen` and indicates that a pipe to the standard stream should be
|
2014-05-11 17:26:21 -03:00
|
|
|
opened. Most useful with :meth:`Popen.communicate`.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: STDOUT
|
|
|
|
|
|
|
|
Special value that can be used as the *stderr* argument to :class:`Popen` and
|
|
|
|
indicates that standard error should go into the same handle as standard
|
|
|
|
output.
|
|
|
|
|
|
|
|
|
2012-08-09 09:11:45 -03:00
|
|
|
.. exception:: SubprocessError
|
|
|
|
|
|
|
|
Base class for all other exceptions from this module.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
|
|
|
|
|
|
|
.. exception:: TimeoutExpired
|
|
|
|
|
|
|
|
Subclass of :exc:`SubprocessError`, raised when a timeout expires
|
|
|
|
while waiting for a child process.
|
|
|
|
|
|
|
|
.. attribute:: cmd
|
|
|
|
|
|
|
|
Command that was used to spawn the child process.
|
|
|
|
|
|
|
|
.. attribute:: timeout
|
|
|
|
|
|
|
|
Timeout in seconds.
|
|
|
|
|
|
|
|
.. attribute:: output
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
Output of the child process if it was captured by :func:`run` or
|
2022-09-30 14:45:47 -03:00
|
|
|
:func:`check_output`. Otherwise, ``None``. This is always
|
|
|
|
:class:`bytes` when any output was captured regardless of the
|
|
|
|
``text=True`` setting. It may remain ``None`` instead of ``b''``
|
|
|
|
when no output was observed.
|
2012-08-09 09:11:45 -03:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: stdout
|
|
|
|
|
|
|
|
Alias for output, for symmetry with :attr:`stderr`.
|
|
|
|
|
|
|
|
.. attribute:: stderr
|
|
|
|
|
|
|
|
Stderr output of the child process if it was captured by :func:`run`.
|
2022-09-30 14:45:47 -03:00
|
|
|
Otherwise, ``None``. This is always :class:`bytes` when stderr output
|
|
|
|
was captured regardless of the ``text=True`` setting. It may remain
|
|
|
|
``None`` instead of ``b''`` when no stderr output was observed.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2012-08-09 09:11:45 -03:00
|
|
|
.. versionadded:: 3.3
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. versionchanged:: 3.5
|
|
|
|
*stdout* and *stderr* attributes added
|
2012-08-09 09:11:45 -03:00
|
|
|
|
|
|
|
.. exception:: CalledProcessError
|
|
|
|
|
|
|
|
Subclass of :exc:`SubprocessError`, raised when a process run by
|
2022-04-30 18:32:00 -03:00
|
|
|
:func:`check_call`, :func:`check_output`, or :func:`run` (with ``check=True``)
|
|
|
|
returns a non-zero exit status.
|
|
|
|
|
2012-08-09 09:11:45 -03:00
|
|
|
|
|
|
|
.. attribute:: returncode
|
|
|
|
|
2016-06-02 21:31:21 -03:00
|
|
|
Exit status of the child process. If the process exited due to a
|
|
|
|
signal, this will be the negative signal number.
|
2012-08-09 09:11:45 -03:00
|
|
|
|
|
|
|
.. attribute:: cmd
|
|
|
|
|
|
|
|
Command that was used to spawn the child process.
|
|
|
|
|
|
|
|
.. attribute:: output
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
Output of the child process if it was captured by :func:`run` or
|
2012-08-09 09:11:45 -03:00
|
|
|
:func:`check_output`. Otherwise, ``None``.
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. attribute:: stdout
|
|
|
|
|
|
|
|
Alias for output, for symmetry with :attr:`stderr`.
|
|
|
|
|
|
|
|
.. attribute:: stderr
|
|
|
|
|
|
|
|
Stderr output of the child process if it was captured by :func:`run`.
|
|
|
|
Otherwise, ``None``.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.5
|
|
|
|
*stdout* and *stderr* attributes added
|
2012-08-09 09:11:45 -03:00
|
|
|
|
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
.. _frequently-used-arguments:
|
|
|
|
|
|
|
|
Frequently Used Arguments
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
To support a wide variety of use cases, the :class:`Popen` constructor (and
|
|
|
|
the convenience functions) accept a large number of optional arguments. For
|
|
|
|
most typical use cases, many of these arguments can be safely left at their
|
|
|
|
default values. The arguments that are most commonly needed are:
|
|
|
|
|
|
|
|
*args* is required for all calls and should be a string, or a sequence of
|
|
|
|
program arguments. Providing a sequence of arguments is generally
|
|
|
|
preferred, as it allows the module to take care of any required escaping
|
|
|
|
and quoting of arguments (e.g. to permit spaces in file names). If passing
|
|
|
|
a single string, either *shell* must be :const:`True` (see below) or else
|
|
|
|
the string must simply name the program to be executed without specifying
|
|
|
|
any arguments.
|
|
|
|
|
|
|
|
*stdin*, *stdout* and *stderr* specify the executed program's standard input,
|
|
|
|
standard output and standard error file handles, respectively. Valid values
|
2023-01-20 03:56:13 -04:00
|
|
|
are ``None``, :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a
|
|
|
|
positive integer), and an existing :term:`file object` with a valid file
|
|
|
|
descriptor. With the default settings of ``None``, no redirection will
|
|
|
|
occur. :data:`PIPE` indicates that a new pipe to the child should be
|
|
|
|
created. :data:`DEVNULL` indicates that the special file :data:`os.devnull`
|
|
|
|
will be used. Additionally, *stderr* can be :data:`STDOUT`, which indicates
|
|
|
|
that the stderr data from the child process should be captured into the same
|
|
|
|
file handle as for *stdout*.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2012-08-15 11:43:58 -03:00
|
|
|
.. index::
|
|
|
|
single: universal newlines; subprocess module
|
|
|
|
|
2018-02-11 16:58:23 -04:00
|
|
|
If *encoding* or *errors* are specified, or *text* (also known as
|
|
|
|
*universal_newlines*) is true,
|
2016-09-07 00:16:17 -03:00
|
|
|
the file objects *stdin*, *stdout* and *stderr* will be opened in text
|
|
|
|
mode using the *encoding* and *errors* specified in the call or the
|
|
|
|
defaults for :class:`io.TextIOWrapper`.
|
2013-07-07 04:26:45 -03:00
|
|
|
|
2016-09-07 00:16:17 -03:00
|
|
|
For *stdin*, line ending characters ``'\n'`` in the input will be converted
|
|
|
|
to the default line separator :data:`os.linesep`. For *stdout* and *stderr*,
|
|
|
|
all line endings in the output will be converted to ``'\n'``. For more
|
|
|
|
information see the documentation of the :class:`io.TextIOWrapper` class
|
|
|
|
when the *newline* argument to its constructor is ``None``.
|
|
|
|
|
|
|
|
If text mode is not used, *stdin*, *stdout* and *stderr* will be opened as
|
|
|
|
binary streams. No encoding or line ending conversion is performed.
|
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
Added *encoding* and *errors* parameters.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2018-02-11 16:58:23 -04:00
|
|
|
.. versionadded:: 3.7
|
|
|
|
Added the *text* parameter as an alias for *universal_newlines*.
|
|
|
|
|
2012-08-13 16:09:04 -03:00
|
|
|
.. note::
|
|
|
|
|
2013-03-20 22:32:03 -03:00
|
|
|
The newlines attribute of the file objects :attr:`Popen.stdin`,
|
|
|
|
:attr:`Popen.stdout` and :attr:`Popen.stderr` are not updated by
|
|
|
|
the :meth:`Popen.communicate` method.
|
2012-08-13 16:09:04 -03:00
|
|
|
|
|
|
|
If *shell* is ``True``, the specified command will be executed through
|
2012-09-15 02:34:08 -03:00
|
|
|
the shell. This can be useful if you are using Python primarily for the
|
2011-11-08 06:49:23 -04:00
|
|
|
enhanced control flow it offers over most system shells and still want
|
2012-09-15 02:34:08 -03:00
|
|
|
convenient access to other shell features such as shell pipes, filename
|
|
|
|
wildcards, environment variable expansion, and expansion of ``~`` to a
|
|
|
|
user's home directory. However, note that Python itself offers
|
|
|
|
implementations of many shell-like features (in particular, :mod:`glob`,
|
|
|
|
:mod:`fnmatch`, :func:`os.walk`, :func:`os.path.expandvars`,
|
|
|
|
:func:`os.path.expanduser`, and :mod:`shutil`).
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2012-08-13 16:11:14 -03:00
|
|
|
.. versionchanged:: 3.3
|
|
|
|
When *universal_newlines* is ``True``, the class uses the encoding
|
|
|
|
:func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`
|
|
|
|
instead of ``locale.getpreferredencoding()``. See the
|
|
|
|
:class:`io.TextIOWrapper` class for more information on this change.
|
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
.. note::
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
Read the `Security Considerations`_ section before using ``shell=True``.
|
2012-10-28 06:42:26 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
These options, along with all of the other options, are described in more
|
|
|
|
detail in the :class:`Popen` constructor documentation.
|
|
|
|
|
|
|
|
|
2011-12-25 06:27:37 -04:00
|
|
|
Popen Constructor
|
2011-12-25 12:14:11 -04:00
|
|
|
^^^^^^^^^^^^^^^^^
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
The underlying process creation and management in this module is handled by
|
|
|
|
the :class:`Popen` class. It offers a lot of flexibility so that developers
|
|
|
|
are able to handle the less common cases not covered by the convenience
|
|
|
|
functions.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2013-03-23 15:44:25 -03:00
|
|
|
.. class:: Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, \
|
2012-10-10 21:46:18 -03:00
|
|
|
stderr=None, preexec_fn=None, close_fds=True, shell=False, \
|
2018-11-11 23:40:42 -04:00
|
|
|
cwd=None, env=None, universal_newlines=None, \
|
2012-10-10 21:46:18 -03:00
|
|
|
startupinfo=None, creationflags=0, restore_signals=True, \
|
2020-12-16 21:37:28 -04:00
|
|
|
start_new_session=False, pass_fds=(), *, group=None, \
|
2019-10-12 17:24:56 -03:00
|
|
|
extra_groups=None, user=None, umask=-1, \
|
2022-05-05 20:22:32 -03:00
|
|
|
encoding=None, errors=None, text=None, pipesize=-1, \
|
|
|
|
process_group=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-05-11 17:28:35 -03:00
|
|
|
Execute a child program in a new process. On POSIX, the class uses
|
2020-10-20 17:02:24 -03:00
|
|
|
:meth:`os.execvpe`-like behavior to execute the child program. On Windows,
|
2012-10-10 21:46:18 -03:00
|
|
|
the class uses the Windows ``CreateProcess()`` function. The arguments to
|
|
|
|
:class:`Popen` are as follows.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-05-28 16:49:35 -03:00
|
|
|
*args* should be a sequence of program arguments or else a single string
|
|
|
|
or :term:`path-like object`.
|
2018-02-27 19:03:46 -04:00
|
|
|
By default, the program to execute is the first item in *args* if *args* is
|
|
|
|
a sequence. If *args* is a string, the interpretation is
|
|
|
|
platform-dependent and described below. See the *shell* and *executable*
|
|
|
|
arguments for additional differences from the default behavior. Unless
|
|
|
|
otherwise stated, it is recommended to pass *args* as a sequence.
|
2012-10-09 03:06:57 -03:00
|
|
|
|
2020-10-20 17:02:24 -03:00
|
|
|
.. warning::
|
|
|
|
|
2022-07-05 06:16:10 -03:00
|
|
|
For maximum reliability, use a fully qualified path for the executable.
|
2020-10-20 17:02:24 -03:00
|
|
|
To search for an unqualified name on :envvar:`PATH`, use
|
|
|
|
:meth:`shutil.which`. On all platforms, passing :data:`sys.executable`
|
|
|
|
is the recommended way to launch the current Python interpreter again,
|
|
|
|
and use the ``-m`` command-line format to launch an installed module.
|
|
|
|
|
|
|
|
Resolving the path of *executable* (or the first item of *args*) is
|
|
|
|
platform dependent. For POSIX, see :meth:`os.execvpe`, and note that
|
|
|
|
when resolving or searching for the executable path, *cwd* overrides the
|
|
|
|
current working directory and *env* can override the ``PATH``
|
|
|
|
environment variable. For Windows, see the documentation of the
|
|
|
|
``lpApplicationName`` and ``lpCommandLine`` parameters of WinAPI
|
|
|
|
``CreateProcess``, and note that when resolving or searching for the
|
|
|
|
executable path with ``shell=False``, *cwd* does not override the
|
|
|
|
current working directory and *env* cannot override the ``PATH``
|
|
|
|
environment variable. Using a full path avoids all of these variations.
|
|
|
|
|
2020-02-10 18:51:01 -04:00
|
|
|
An example of passing some arguments to an external program
|
|
|
|
as a sequence is::
|
|
|
|
|
|
|
|
Popen(["/usr/bin/git", "commit", "-m", "Fixes a bug."])
|
|
|
|
|
2014-05-11 17:28:35 -03:00
|
|
|
On POSIX, if *args* is a string, the string is interpreted as the name or
|
2012-10-10 21:46:18 -03:00
|
|
|
path of the program to execute. However, this can only be done if not
|
|
|
|
passing arguments to the program.
|
2010-02-04 12:41:57 -04:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2020-02-10 18:51:01 -04:00
|
|
|
It may not be obvious how to break a shell command into a sequence of arguments,
|
|
|
|
especially in complex cases. :meth:`shlex.split` can illustrate how to
|
|
|
|
determine the correct tokenization for *args*::
|
2010-02-04 12:41:57 -04:00
|
|
|
|
|
|
|
>>> import shlex, subprocess
|
2010-02-05 12:25:12 -04:00
|
|
|
>>> command_line = input()
|
2010-02-04 12:41:57 -04:00
|
|
|
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
|
|
|
|
>>> args = shlex.split(command_line)
|
|
|
|
>>> print(args)
|
|
|
|
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
|
|
|
|
>>> p = subprocess.Popen(args) # Success!
|
|
|
|
|
|
|
|
Note in particular that options (such as *-input*) and arguments (such
|
|
|
|
as *eggs.txt*) that are separated by whitespace in the shell go in separate
|
|
|
|
list elements, while arguments that need quoting or backslash escaping when
|
|
|
|
used in the shell (such as filenames containing spaces or the *echo* command
|
|
|
|
shown above) are single list elements.
|
|
|
|
|
2012-10-10 21:46:18 -03:00
|
|
|
On Windows, if *args* is a sequence, it will be converted to a string in a
|
|
|
|
manner described in :ref:`converting-argument-sequence`. This is because
|
|
|
|
the underlying ``CreateProcess()`` operates on strings.
|
2012-10-09 03:06:57 -03:00
|
|
|
|
2019-05-28 16:49:35 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
*args* parameter accepts a :term:`path-like object` if *shell* is
|
|
|
|
``False`` and a sequence containing path-like objects on POSIX.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
*args* parameter accepts a :term:`path-like object` if *shell* is
|
|
|
|
``False`` and a sequence containing bytes and path-like objects
|
|
|
|
on Windows.
|
|
|
|
|
2016-10-19 10:43:42 -03:00
|
|
|
The *shell* argument (which defaults to ``False``) specifies whether to use
|
|
|
|
the shell as the program to execute. If *shell* is ``True``, it is
|
2012-10-10 21:46:18 -03:00
|
|
|
recommended to pass *args* as a string rather than as a sequence.
|
2012-10-09 03:06:57 -03:00
|
|
|
|
2014-05-11 17:28:35 -03:00
|
|
|
On POSIX with ``shell=True``, the shell defaults to :file:`/bin/sh`. If
|
2012-10-09 03:06:57 -03:00
|
|
|
*args* is a string, the string specifies the command
|
|
|
|
to execute through the shell. This means that the string must be
|
2010-02-04 12:41:57 -04:00
|
|
|
formatted exactly as it would be when typed at the shell prompt. This
|
|
|
|
includes, for example, quoting or backslash escaping filenames with spaces in
|
|
|
|
them. If *args* is a sequence, the first item specifies the command string, and
|
|
|
|
any additional items will be treated as additional arguments to the shell
|
2012-10-09 03:06:57 -03:00
|
|
|
itself. That is to say, :class:`Popen` does the equivalent of::
|
2010-02-04 12:41:57 -04:00
|
|
|
|
|
|
|
Popen(['/bin/sh', '-c', args[0], args[1], ...])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-10-09 03:06:57 -03:00
|
|
|
On Windows with ``shell=True``, the :envvar:`COMSPEC` environment variable
|
|
|
|
specifies the default shell. The only time you need to specify
|
|
|
|
``shell=True`` on Windows is when the command you wish to execute is built
|
|
|
|
into the shell (e.g. :command:`dir` or :command:`copy`). You do not need
|
|
|
|
``shell=True`` to run a batch file or console-based executable.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
.. note::
|
2012-10-11 02:52:22 -03:00
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
Read the `Security Considerations`_ section before using ``shell=True``.
|
2012-10-11 02:52:22 -03:00
|
|
|
|
2014-09-21 16:10:56 -03:00
|
|
|
*bufsize* will be supplied as the corresponding argument to the
|
|
|
|
:func:`open` function when creating the stdin/stdout/stderr pipe
|
|
|
|
file objects:
|
|
|
|
|
2023-07-28 07:26:28 -03:00
|
|
|
- ``0`` means unbuffered (read and write are one
|
2014-09-21 16:10:56 -03:00
|
|
|
system call and can return short)
|
2023-07-28 07:26:28 -03:00
|
|
|
- ``1`` means line buffered
|
2023-02-08 04:12:46 -04:00
|
|
|
(only usable if ``text=True`` or ``universal_newlines=True``)
|
2014-09-21 16:10:56 -03:00
|
|
|
- any other positive value means use a buffer of approximately that
|
|
|
|
size
|
|
|
|
- negative bufsize (the default) means the system default of
|
|
|
|
io.DEFAULT_BUFFER_SIZE will be used.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-11-25 03:48:37 -04:00
|
|
|
.. versionchanged:: 3.3.1
|
2013-03-23 15:44:25 -03:00
|
|
|
*bufsize* now defaults to -1 to enable buffering by default to match the
|
2013-11-25 03:48:37 -04:00
|
|
|
behavior that most code expects. In versions prior to Python 3.2.4 and
|
2023-07-28 07:26:28 -03:00
|
|
|
3.3.1 it incorrectly defaulted to ``0`` which was unbuffered
|
2013-11-25 03:48:37 -04:00
|
|
|
and allowed short reads. This was unintentional and did not match the
|
|
|
|
behavior of Python 2 as most code expected.
|
2010-06-02 14:10:49 -03:00
|
|
|
|
2012-10-09 03:06:57 -03:00
|
|
|
The *executable* argument specifies a replacement program to execute. It
|
|
|
|
is very seldom needed. When ``shell=False``, *executable* replaces the
|
2012-10-10 21:46:18 -03:00
|
|
|
program to execute specified by *args*. However, the original *args* is
|
|
|
|
still passed to the program. Most programs treat the program specified
|
|
|
|
by *args* as the command name, which can then be different from the program
|
2014-05-11 17:28:35 -03:00
|
|
|
actually executed. On POSIX, the *args* name
|
2012-10-09 03:06:57 -03:00
|
|
|
becomes the display name for the executable in utilities such as
|
2014-05-11 17:28:35 -03:00
|
|
|
:program:`ps`. If ``shell=True``, on POSIX the *executable* argument
|
2012-10-09 03:06:57 -03:00
|
|
|
specifies a replacement shell for the default :file:`/bin/sh`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-05-28 16:49:35 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
*executable* parameter accepts a :term:`path-like object` on POSIX.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
*executable* parameter accepts a bytes and :term:`path-like object`
|
|
|
|
on Windows.
|
|
|
|
|
2023-02-09 13:36:24 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-02-08 18:12:19 -04:00
|
|
|
|
|
|
|
Changed Windows shell search order for ``shell=True``. The current
|
|
|
|
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
|
|
|
|
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
|
|
|
|
malicious program named ``cmd.exe`` into a current directory no
|
|
|
|
longer works.
|
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
*stdin*, *stdout* and *stderr* specify the executed program's standard input,
|
Merged revisions 67531-67532,67538,67553-67554,67556-67557,67571,67574-67575,67579-67580,67591,67597,67608,67631 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67531 | georg.brandl | 2008-12-04 19:54:05 +0100 (Thu, 04 Dec 2008) | 2 lines
Add reference to enumerate() to indices example.
........
r67532 | georg.brandl | 2008-12-04 19:59:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Add another heapq example.
........
r67538 | georg.brandl | 2008-12-04 22:28:16 +0100 (Thu, 04 Dec 2008) | 2 lines
Clarification to avoid confusing output with file descriptors.
........
r67553 | georg.brandl | 2008-12-05 08:49:49 +0100 (Fri, 05 Dec 2008) | 2 lines
#4408: document regex.groups.
........
r67554 | georg.brandl | 2008-12-05 08:52:26 +0100 (Fri, 05 Dec 2008) | 2 lines
#4409: fix asterisks looking like footnotes.
........
r67556 | georg.brandl | 2008-12-05 09:02:17 +0100 (Fri, 05 Dec 2008) | 2 lines
#4441: improve doc for os.open() flags.
........
r67557 | georg.brandl | 2008-12-05 09:06:57 +0100 (Fri, 05 Dec 2008) | 2 lines
Add an index entry for "subclassing immutable types".
........
r67571 | georg.brandl | 2008-12-05 10:13:45 +0100 (Fri, 05 Dec 2008) | 2 lines
Use markup.
........
r67574 | georg.brandl | 2008-12-05 10:25:32 +0100 (Fri, 05 Dec 2008) | 2 lines
#4441 followup: Add link to open() docs for Windows.
........
r67575 | georg.brandl | 2008-12-05 12:34:51 +0100 (Fri, 05 Dec 2008) | 2 lines
#4544: add `dedent` to textwrap.__all__.
........
r67579 | georg.brandl | 2008-12-05 16:29:39 +0100 (Fri, 05 Dec 2008) | 2 lines
#4517: add "special method" glossary entry and clarify when __getattribute__ is bypassed.
........
r67580 | georg.brandl | 2008-12-05 16:32:29 +0100 (Fri, 05 Dec 2008) | 2 lines
#4478: document that copyfile() can raise Error.
........
r67591 | georg.brandl | 2008-12-05 19:00:06 +0100 (Fri, 05 Dec 2008) | 2 lines
Followup to #4511: add link from decorator glossary entry to definition.
........
r67597 | georg.brandl | 2008-12-05 20:03:19 +0100 (Fri, 05 Dec 2008) | 2 lines
Remove confusing sentence part.
........
r67608 | georg.brandl | 2008-12-06 12:57:12 +0100 (Sat, 06 Dec 2008) | 2 lines
Follow-up to #4488: document PIPE and STDOUT properly.
........
r67631 | georg.brandl | 2008-12-07 12:54:07 +0100 (Sun, 07 Dec 2008) | 2 lines
Add link to the favicon to the docs.
........
2008-12-07 11:06:20 -04:00
|
|
|
standard output and standard error file handles, respectively. Valid values
|
2023-01-20 03:56:13 -04:00
|
|
|
are ``None``, :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a
|
|
|
|
positive integer), and an existing :term:`file object` with a valid file
|
|
|
|
descriptor. With the default settings of ``None``, no redirection will
|
|
|
|
occur. :data:`PIPE` indicates that a new pipe to the child should be
|
|
|
|
created. :data:`DEVNULL` indicates that the special file :data:`os.devnull`
|
|
|
|
will be used. Additionally, *stderr* can be :data:`STDOUT`, which indicates
|
2022-02-25 12:41:19 -04:00
|
|
|
that the stderr data from the applications should be captured into the same
|
2023-01-20 03:56:13 -04:00
|
|
|
file handle as for *stdout*.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
If *preexec_fn* is set to a callable object, this object will be called in the
|
2010-03-14 03:49:55 -03:00
|
|
|
child process just before the child is executed.
|
2014-05-11 17:28:35 -03:00
|
|
|
(POSIX only)
|
2010-03-14 03:49:55 -03:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
2022-05-05 20:22:32 -03:00
|
|
|
The *preexec_fn* parameter is NOT SAFE to use in the presence of threads
|
2010-03-14 03:49:55 -03:00
|
|
|
in your application. The child process could deadlock before exec is
|
|
|
|
called.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you need to modify the environment for the child use the *env*
|
|
|
|
parameter rather than doing it in a *preexec_fn*.
|
2022-05-05 20:22:32 -03:00
|
|
|
The *start_new_session* and *process_group* parameters should take the place of
|
|
|
|
code using *preexec_fn* to call :func:`os.setsid` or :func:`os.setpgid` in the child.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-08-27 18:36:56 -03:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
|
|
|
|
The *preexec_fn* parameter is no longer supported in subinterpreters.
|
|
|
|
The use of the parameter in a subinterpreter raises
|
|
|
|
:exc:`RuntimeError`. The new restriction may affect applications that
|
|
|
|
are deployed in mod_wsgi, uWSGI, and other embedded environments.
|
|
|
|
|
2023-07-28 07:26:28 -03:00
|
|
|
If *close_fds* is true, all file descriptors except ``0``, ``1`` and
|
|
|
|
``2`` will be closed before the child process is executed. Otherwise
|
2018-03-25 14:27:59 -03:00
|
|
|
when *close_fds* is false, file descriptors obey their inheritable flag
|
|
|
|
as described in :ref:`fd_inheritance`.
|
|
|
|
|
2010-12-04 05:10:44 -04:00
|
|
|
On Windows, if *close_fds* is true then no handles will be inherited by the
|
2017-12-18 05:28:19 -04:00
|
|
|
child process unless explicitly passed in the ``handle_list`` element of
|
|
|
|
:attr:`STARTUPINFO.lpAttributeList`, or by standard handle redirection.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-12-14 09:43:30 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
The default for *close_fds* was changed from :const:`False` to
|
|
|
|
what is described above.
|
|
|
|
|
2017-12-18 05:28:19 -04:00
|
|
|
.. versionchanged:: 3.7
|
|
|
|
On Windows the default for *close_fds* was changed from :const:`False` to
|
|
|
|
:const:`True` when redirecting the standard handles. It's now possible to
|
|
|
|
set *close_fds* to :const:`True` when redirecting the standard handles.
|
|
|
|
|
2010-12-14 09:43:30 -04:00
|
|
|
*pass_fds* is an optional sequence of file descriptors to keep open
|
|
|
|
between the parent and child. Providing any *pass_fds* forces
|
2014-05-11 17:28:35 -03:00
|
|
|
*close_fds* to be :const:`True`. (POSIX only)
|
2010-12-14 09:43:30 -04:00
|
|
|
|
2018-07-05 21:01:16 -03:00
|
|
|
.. versionchanged:: 3.2
|
2010-12-14 09:43:30 -04:00
|
|
|
The *pass_fds* parameter was added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2012-09-30 04:10:28 -03:00
|
|
|
If *cwd* is not ``None``, the function changes the working directory to
|
2019-05-28 16:49:35 -03:00
|
|
|
*cwd* before executing the child. *cwd* can be a string, bytes or
|
2021-04-17 06:59:18 -03:00
|
|
|
:term:`path-like <path-like object>` object. On POSIX, the function
|
2017-02-26 13:06:10 -04:00
|
|
|
looks for *executable* (or for the first item in *args*) relative to *cwd*
|
|
|
|
if the executable path is a relative path.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.6
|
2019-05-28 16:49:35 -03:00
|
|
|
*cwd* parameter accepts a :term:`path-like object` on POSIX.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.7
|
|
|
|
*cwd* parameter accepts a :term:`path-like object` on Windows.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
*cwd* parameter accepts a bytes object on Windows.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-11-29 06:17:13 -04:00
|
|
|
If *restore_signals* is true (the default) all signals that Python has set to
|
2010-03-14 03:49:55 -03:00
|
|
|
SIG_IGN are restored to SIG_DFL in the child process before the exec.
|
|
|
|
Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals.
|
2014-05-11 17:28:35 -03:00
|
|
|
(POSIX only)
|
2010-03-14 03:49:55 -03:00
|
|
|
|
|
|
|
.. versionchanged:: 3.2
|
|
|
|
*restore_signals* was added.
|
|
|
|
|
2022-05-05 20:22:32 -03:00
|
|
|
If *start_new_session* is true the ``setsid()`` system call will be made in the
|
|
|
|
child process prior to the execution of the subprocess.
|
2010-03-14 03:49:55 -03:00
|
|
|
|
2022-05-05 20:22:32 -03:00
|
|
|
.. availability:: POSIX
|
2010-03-14 03:49:55 -03:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
*start_new_session* was added.
|
|
|
|
|
2022-05-05 20:22:32 -03:00
|
|
|
If *process_group* is a non-negative integer, the ``setpgid(0, value)`` system call will
|
|
|
|
be made in the child process prior to the execution of the subprocess.
|
|
|
|
|
|
|
|
.. availability:: POSIX
|
|
|
|
.. versionchanged:: 3.11
|
|
|
|
*process_group* was added.
|
|
|
|
|
2019-09-12 14:15:44 -03:00
|
|
|
If *group* is not ``None``, the setregid() system call will be made in the
|
|
|
|
child process prior to the execution of the subprocess. If the provided
|
|
|
|
value is a string, it will be looked up via :func:`grp.getgrnam()` and
|
|
|
|
the value in ``gr_gid`` will be used. If the value is an integer, it
|
|
|
|
will be passed verbatim. (POSIX only)
|
|
|
|
|
|
|
|
.. availability:: POSIX
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
|
|
|
If *extra_groups* is not ``None``, the setgroups() system call will be
|
|
|
|
made in the child process prior to the execution of the subprocess.
|
|
|
|
Strings provided in *extra_groups* will be looked up via
|
|
|
|
:func:`grp.getgrnam()` and the values in ``gr_gid`` will be used.
|
|
|
|
Integer values will be passed verbatim. (POSIX only)
|
|
|
|
|
|
|
|
.. availability:: POSIX
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
|
|
|
If *user* is not ``None``, the setreuid() system call will be made in the
|
|
|
|
child process prior to the execution of the subprocess. If the provided
|
|
|
|
value is a string, it will be looked up via :func:`pwd.getpwnam()` and
|
|
|
|
the value in ``pw_uid`` will be used. If the value is an integer, it will
|
|
|
|
be passed verbatim. (POSIX only)
|
|
|
|
|
|
|
|
.. availability:: POSIX
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
2019-10-12 17:24:56 -03:00
|
|
|
If *umask* is not negative, the umask() system call will be made in the
|
|
|
|
child process prior to the execution of the subprocess.
|
|
|
|
|
|
|
|
.. availability:: POSIX
|
|
|
|
.. versionadded:: 3.9
|
|
|
|
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
If *env* is not ``None``, it must be a mapping that defines the environment
|
2010-03-14 03:49:55 -03:00
|
|
|
variables for the new process; these are used instead of the default
|
2022-11-08 17:00:31 -04:00
|
|
|
behavior of inheriting the current process' environment. This mapping can be
|
|
|
|
str to str on any platform or bytes to bytes on POSIX platforms much like
|
|
|
|
:data:`os.environ` or :data:`os.environb`.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-04-16 15:15:32 -03:00
|
|
|
.. note::
|
2009-04-15 19:35:15 -03:00
|
|
|
|
2009-12-20 10:38:23 -04:00
|
|
|
If specified, *env* must provide any variables required for the program to
|
|
|
|
execute. On Windows, in order to run a `side-by-side assembly`_ the
|
|
|
|
specified *env* **must** include a valid :envvar:`SystemRoot`.
|
2009-04-15 19:35:15 -03:00
|
|
|
|
2016-02-26 14:37:12 -04:00
|
|
|
.. _side-by-side assembly: https://en.wikipedia.org/wiki/Side-by-Side_Assembly
|
2009-04-16 15:15:32 -03:00
|
|
|
|
2018-02-11 16:58:23 -04:00
|
|
|
If *encoding* or *errors* are specified, or *text* is true, the file objects
|
|
|
|
*stdin*, *stdout* and *stderr* are opened in text mode with the specified
|
2022-04-30 03:53:29 -03:00
|
|
|
*encoding* and *errors*, as described above in :ref:`frequently-used-arguments`.
|
2018-02-11 16:58:23 -04:00
|
|
|
The *universal_newlines* argument is equivalent to *text* and is provided
|
|
|
|
for backwards compatibility. By default, file objects are opened in binary mode.
|
2016-09-07 00:16:17 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
*encoding* and *errors* were added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2018-02-11 16:58:23 -04:00
|
|
|
.. versionadded:: 3.7
|
|
|
|
*text* was added as a more readable alias for *universal_newlines*.
|
|
|
|
|
2011-04-30 00:17:51 -03:00
|
|
|
If given, *startupinfo* will be a :class:`STARTUPINFO` object, which is
|
|
|
|
passed to the underlying ``CreateProcess`` function.
|
2017-11-08 10:18:59 -04:00
|
|
|
*creationflags*, if given, can be one or more of the following flags:
|
|
|
|
|
|
|
|
* :data:`CREATE_NEW_CONSOLE`
|
|
|
|
* :data:`CREATE_NEW_PROCESS_GROUP`
|
|
|
|
* :data:`ABOVE_NORMAL_PRIORITY_CLASS`
|
|
|
|
* :data:`BELOW_NORMAL_PRIORITY_CLASS`
|
|
|
|
* :data:`HIGH_PRIORITY_CLASS`
|
|
|
|
* :data:`IDLE_PRIORITY_CLASS`
|
|
|
|
* :data:`NORMAL_PRIORITY_CLASS`
|
|
|
|
* :data:`REALTIME_PRIORITY_CLASS`
|
|
|
|
* :data:`CREATE_NO_WINDOW`
|
|
|
|
* :data:`DETACHED_PROCESS`
|
|
|
|
* :data:`CREATE_DEFAULT_ERROR_MODE`
|
|
|
|
* :data:`CREATE_BREAKAWAY_FROM_JOB`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2020-10-19 20:30:02 -03:00
|
|
|
*pipesize* can be used to change the size of the pipe when
|
|
|
|
:data:`PIPE` is used for *stdin*, *stdout* or *stderr*. The size of the pipe
|
|
|
|
is only changed on platforms that support this (only Linux at this time of
|
|
|
|
writing). Other platforms will ignore this parameter.
|
|
|
|
|
|
|
|
.. versionadded:: 3.10
|
|
|
|
The ``pipesize`` parameter was added.
|
|
|
|
|
2011-05-12 01:42:08 -03:00
|
|
|
Popen objects are supported as context managers via the :keyword:`with` statement:
|
|
|
|
on exit, standard file descriptors are closed, and the process is waited for.
|
2010-12-02 22:46:02 -04:00
|
|
|
::
|
|
|
|
|
|
|
|
with Popen(["ifconfig"], stdout=PIPE) as proc:
|
|
|
|
log.write(proc.stdout.read())
|
|
|
|
|
2019-06-27 14:47:59 -03:00
|
|
|
.. audit-event:: subprocess.Popen executable,args,cwd,env subprocess.Popen
|
2019-06-24 12:42:54 -03:00
|
|
|
|
|
|
|
Popen and the other functions in this module that use it raise an
|
|
|
|
:ref:`auditing event <auditing>` ``subprocess.Popen`` with arguments
|
2019-11-19 08:14:53 -04:00
|
|
|
``executable``, ``args``, ``cwd``, and ``env``. The value for ``args``
|
2019-06-24 12:42:54 -03:00
|
|
|
may be a single string or a list of strings, depending on platform.
|
|
|
|
|
2010-12-02 22:46:02 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
Added context manager support.
|
|
|
|
|
2016-05-20 07:11:15 -03:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
Popen destructor now emits a :exc:`ResourceWarning` warning if the child
|
|
|
|
process is still running.
|
|
|
|
|
2019-04-25 09:30:16 -03:00
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Popen can use :func:`os.posix_spawn` in some cases for better
|
|
|
|
performance. On Windows Subsystem for Linux and QEMU User Emulation,
|
|
|
|
Popen constructor using :func:`os.posix_spawn` no longer raise an
|
|
|
|
exception on errors like missing program, but the child process fails
|
|
|
|
with a non-zero :attr:`~Popen.returncode`.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Exceptions
|
|
|
|
^^^^^^^^^^
|
|
|
|
|
|
|
|
Exceptions raised in the child process, before the new program has started to
|
2019-01-03 15:53:56 -04:00
|
|
|
execute, will be re-raised in the parent.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
The most common exception raised is :exc:`OSError`. This occurs, for example,
|
|
|
|
when trying to execute a non-existent file. Applications should prepare for
|
2021-07-22 20:06:18 -03:00
|
|
|
:exc:`OSError` exceptions. Note that, when ``shell=True``, :exc:`OSError`
|
2021-07-22 15:23:29 -03:00
|
|
|
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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A :exc:`ValueError` will be raised if :class:`Popen` is called with invalid
|
|
|
|
arguments.
|
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
:func:`check_call` and :func:`check_output` will raise
|
|
|
|
:exc:`CalledProcessError` if the called process returns a non-zero return
|
|
|
|
code.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-03-14 13:02:10 -03:00
|
|
|
All of the functions and methods that accept a *timeout* parameter, such as
|
2023-05-25 17:14:09 -03:00
|
|
|
:func:`run` and :meth:`Popen.communicate` will raise :exc:`TimeoutExpired` if
|
2011-03-14 13:02:10 -03:00
|
|
|
the timeout expires before the process exits.
|
|
|
|
|
2011-03-16 11:03:10 -03:00
|
|
|
Exceptions defined in this module all inherit from :exc:`SubprocessError`.
|
2011-03-14 15:08:43 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
|
|
|
The :exc:`SubprocessError` base class was added.
|
|
|
|
|
2021-08-09 19:35:51 -03:00
|
|
|
.. _subprocess-security:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
Security Considerations
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
Unlike some other popen functions, this implementation will never
|
|
|
|
implicitly call a system shell. This means that all characters,
|
|
|
|
including shell metacharacters, can safely be passed to child processes.
|
|
|
|
If the shell is invoked explicitly, via ``shell=True``, it is the application's
|
|
|
|
responsibility to ensure that all whitespace and metacharacters are
|
|
|
|
quoted appropriately to avoid
|
2016-02-26 14:37:12 -04:00
|
|
|
`shell injection <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
|
2020-11-11 03:29:56 -04:00
|
|
|
vulnerabilities. On :ref:`some platforms <shlex-quote-warning>`, it is possible
|
|
|
|
to use :func:`shlex.quote` for this escaping.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
Popen Objects
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Instances of the :class:`Popen` class have the following methods:
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Popen.poll()
|
|
|
|
|
2013-08-23 18:23:38 -03:00
|
|
|
Check if child process has terminated. Set and return
|
2017-08-29 11:46:24 -03:00
|
|
|
:attr:`~Popen.returncode` attribute. Otherwise, returns ``None``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2011-03-14 13:02:10 -03:00
|
|
|
.. method:: Popen.wait(timeout=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-08-23 18:23:38 -03:00
|
|
|
Wait for child process to terminate. Set and return
|
|
|
|
:attr:`~Popen.returncode` attribute.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-03-14 13:02:10 -03:00
|
|
|
If the process does not terminate after *timeout* seconds, raise a
|
|
|
|
:exc:`TimeoutExpired` exception. It is safe to catch this exception and
|
|
|
|
retry the wait.
|
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
This will deadlock when using ``stdout=PIPE`` or ``stderr=PIPE``
|
|
|
|
and the child process generates enough output to a pipe such that
|
|
|
|
it blocks waiting for the OS pipe buffer to accept more data.
|
|
|
|
Use :meth:`Popen.communicate` when using pipes to avoid that.
|
|
|
|
|
2014-02-24 08:18:47 -04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The function is implemented using a busy loop (non-blocking call and
|
|
|
|
short sleeps). Use the :mod:`asyncio` module for an asynchronous wait:
|
|
|
|
see :class:`asyncio.create_subprocess_exec`.
|
|
|
|
|
2011-03-14 13:36:53 -03:00
|
|
|
.. versionchanged:: 3.3
|
2011-03-14 13:02:10 -03:00
|
|
|
*timeout* was added.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-03-14 13:02:10 -03:00
|
|
|
.. method:: Popen.communicate(input=None, timeout=None)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Interact with process: Send data to stdin. Read data from stdout and stderr,
|
2020-06-24 00:12:10 -03:00
|
|
|
until end-of-file is reached. Wait for process to terminate and set the
|
|
|
|
:attr:`~Popen.returncode` attribute. The optional *input* argument should be
|
|
|
|
data to be sent to the child process, or ``None``, if no data should be sent
|
|
|
|
to the child. If streams were opened in text mode, *input* must be a string.
|
|
|
|
Otherwise, it must be bytes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-10-13 19:52:07 -03:00
|
|
|
:meth:`communicate` returns a tuple ``(stdout_data, stderr_data)``.
|
2016-09-07 00:16:17 -03:00
|
|
|
The data will be strings if streams were opened in text mode; otherwise,
|
|
|
|
bytes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 59107-59186 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
(Note: some conflicts in the PCbuild9 directory reverted. Sorry Christian!)
........
r59120 | christian.heimes | 2007-11-22 03:21:16 -0800 (Thu, 22 Nov 2007) | 3 lines
Backport of the PCbuild9 directory from the py3k branch.
I've finished the last task for the PCbuild9 directory today. I don't think there is much left to do. Now you can all play around with the shiny new VS 2008 and try the PGO builds. I was able to get a speed improvement of about 10% on py3k.
Have fun! :)
........
r59126 | brett.cannon | 2007-11-22 16:06:51 -0800 (Thu, 22 Nov 2007) | 2 lines
Fix a bug in the test for using __loader__.get_data().
........
r59131 | christian.heimes | 2007-11-22 23:05:03 -0800 (Thu, 22 Nov 2007) | 1 line
Backport of PCbuild9 fixes from py3k r59130
........
r59132 | christian.heimes | 2007-11-23 01:10:36 -0800 (Fri, 23 Nov 2007) | 2 lines
Applied patch #1754273 and #1754271 from Thomas Glee
The patches are adding deprecation warnings for back ticks and <>
........
r59133 | christian.heimes | 2007-11-23 04:12:02 -0800 (Fri, 23 Nov 2007) | 2 lines
Fixed problems in the last commit. Filenames and line numbers weren't reported correctly.
Backquotes still don't report the correct file. The AST nodes only contain the line number but not the file name.
........
r59134 | christian.heimes | 2007-11-23 04:16:35 -0800 (Fri, 23 Nov 2007) | 1 line
How did the comment get there?
........
r59135 | christian.heimes | 2007-11-23 05:25:31 -0800 (Fri, 23 Nov 2007) | 1 line
And yet another fix for the patch. Paul Moore has send me a note that I've missed a declaration. The additional code has moved the declaration in the middle of the block.
........
r59136 | andrew.kuchling | 2007-11-23 05:37:39 -0800 (Fri, 23 Nov 2007) | 1 line
Add item
........
r59137 | skip.montanaro | 2007-11-23 09:08:35 -0800 (Fri, 23 Nov 2007) | 2 lines
Make trace and doctest play nice together (issue 1429818). Will backport.
........
r59139 | skip.montanaro | 2007-11-23 09:12:47 -0800 (Fri, 23 Nov 2007) | 1 line
issue 1429818
........
r59144 | facundo.batista | 2007-11-23 09:59:00 -0800 (Fri, 23 Nov 2007) | 10 lines
Major change in the internal structure of the Decimal
number: now it does not store the mantissa as a tuple
of numbers, but as a string.
This avoids a lot of conversions, and achieves a
speedup of 40%. The API remains intact.
Thanks Mark Dickinson.
........
r59146 | facundo.batista | 2007-11-23 10:14:50 -0800 (Fri, 23 Nov 2007) | 3 lines
Test cases from Cowlishaw, v2.57. All are pased cleanly.
........
r59156 | christian.heimes | 2007-11-23 17:36:02 -0800 (Fri, 23 Nov 2007) | 2 lines
Added filename to compiling struct based on Martin's suggestion.
I'm wonder why I was trying to add the filename to the node all the time. The compiling struct is more obvious.
........
r59158 | christian.heimes | 2007-11-23 17:53:59 -0800 (Fri, 23 Nov 2007) | 2 lines
Backport of fixes from py3k branch
svn merge -r59131:HEAD ../../py3k/PCbuild9/ .
........
r59159 | skip.montanaro | 2007-11-23 20:29:08 -0800 (Fri, 23 Nov 2007) | 1 line
revert change that breaks test_doctest (which I forgot to run - sorry)
........
r59162 | skip.montanaro | 2007-11-23 20:31:15 -0800 (Fri, 23 Nov 2007) | 1 line
revert
........
r59164 | georg.brandl | 2007-11-24 03:31:46 -0800 (Sat, 24 Nov 2007) | 3 lines
#1344: document that you need to open std{in,out,err} with PIPE if you want
communicate() to work as described.
........
r59165 | georg.brandl | 2007-11-24 03:39:13 -0800 (Sat, 24 Nov 2007) | 2 lines
#1467: fix documentation for TestResult.add{Error,Failure}.
........
r59166 | georg.brandl | 2007-11-24 03:42:14 -0800 (Sat, 24 Nov 2007) | 2 lines
#1355: remove mention of PyXML from xml.dom docs.
........
r59169 | amaury.forgeotdarc | 2007-11-24 05:20:22 -0800 (Sat, 24 Nov 2007) | 2 lines
Warning "<> not supported in 3.x" should be enabled only when the -3 option is set.
........
r59170 | amaury.forgeotdarc | 2007-11-24 05:44:17 -0800 (Sat, 24 Nov 2007) | 3 lines
Issue #1445: Fix a SystemError when accessing the ``cell_contents``
attribute of an empty cell object. Now a ValueError is raised.
........
r59172 | georg.brandl | 2007-11-24 05:56:09 -0800 (Sat, 24 Nov 2007) | 3 lines
#1735632: add O_NOATIME constant to os module.
Also document a few other O_ constants that were missing from documentation.
........
r59173 | skip.montanaro | 2007-11-24 06:30:47 -0800 (Sat, 24 Nov 2007) | 1 line
back in these go - thanks to Titus Brown for the fix
........
r59176 | martin.v.loewis | 2007-11-24 10:33:40 -0800 (Sat, 24 Nov 2007) | 2 lines
Bug #1494: Document that appendChild removes first.
........
r59186 | guido.van.rossum | 2007-11-26 14:16:49 -0800 (Mon, 26 Nov 2007) | 2 lines
A thread-less variant of brownian.py, submitted by Michele Simoniato.
........
2007-11-26 19:23:18 -04:00
|
|
|
Note that if you want to send data to the process's stdin, you need to create
|
|
|
|
the Popen object with ``stdin=PIPE``. Similarly, to get anything other than
|
|
|
|
``None`` in the result tuple, you need to give ``stdout=PIPE`` and/or
|
|
|
|
``stderr=PIPE`` too.
|
|
|
|
|
2011-03-14 13:02:10 -03:00
|
|
|
If the process does not terminate after *timeout* seconds, a
|
|
|
|
:exc:`TimeoutExpired` exception will be raised. Catching this exception and
|
|
|
|
retrying communication will not lose any output.
|
|
|
|
|
|
|
|
The child process is not killed if the timeout expires, so in order to
|
|
|
|
cleanup properly a well-behaved application should kill the child process and
|
|
|
|
finish communication::
|
|
|
|
|
|
|
|
proc = subprocess.Popen(...)
|
|
|
|
try:
|
|
|
|
outs, errs = proc.communicate(timeout=15)
|
|
|
|
except TimeoutExpired:
|
|
|
|
proc.kill()
|
|
|
|
outs, errs = proc.communicate()
|
|
|
|
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
.. note::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
The data read is buffered in memory, so do not use this method if the data
|
|
|
|
size is large or unlimited.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-03-14 13:36:53 -03:00
|
|
|
.. versionchanged:: 3.3
|
2011-03-14 13:02:10 -03:00
|
|
|
*timeout* was added.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
.. method:: Popen.send_signal(signal)
|
|
|
|
|
|
|
|
Sends the signal *signal* to the child.
|
|
|
|
|
2020-01-15 12:38:55 -04:00
|
|
|
Do nothing if the process completed.
|
|
|
|
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
.. note::
|
|
|
|
|
2010-04-12 14:16:38 -03:00
|
|
|
On Windows, SIGTERM is an alias for :meth:`terminate`. CTRL_C_EVENT and
|
2010-10-15 09:55:19 -03:00
|
|
|
CTRL_BREAK_EVENT can be sent to processes started with a *creationflags*
|
2022-10-18 10:46:18 -03:00
|
|
|
parameter which includes ``CREATE_NEW_PROCESS_GROUP``.
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: Popen.terminate()
|
|
|
|
|
2020-05-17 18:29:51 -03:00
|
|
|
Stop the child. On POSIX OSs the method sends SIGTERM to the
|
2010-10-06 07:11:56 -03:00
|
|
|
child. On Windows the Win32 API function :c:func:`TerminateProcess` is called
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
to stop the child.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: Popen.kill()
|
|
|
|
|
2020-05-17 18:29:51 -03:00
|
|
|
Kills the child. On POSIX OSs the function sends SIGKILL to the child.
|
Merged revisions 62386-62387,62389-62393,62396,62400-62402,62407,62409-62410,62412-62414,62418-62419 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r62386 | christian.heimes | 2008-04-19 04:23:57 +0200 (Sat, 19 Apr 2008) | 2 lines
Added kill, terminate and send_signal to subprocess.Popen
The bits and pieces for the Windows side were already in place. The POSIX side is trivial (as usual) and uses os.kill().
........
r62387 | georg.brandl | 2008-04-19 10:23:59 +0200 (Sat, 19 Apr 2008) | 2 lines
Fix-up docs for revision 62386.
........
r62389 | georg.brandl | 2008-04-19 18:57:43 +0200 (Sat, 19 Apr 2008) | 2 lines
#2369: clarify that copyfile() doesn't take a target directory.
........
r62390 | georg.brandl | 2008-04-19 18:58:28 +0200 (Sat, 19 Apr 2008) | 2 lines
#2634: clarify meaning of env parameter to spawn/exec*e.
........
r62391 | georg.brandl | 2008-04-19 18:58:49 +0200 (Sat, 19 Apr 2008) | 2 lines
#2633: clarify meaning of env parameter.
........
r62392 | georg.brandl | 2008-04-19 18:59:16 +0200 (Sat, 19 Apr 2008) | 2 lines
#2631: clarify IMPORT_NAME semantics.
........
r62393 | georg.brandl | 2008-04-19 19:00:14 +0200 (Sat, 19 Apr 2008) | 2 lines
:func: et al. should *not* include the parens.
........
r62396 | mark.dickinson | 2008-04-19 20:51:48 +0200 (Sat, 19 Apr 2008) | 5 lines
Additional tests for math.pow, and extra special-case
handling code in math.pow, in the hope of making all
tests pass on the alpha Tru64 buildbot.
........
r62400 | mark.dickinson | 2008-04-19 21:41:52 +0200 (Sat, 19 Apr 2008) | 3 lines
Additional special-case handling for math.pow.
Windows/VS2008 doesn't like (-1)**(+-inf).
........
r62401 | benjamin.peterson | 2008-04-19 21:47:34 +0200 (Sat, 19 Apr 2008) | 2 lines
Complete documentation for errors argument of io's open and TextIOWrapper
........
r62402 | mark.dickinson | 2008-04-19 22:31:16 +0200 (Sat, 19 Apr 2008) | 2 lines
Document updates to math and cmath modules.
........
r62407 | georg.brandl | 2008-04-19 23:28:38 +0200 (Sat, 19 Apr 2008) | 2 lines
Update template for newest Sphinx.
........
r62409 | mark.dickinson | 2008-04-19 23:35:35 +0200 (Sat, 19 Apr 2008) | 5 lines
Correct documentation for math.pow;
0**nan is nan, not 0. (But nan**0 and 1**nan are 1.)
Also fix minor typo: 'quite NaN' -> 'quiet NaN'
........
r62410 | mark.dickinson | 2008-04-19 23:49:22 +0200 (Sat, 19 Apr 2008) | 4 lines
Move asinh documentation to the proper place.
Remove meaningless 'in radians' from inverse
hyperbolic functions.
........
r62412 | mark.dickinson | 2008-04-20 03:22:30 +0200 (Sun, 20 Apr 2008) | 5 lines
Report additional diagnostic information in
test_math, to help track down debian-alpha
buildbot failure.
........
r62413 | mark.dickinson | 2008-04-20 03:39:24 +0200 (Sun, 20 Apr 2008) | 3 lines
FreeBSD doesn't follow C99 for modf(inf); so add explicit
special-value handling to math.modf code.
........
r62414 | mark.dickinson | 2008-04-20 06:13:13 +0200 (Sun, 20 Apr 2008) | 5 lines
Yet more explicit special case handling to make
math.pow behave on alpha Tru64. All IEEE 754
special values are now handled directly; only
the finite**finite case is handled by libm.
........
r62418 | mark.dickinson | 2008-04-20 18:13:17 +0200 (Sun, 20 Apr 2008) | 7 lines
Issue 2662: Initialize special value tables dynamically (i.e. when
cmath module is loaded) instead of statically. This fixes compile-time
problems on platforms where HUGE_VAL is an extern variable rather than
a constant.
Thanks Hirokazu Yamamoto for the patch.
........
r62419 | andrew.kuchling | 2008-04-20 18:54:02 +0200 (Sun, 20 Apr 2008) | 1 line
Move description of math module changes; various edits to description of cmath changes
........
2008-04-20 18:01:16 -03:00
|
|
|
On Windows :meth:`kill` is an alias for :meth:`terminate`.
|
|
|
|
|
|
|
|
|
2023-02-08 04:12:46 -04:00
|
|
|
The following attributes are also set by the class for you to access.
|
|
|
|
Reassigning them to new values is unsupported:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-04-29 15:33:23 -03:00
|
|
|
.. attribute:: Popen.args
|
|
|
|
|
|
|
|
The *args* argument as it was passed to :class:`Popen` -- a
|
|
|
|
sequence of program arguments or else a single string.
|
|
|
|
|
|
|
|
.. versionadded:: 3.3
|
Merged revisions 65437,65469,65476,65480,65502,65528,65539,65543,65558,65561-65562,65565,65591,65601,65608,65610,65639 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65437 | georg.brandl | 2008-08-03 22:28:55 +0000 (Sun, 03 Aug 2008) | 2 lines
Note the removal of several committers.
........
r65469 | gregory.p.smith | 2008-08-04 01:03:50 +0000 (Mon, 04 Aug 2008) | 3 lines
issue1606: Add warnings to the subprocess documentation about common pitfalls
of using pipes that cause deadlocks.
........
r65476 | georg.brandl | 2008-08-04 06:29:36 +0000 (Mon, 04 Aug 2008) | 2 lines
Fix markup.
........
r65480 | georg.brandl | 2008-08-04 07:31:50 +0000 (Mon, 04 Aug 2008) | 3 lines
Clarify the meaning of the select() parameters and sync
names with docstring.
........
r65502 | gregory.p.smith | 2008-08-04 18:34:07 +0000 (Mon, 04 Aug 2008) | 2 lines
more cleanup ups of the recently added warnings in the subprocess docs.
........
r65528 | brett.cannon | 2008-08-04 21:52:25 +0000 (Mon, 04 Aug 2008) | 4 lines
Add a note about all the modules/packages changed to silence -3 warnings. More
changes are needed once some decisions are made, but this is the work up to this
point.
........
r65539 | andrew.kuchling | 2008-08-05 01:38:08 +0000 (Tue, 05 Aug 2008) | 6 lines
#3367 from Kristjan Valur Jonsson:
If a PyTokenizer_FromString() is called with an empty string, the
tokenizer's line_start member never gets initialized. Later, it is
compared with the token pointer 'a' in parsetok.c:193 and that behavior
can result in undefined behavior.
........
r65543 | andrew.kuchling | 2008-08-05 02:05:23 +0000 (Tue, 05 Aug 2008) | 1 line
#3367: revert rev. 65539: this change causes test_parser to fail
........
r65558 | georg.brandl | 2008-08-06 17:20:41 +0000 (Wed, 06 Aug 2008) | 2 lines
Fix longstringitem definition. #3505.
........
r65561 | mark.dickinson | 2008-08-06 20:12:30 +0000 (Wed, 06 Aug 2008) | 2 lines
Docstring typo
........
r65562 | mark.dickinson | 2008-08-06 21:36:57 +0000 (Wed, 06 Aug 2008) | 2 lines
Remove duplicate import
........
r65565 | andrew.kuchling | 2008-08-07 01:47:34 +0000 (Thu, 07 Aug 2008) | 1 line
Add some items
........
r65591 | georg.brandl | 2008-08-08 06:42:20 +0000 (Fri, 08 Aug 2008) | 2 lines
#3519: callee is an expression too.
........
r65601 | georg.brandl | 2008-08-08 15:34:34 +0000 (Fri, 08 Aug 2008) | 2 lines
Remove mention of backquotes in the tutorial.
........
r65608 | guido.van.rossum | 2008-08-09 14:55:34 +0000 (Sat, 09 Aug 2008) | 2 lines
Add news item about _sre.compile() re-bytecode validator.
........
r65610 | antoine.pitrou | 2008-08-09 17:27:23 +0000 (Sat, 09 Aug 2008) | 3 lines
move NEWS entry to the appropriate section (oops!)
........
r65639 | georg.brandl | 2008-08-11 10:27:31 +0000 (Mon, 11 Aug 2008) | 2 lines
#3540: fix exception name.
........
2008-08-12 05:18:18 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. attribute:: Popen.stdin
|
|
|
|
|
2014-01-18 01:45:56 -04:00
|
|
|
If the *stdin* argument was :data:`PIPE`, this attribute is a writeable
|
2016-09-07 00:16:17 -03:00
|
|
|
stream object as returned by :func:`open`. If the *encoding* or *errors*
|
2023-02-08 04:12:46 -04:00
|
|
|
arguments were specified or the *text* or *universal_newlines* argument
|
|
|
|
was ``True``, the stream is a text stream, otherwise it is a byte stream.
|
|
|
|
If the *stdin* argument was not :data:`PIPE`, this attribute is ``None``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. attribute:: Popen.stdout
|
|
|
|
|
2014-01-18 01:45:56 -04:00
|
|
|
If the *stdout* argument was :data:`PIPE`, this attribute is a readable
|
|
|
|
stream object as returned by :func:`open`. Reading from the stream provides
|
2016-09-07 00:16:17 -03:00
|
|
|
output from the child process. If the *encoding* or *errors* arguments were
|
2023-02-08 04:12:46 -04:00
|
|
|
specified or the *text* or *universal_newlines* argument was ``True``, the
|
|
|
|
stream is a text stream, otherwise it is a byte stream. If the *stdout*
|
|
|
|
argument was not :data:`PIPE`, this attribute is ``None``.
|
2014-01-18 01:49:04 -04:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. attribute:: Popen.stderr
|
|
|
|
|
2014-01-18 01:45:56 -04:00
|
|
|
If the *stderr* argument was :data:`PIPE`, this attribute is a readable
|
|
|
|
stream object as returned by :func:`open`. Reading from the stream provides
|
2016-09-07 00:16:17 -03:00
|
|
|
error output from the child process. If the *encoding* or *errors* arguments
|
2023-02-08 04:12:46 -04:00
|
|
|
were specified or the *text* or *universal_newlines* argument was ``True``, the
|
|
|
|
stream is a text stream, otherwise it is a byte stream. If the *stderr* argument
|
|
|
|
was not :data:`PIPE`, this attribute is ``None``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2014-05-11 17:26:21 -03:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Use :meth:`~Popen.communicate` rather than :attr:`.stdin.write <Popen.stdin>`,
|
|
|
|
:attr:`.stdout.read <Popen.stdout>` or :attr:`.stderr.read <Popen.stderr>` to avoid
|
|
|
|
deadlocks due to any of the other OS pipe buffers filling up and blocking the
|
|
|
|
child process.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. attribute:: Popen.pid
|
|
|
|
|
|
|
|
The process ID of the child process.
|
|
|
|
|
2010-03-21 06:50:49 -03:00
|
|
|
Note that if you set the *shell* argument to ``True``, this is the process ID
|
|
|
|
of the spawned shell.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. attribute:: Popen.returncode
|
|
|
|
|
2023-04-24 13:46:12 -03:00
|
|
|
The child return code. Initially ``None``, :attr:`returncode` is set by
|
|
|
|
a call to the :meth:`poll`, :meth:`wait`, or :meth:`communicate` methods
|
|
|
|
if they detect that the process has terminated.
|
|
|
|
|
|
|
|
A ``None`` value indicates that the process hadn't yet terminated at the
|
|
|
|
time of the last method call.
|
2009-01-03 17:18:54 -04:00
|
|
|
|
Merged revisions 59774-59783 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59774 | georg.brandl | 2008-01-06 16:41:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1501: document that 0**0 == 1.
........
r59775 | georg.brandl | 2008-01-06 16:48:20 +0100 (Sun, 06 Jan 2008) | 2 lines
#759525: document that dir() doesn't return metaclass attrs when given a class as arg.
........
r59776 | georg.brandl | 2008-01-06 16:55:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1615275: clarify return object types of different tempfile factories.
........
r59777 | georg.brandl | 2008-01-06 17:01:26 +0100 (Sun, 06 Jan 2008) | 2 lines
#1727024: document that Popen.returncode is set by Popen.poll/wait.
........
r59778 | georg.brandl | 2008-01-06 17:04:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1686390: add example for csv.Sniffer use.
........
r59779 | georg.brandl | 2008-01-06 17:12:39 +0100 (Sun, 06 Jan 2008) | 2 lines
#1559684: document that shutil.copy* doesn't copy all metadata on Posix and Windows too.
........
r59780 | georg.brandl | 2008-01-06 17:17:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1582: document __reversed__, patch by Mark Russell.
........
r59781 | georg.brandl | 2008-01-06 17:22:56 +0100 (Sun, 06 Jan 2008) | 2 lines
#1499: Document compile() exceptions.
........
r59782 | georg.brandl | 2008-01-06 17:49:50 +0100 (Sun, 06 Jan 2008) | 2 lines
#1325: Add docs and tests for zipimporter.archive and zipimporter.prefix.
........
2008-01-06 13:05:40 -04:00
|
|
|
A negative value ``-N`` indicates that the child was terminated by signal
|
2014-05-11 17:28:35 -03:00
|
|
|
``N`` (POSIX only).
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2011-04-30 00:17:51 -03:00
|
|
|
Windows Popen Helpers
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
The :class:`STARTUPINFO` class and following constants are only available
|
|
|
|
on Windows.
|
|
|
|
|
2017-03-01 05:51:55 -04:00
|
|
|
.. class:: STARTUPINFO(*, dwFlags=0, hStdInput=None, hStdOutput=None, \
|
2017-12-18 05:28:19 -04:00
|
|
|
hStdError=None, wShowWindow=0, lpAttributeList=None)
|
2011-04-30 00:18:33 -03:00
|
|
|
|
2011-04-30 00:17:51 -03:00
|
|
|
Partial support of the Windows
|
2016-02-26 14:37:12 -04:00
|
|
|
`STARTUPINFO <https://msdn.microsoft.com/en-us/library/ms686331(v=vs.85).aspx>`__
|
2017-03-01 05:51:55 -04:00
|
|
|
structure is used for :class:`Popen` creation. The following attributes can
|
|
|
|
be set by passing them as keyword-only arguments.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.7
|
|
|
|
Keyword-only argument support was added.
|
2011-04-30 00:17:51 -03:00
|
|
|
|
|
|
|
.. attribute:: dwFlags
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
A bit field that determines whether certain :class:`STARTUPINFO`
|
|
|
|
attributes are used when the process creates a window. ::
|
2011-04-30 00:17:51 -03:00
|
|
|
|
|
|
|
si = subprocess.STARTUPINFO()
|
|
|
|
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW
|
|
|
|
|
|
|
|
.. attribute:: hStdInput
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
If :attr:`dwFlags` specifies :data:`STARTF_USESTDHANDLES`, this attribute
|
|
|
|
is the standard input handle for the process. If
|
|
|
|
:data:`STARTF_USESTDHANDLES` is not specified, the default for standard
|
|
|
|
input is the keyboard buffer.
|
2011-04-30 00:17:51 -03:00
|
|
|
|
|
|
|
.. attribute:: hStdOutput
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
If :attr:`dwFlags` specifies :data:`STARTF_USESTDHANDLES`, this attribute
|
|
|
|
is the standard output handle for the process. Otherwise, this attribute
|
|
|
|
is ignored and the default for standard output is the console window's
|
2011-04-30 00:17:51 -03:00
|
|
|
buffer.
|
|
|
|
|
|
|
|
.. attribute:: hStdError
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
If :attr:`dwFlags` specifies :data:`STARTF_USESTDHANDLES`, this attribute
|
|
|
|
is the standard error handle for the process. Otherwise, this attribute is
|
2011-04-30 00:17:51 -03:00
|
|
|
ignored and the default for standard error is the console window's buffer.
|
|
|
|
|
|
|
|
.. attribute:: wShowWindow
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
If :attr:`dwFlags` specifies :data:`STARTF_USESHOWWINDOW`, this attribute
|
2011-04-30 00:17:51 -03:00
|
|
|
can be any of the values that can be specified in the ``nCmdShow``
|
|
|
|
parameter for the
|
2016-02-26 14:37:12 -04:00
|
|
|
`ShowWindow <https://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx>`__
|
2011-07-04 15:28:30 -03:00
|
|
|
function, except for ``SW_SHOWDEFAULT``. Otherwise, this attribute is
|
2011-04-30 00:17:51 -03:00
|
|
|
ignored.
|
2011-04-30 00:18:33 -03:00
|
|
|
|
2011-04-30 00:17:51 -03:00
|
|
|
:data:`SW_HIDE` is provided for this attribute. It is used when
|
|
|
|
:class:`Popen` is called with ``shell=True``.
|
|
|
|
|
2017-12-18 05:28:19 -04:00
|
|
|
.. attribute:: lpAttributeList
|
|
|
|
|
|
|
|
A dictionary of additional attributes for process creation as given in
|
|
|
|
``STARTUPINFOEX``, see
|
|
|
|
`UpdateProcThreadAttribute <https://msdn.microsoft.com/en-us/library/windows/desktop/ms686880(v=vs.85).aspx>`__.
|
|
|
|
|
|
|
|
Supported attributes:
|
|
|
|
|
|
|
|
**handle_list**
|
|
|
|
Sequence of handles that will be inherited. *close_fds* must be true if
|
|
|
|
non-empty.
|
|
|
|
|
|
|
|
The handles must be temporarily made inheritable by
|
|
|
|
:func:`os.set_handle_inheritable` when passed to the :class:`Popen`
|
|
|
|
constructor, else :class:`OSError` will be raised with Windows error
|
|
|
|
``ERROR_INVALID_PARAMETER`` (87).
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
In a multithreaded process, use caution to avoid leaking handles
|
|
|
|
that are marked inheritable when combining this feature with
|
|
|
|
concurrent calls to other process creation functions that inherit
|
|
|
|
all handles such as :func:`os.system`. This also applies to
|
|
|
|
standard handle redirection, which temporarily creates inheritable
|
|
|
|
handles.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
2011-04-30 00:17:51 -03:00
|
|
|
|
2017-11-08 10:18:59 -04:00
|
|
|
Windows Constants
|
|
|
|
^^^^^^^^^^^^^^^^^
|
2011-04-30 00:17:51 -03:00
|
|
|
|
|
|
|
The :mod:`subprocess` module exposes the following constants.
|
|
|
|
|
|
|
|
.. data:: STD_INPUT_HANDLE
|
|
|
|
|
|
|
|
The standard input device. Initially, this is the console input buffer,
|
|
|
|
``CONIN$``.
|
|
|
|
|
|
|
|
.. data:: STD_OUTPUT_HANDLE
|
|
|
|
|
|
|
|
The standard output device. Initially, this is the active console screen
|
|
|
|
buffer, ``CONOUT$``.
|
|
|
|
|
|
|
|
.. data:: STD_ERROR_HANDLE
|
|
|
|
|
|
|
|
The standard error device. Initially, this is the active console screen
|
|
|
|
buffer, ``CONOUT$``.
|
|
|
|
|
|
|
|
.. data:: SW_HIDE
|
|
|
|
|
|
|
|
Hides the window. Another window will be activated.
|
|
|
|
|
|
|
|
.. data:: STARTF_USESTDHANDLES
|
|
|
|
|
|
|
|
Specifies that the :attr:`STARTUPINFO.hStdInput`,
|
2011-07-04 15:28:30 -03:00
|
|
|
:attr:`STARTUPINFO.hStdOutput`, and :attr:`STARTUPINFO.hStdError` attributes
|
2011-04-30 00:17:51 -03:00
|
|
|
contain additional information.
|
|
|
|
|
|
|
|
.. data:: STARTF_USESHOWWINDOW
|
|
|
|
|
2011-07-04 15:28:30 -03:00
|
|
|
Specifies that the :attr:`STARTUPINFO.wShowWindow` attribute contains
|
2011-04-30 00:17:51 -03:00
|
|
|
additional information.
|
|
|
|
|
|
|
|
.. data:: CREATE_NEW_CONSOLE
|
|
|
|
|
|
|
|
The new process has a new console, instead of inheriting its parent's
|
|
|
|
console (the default).
|
2011-04-30 00:18:33 -03:00
|
|
|
|
2011-04-30 00:20:57 -03:00
|
|
|
.. data:: CREATE_NEW_PROCESS_GROUP
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
group will be created. This flag is necessary for using :func:`os.kill`
|
|
|
|
on the subprocess.
|
|
|
|
|
|
|
|
This flag is ignored if :data:`CREATE_NEW_CONSOLE` is specified.
|
|
|
|
|
2017-11-08 10:18:59 -04:00
|
|
|
.. data:: ABOVE_NORMAL_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have an above average priority.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: BELOW_NORMAL_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have a below average priority.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: HIGH_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have a high priority.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: IDLE_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have an idle (lowest) priority.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: NORMAL_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have an normal priority. (default)
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: REALTIME_PRIORITY_CLASS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will have realtime priority.
|
|
|
|
You should almost never use REALTIME_PRIORITY_CLASS, because this interrupts
|
|
|
|
system threads that manage mouse input, keyboard input, and background disk
|
|
|
|
flushing. This class can be appropriate for applications that "talk" directly
|
|
|
|
to hardware or that perform brief tasks that should have limited interruptions.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: CREATE_NO_WINDOW
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
2019-03-04 14:12:04 -04:00
|
|
|
will not create a window.
|
2017-11-08 10:18:59 -04:00
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: DETACHED_PROCESS
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
will not inherit its parent's console.
|
|
|
|
This value cannot be used with CREATE_NEW_CONSOLE.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: CREATE_DEFAULT_ERROR_MODE
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
does not inherit the error mode of the calling process. Instead, the new
|
|
|
|
process gets the default error mode.
|
|
|
|
This feature is particularly useful for multithreaded shell applications
|
|
|
|
that run with hard errors disabled.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
|
|
|
.. data:: CREATE_BREAKAWAY_FROM_JOB
|
|
|
|
|
|
|
|
A :class:`Popen` ``creationflags`` parameter to specify that a new process
|
|
|
|
is not associated with the job.
|
|
|
|
|
|
|
|
.. versionadded:: 3.7
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. _call-function-trio:
|
|
|
|
|
|
|
|
Older high-level API
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Prior to Python 3.5, these three functions comprised the high level API to
|
|
|
|
subprocess. You can now use :func:`run` in many cases, but lots of existing code
|
|
|
|
calls these functions.
|
|
|
|
|
2020-05-17 07:52:47 -03:00
|
|
|
.. function:: call(args, *, stdin=None, stdout=None, stderr=None, \
|
|
|
|
shell=False, cwd=None, timeout=None, **other_popen_kwargs)
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
Run the command described by *args*. Wait for command to complete, then
|
2015-07-25 08:27:07 -03:00
|
|
|
return the :attr:`~Popen.returncode` attribute.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2019-09-11 08:26:31 -03:00
|
|
|
Code needing to capture stdout or stderr should use :func:`run` instead::
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
run(...).returncode
|
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
To suppress stdout or stderr, supply a value of :data:`DEVNULL`.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
The arguments shown above are merely some common ones.
|
|
|
|
The full function signature is the
|
2015-07-25 08:27:07 -03:00
|
|
|
same as that of the :class:`Popen` constructor - this function passes all
|
|
|
|
supplied arguments other than *timeout* directly through to that interface.
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Do not use ``stdout=PIPE`` or ``stderr=PIPE`` with this
|
|
|
|
function. The child process will block if it generates enough
|
|
|
|
output to a pipe to fill up the OS pipe buffer as the pipes are
|
|
|
|
not being read from.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
*timeout* was added.
|
|
|
|
|
2023-02-09 13:36:24 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-02-08 18:12:19 -04:00
|
|
|
|
|
|
|
Changed Windows shell search order for ``shell=True``. The current
|
|
|
|
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
|
|
|
|
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
|
|
|
|
malicious program named ``cmd.exe`` into a current directory no
|
|
|
|
longer works.
|
|
|
|
|
2020-05-17 07:52:47 -03:00
|
|
|
.. function:: check_call(args, *, stdin=None, stdout=None, stderr=None, \
|
|
|
|
shell=False, cwd=None, timeout=None, \
|
|
|
|
**other_popen_kwargs)
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
Run command with arguments. Wait for command to complete. If the return
|
|
|
|
code was zero then return, otherwise raise :exc:`CalledProcessError`. The
|
|
|
|
:exc:`CalledProcessError` object will have the return code in the
|
|
|
|
:attr:`~CalledProcessError.returncode` attribute.
|
2021-09-07 15:16:44 -03:00
|
|
|
If :func:`check_call` was unable to start the process it will propagate the exception
|
|
|
|
that was raised.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2019-09-11 08:26:31 -03:00
|
|
|
Code needing to capture stdout or stderr should use :func:`run` instead::
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
run(..., check=True)
|
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
To suppress stdout or stderr, supply a value of :data:`DEVNULL`.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
The arguments shown above are merely some common ones.
|
|
|
|
The full function signature is the
|
2015-07-25 08:27:07 -03:00
|
|
|
same as that of the :class:`Popen` constructor - this function passes all
|
|
|
|
supplied arguments other than *timeout* directly through to that interface.
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Do not use ``stdout=PIPE`` or ``stderr=PIPE`` with this
|
|
|
|
function. The child process will block if it generates enough
|
|
|
|
output to a pipe to fill up the OS pipe buffer as the pipes are
|
|
|
|
not being read from.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
*timeout* was added.
|
|
|
|
|
2023-02-09 13:36:24 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-02-08 18:12:19 -04:00
|
|
|
|
|
|
|
Changed Windows shell search order for ``shell=True``. The current
|
|
|
|
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
|
|
|
|
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
|
|
|
|
malicious program named ``cmd.exe`` into a current directory no
|
|
|
|
longer works.
|
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
|
2016-09-07 00:16:17 -03:00
|
|
|
.. function:: check_output(args, *, stdin=None, stderr=None, shell=False, \
|
2017-05-25 23:28:17 -03:00
|
|
|
cwd=None, encoding=None, errors=None, \
|
2020-05-17 07:52:47 -03:00
|
|
|
universal_newlines=None, timeout=None, text=None, \
|
|
|
|
**other_popen_kwargs)
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
Run command with arguments and return its output.
|
|
|
|
|
|
|
|
If the return code was non-zero it raises a :exc:`CalledProcessError`. The
|
|
|
|
:exc:`CalledProcessError` object will have the return code in the
|
|
|
|
:attr:`~CalledProcessError.returncode` attribute and any output in the
|
|
|
|
:attr:`~CalledProcessError.output` attribute.
|
|
|
|
|
|
|
|
This is equivalent to::
|
|
|
|
|
|
|
|
run(..., check=True, stdout=PIPE).stdout
|
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
The arguments shown above are merely some common ones.
|
2015-07-25 08:27:07 -03:00
|
|
|
The full function signature is largely the same as that of :func:`run` -
|
|
|
|
most arguments are passed directly through to that interface.
|
2020-12-25 00:57:21 -04:00
|
|
|
One API deviation from :func:`run` behavior exists: passing ``input=None``
|
|
|
|
will behave the same as ``input=b''`` (or ``input=''``, depending on other
|
|
|
|
arguments) rather than using the parent's standard input file handle.
|
2015-07-25 08:27:07 -03:00
|
|
|
|
2015-04-14 20:14:25 -03:00
|
|
|
By default, this function will return the data as encoded bytes. The actual
|
|
|
|
encoding of the output data may depend on the command being invoked, so the
|
|
|
|
decoding to text will often need to be handled at the application level.
|
|
|
|
|
2019-03-23 04:40:28 -03:00
|
|
|
This behaviour may be overridden by setting *text*, *encoding*, *errors*,
|
|
|
|
or *universal_newlines* to ``True`` as described in
|
|
|
|
:ref:`frequently-used-arguments` and :func:`run`.
|
2015-04-14 20:14:25 -03:00
|
|
|
|
|
|
|
To also capture standard error in the result, use
|
|
|
|
``stderr=subprocess.STDOUT``::
|
|
|
|
|
|
|
|
>>> subprocess.check_output(
|
|
|
|
... "ls non_existent_file; exit 0",
|
|
|
|
... stderr=subprocess.STDOUT,
|
|
|
|
... shell=True)
|
|
|
|
'ls: non_existent_file: No such file or directory\n'
|
|
|
|
|
|
|
|
.. versionadded:: 3.1
|
|
|
|
|
|
|
|
.. versionchanged:: 3.3
|
|
|
|
*timeout* was added.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.4
|
2015-07-25 08:27:07 -03:00
|
|
|
Support for the *input* keyword argument was added.
|
2011-04-30 00:17:51 -03:00
|
|
|
|
2018-02-06 20:46:29 -04:00
|
|
|
.. versionchanged:: 3.6
|
|
|
|
*encoding* and *errors* were added. See :func:`run` for details.
|
|
|
|
|
2018-11-11 23:40:42 -04:00
|
|
|
.. versionadded:: 3.7
|
|
|
|
*text* was added as a more readable alias for *universal_newlines*.
|
|
|
|
|
2023-02-09 13:36:24 -04:00
|
|
|
.. versionchanged:: 3.12
|
2023-02-08 18:12:19 -04:00
|
|
|
|
|
|
|
Changed Windows shell search order for ``shell=True``. The current
|
|
|
|
directory and ``%PATH%`` are replaced with ``%COMSPEC%`` and
|
|
|
|
``%SystemRoot%\System32\cmd.exe``. As a result, dropping a
|
|
|
|
malicious program named ``cmd.exe`` into a current directory no
|
|
|
|
longer works.
|
|
|
|
|
2018-11-11 23:40:42 -04:00
|
|
|
|
Merged revisions 64434-64435,64440-64443,64445,64447-64448,64450,64452,64455,64461,64464,64466,64468 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r64434 | andrew.kuchling | 2008-06-20 18:13:58 -0500 (Fri, 20 Jun 2008) | 1 line
Remove request for e-mail; it's unlikely these classes will be saved
........
r64435 | andrew.kuchling | 2008-06-20 18:14:32 -0500 (Fri, 20 Jun 2008) | 1 line
Grammar fixes
........
r64440 | andrew.kuchling | 2008-06-21 08:29:12 -0500 (Sat, 21 Jun 2008) | 1 line
Docstring typo
........
r64441 | andrew.kuchling | 2008-06-21 08:47:20 -0500 (Sat, 21 Jun 2008) | 1 line
Use repr() for bad input strings; this makes the empty string or binary characters more visible
........
r64442 | andrew.kuchling | 2008-06-21 08:48:38 -0500 (Sat, 21 Jun 2008) | 1 line
Docstring correction
........
r64443 | georg.brandl | 2008-06-21 09:26:19 -0500 (Sat, 21 Jun 2008) | 2 lines
Documentation fix.
........
r64445 | facundo.batista | 2008-06-21 12:30:06 -0500 (Sat, 21 Jun 2008) | 3 lines
Reviewed and updated the documentation. Fixes #3017.
........
r64447 | facundo.batista | 2008-06-21 13:58:04 -0500 (Sat, 21 Jun 2008) | 6 lines
Now a from submitted via POST that also has a query string
will contain both FieldStorage and MiniFieldStorage items.
Fixes #1817.
........
r64448 | facundo.batista | 2008-06-21 14:48:19 -0500 (Sat, 21 Jun 2008) | 5 lines
In the deprecated functions I added an alert to review
specially a section of the subprocess documentation
that helps with the replacing of those functionss.
........
r64450 | georg.brandl | 2008-06-22 04:05:29 -0500 (Sun, 22 Jun 2008) | 2 lines
Turn section references into proper cross-references.
........
r64452 | facundo.batista | 2008-06-22 08:36:20 -0500 (Sun, 22 Jun 2008) | 5 lines
Issue #2722. Now the char buffer to support the path string has
not fixed length, it mallocs memory if needed. As a result, we
don't have a maximum for the getcwd() method.
........
r64455 | facundo.batista | 2008-06-22 10:27:10 -0500 (Sun, 22 Jun 2008) | 4 lines
Issue 3164. Small fix to don't repeat a comparation
without necessity.
........
r64461 | georg.brandl | 2008-06-22 13:11:52 -0500 (Sun, 22 Jun 2008) | 2 lines
#3085: Fix syntax error.
........
r64464 | georg.brandl | 2008-06-22 13:31:54 -0500 (Sun, 22 Jun 2008) | 2 lines
Expand docstrings of sqlite3 functions.
........
r64466 | georg.brandl | 2008-06-22 14:07:59 -0500 (Sun, 22 Jun 2008) | 2 lines
Write out "phi" consistently.
........
r64468 | facundo.batista | 2008-06-22 14:35:24 -0500 (Sun, 22 Jun 2008) | 4 lines
Just returning nothing instead of rising TestSkipped, because
it makes the test fail in the trunk.loewis-sun buildbot.
........
2008-07-02 14:30:14 -03:00
|
|
|
.. _subprocess-replacements:
|
|
|
|
|
2012-11-08 04:07:10 -04:00
|
|
|
Replacing Older Functions with the :mod:`subprocess` Module
|
|
|
|
-----------------------------------------------------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
In this section, "a becomes b" means that b can be used as a replacement for a.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
All "a" functions in this section fail (more or less) silently if the
|
|
|
|
executed program cannot be found; the "b" replacements raise :exc:`OSError`
|
|
|
|
instead.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
In addition, the replacements using :func:`check_output` will fail with a
|
|
|
|
:exc:`CalledProcessError` if the requested operation produces a non-zero
|
2013-08-23 18:23:38 -03:00
|
|
|
return code. The output is still available as the
|
|
|
|
:attr:`~CalledProcessError.output` attribute of the raised exception.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
In the following examples, we assume that the relevant functions have already
|
2012-11-08 04:07:10 -04:00
|
|
|
been imported from the :mod:`subprocess` module.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2019-07-16 11:55:19 -03:00
|
|
|
Replacing :program:`/bin/sh` shell command substitution
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-07-26 06:18:21 -03:00
|
|
|
.. code-block:: bash
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-07-16 11:55:19 -03:00
|
|
|
output=$(mycmd myarg)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-07-26 06:18:21 -03:00
|
|
|
becomes::
|
|
|
|
|
|
|
|
output = check_output(["mycmd", "myarg"])
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-10-10 21:49:57 -03:00
|
|
|
Replacing shell pipeline
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-07-26 06:18:21 -03:00
|
|
|
.. code-block:: bash
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-07-16 11:55:19 -03:00
|
|
|
output=$(dmesg | grep hda)
|
2016-07-26 06:18:21 -03:00
|
|
|
|
|
|
|
becomes::
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
p1 = Popen(["dmesg"], stdout=PIPE)
|
|
|
|
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
|
2011-02-05 17:47:25 -04:00
|
|
|
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
|
2007-08-15 11:28:22 -03:00
|
|
|
output = p2.communicate()[0]
|
|
|
|
|
2019-09-11 08:26:31 -03:00
|
|
|
The ``p1.stdout.close()`` call after starting the p2 is important in order for
|
|
|
|
p1 to receive a SIGPIPE if p2 exits before p1.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
Alternatively, for trusted input, the shell's own pipeline support may still
|
2016-07-26 06:18:21 -03:00
|
|
|
be used directly:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2019-07-16 11:55:19 -03:00
|
|
|
output=$(dmesg | grep hda)
|
2016-07-26 06:18:21 -03:00
|
|
|
|
|
|
|
becomes::
|
|
|
|
|
2020-12-13 01:27:22 -04:00
|
|
|
output = check_output("dmesg | grep hda", shell=True)
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
Replacing :func:`os.system`
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
sts = os.system("mycmd" + " myarg")
|
2011-11-08 06:49:23 -04:00
|
|
|
# becomes
|
2021-05-11 17:47:05 -03:00
|
|
|
retcode = call("mycmd" + " myarg", shell=True)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
* Calling the program through the shell is usually not required.
|
2021-05-11 17:47:05 -03:00
|
|
|
* The :func:`call` return value is encoded differently to that of
|
|
|
|
:func:`os.system`.
|
|
|
|
|
|
|
|
* The :func:`os.system` function ignores SIGINT and SIGQUIT signals while
|
|
|
|
the command is running, but the caller must do this separately when
|
|
|
|
using the :mod:`subprocess` module.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
A more realistic example would look like this::
|
|
|
|
|
|
|
|
try:
|
|
|
|
retcode = call("mycmd" + " myarg", shell=True)
|
|
|
|
if retcode < 0:
|
2007-09-01 20:34:30 -03:00
|
|
|
print("Child was terminated by signal", -retcode, file=sys.stderr)
|
2007-08-15 11:28:22 -03:00
|
|
|
else:
|
2007-09-01 20:34:30 -03:00
|
|
|
print("Child returned", retcode, file=sys.stderr)
|
2007-08-15 11:28:22 -03:00
|
|
|
except OSError as e:
|
2007-09-01 20:34:30 -03:00
|
|
|
print("Execution failed:", e, file=sys.stderr)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
Replacing the :func:`os.spawn <os.spawnl>` family
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
P_NOWAIT example::
|
|
|
|
|
|
|
|
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
|
|
|
|
==>
|
|
|
|
pid = Popen(["/bin/mycmd", "myarg"]).pid
|
|
|
|
|
|
|
|
P_WAIT example::
|
|
|
|
|
|
|
|
retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
|
|
|
|
==>
|
|
|
|
retcode = call(["/bin/mycmd", "myarg"])
|
|
|
|
|
|
|
|
Vector example::
|
|
|
|
|
|
|
|
os.spawnvp(os.P_NOWAIT, path, args)
|
|
|
|
==>
|
|
|
|
Popen([path] + args[1:])
|
|
|
|
|
|
|
|
Environment example::
|
|
|
|
|
|
|
|
os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
|
|
|
|
==>
|
|
|
|
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
|
|
|
|
|
|
|
|
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
|
|
|
|
Replacing :func:`os.popen`, :func:`os.popen2`, :func:`os.popen3`
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
(child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
|
2007-08-15 11:28:22 -03:00
|
|
|
==>
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
p = Popen(cmd, shell=True, bufsize=bufsize,
|
|
|
|
stdin=PIPE, stdout=PIPE, close_fds=True)
|
|
|
|
(child_stdin, child_stdout) = (p.stdin, p.stdout)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
(child_stdin,
|
|
|
|
child_stdout,
|
|
|
|
child_stderr) = os.popen3(cmd, mode, bufsize)
|
|
|
|
==>
|
|
|
|
p = Popen(cmd, shell=True, bufsize=bufsize,
|
|
|
|
stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
|
|
|
|
(child_stdin,
|
|
|
|
child_stdout,
|
|
|
|
child_stderr) = (p.stdin, p.stdout, p.stderr)
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
(child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
|
|
|
|
==>
|
|
|
|
p = Popen(cmd, shell=True, bufsize=bufsize,
|
|
|
|
stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
|
|
|
|
(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)
|
|
|
|
|
|
|
|
Return code handling translates as follows::
|
|
|
|
|
|
|
|
pipe = os.popen(cmd, 'w')
|
|
|
|
...
|
|
|
|
rc = pipe.close()
|
2010-07-14 07:16:11 -03:00
|
|
|
if rc is not None and rc >> 8:
|
2009-09-13 04:54:02 -03:00
|
|
|
print("There were some errors")
|
2007-08-15 11:28:22 -03:00
|
|
|
==>
|
2015-09-04 11:01:19 -03:00
|
|
|
process = Popen(cmd, stdin=PIPE)
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
...
|
|
|
|
process.stdin.close()
|
|
|
|
if process.wait() != 0:
|
2009-09-13 04:54:02 -03:00
|
|
|
print("There were some errors")
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
|
|
|
|
|
|
|
|
Replacing functions from the :mod:`popen2` module
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
If the cmd argument to popen2 functions is a string, the command is executed
|
|
|
|
through /bin/sh. If it is a list, the command is directly executed.
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
|
|
|
|
==>
|
2014-05-14 11:09:52 -03:00
|
|
|
p = Popen("somestring", shell=True, bufsize=bufsize,
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
stdin=PIPE, stdout=PIPE, close_fds=True)
|
|
|
|
(child_stdout, child_stdin) = (p.stdout, p.stdin)
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
|
|
|
|
==>
|
|
|
|
p = Popen(["mycmd", "myarg"], bufsize=bufsize,
|
|
|
|
stdin=PIPE, stdout=PIPE, close_fds=True)
|
|
|
|
(child_stdout, child_stdin) = (p.stdout, p.stdin)
|
|
|
|
|
|
|
|
:class:`popen2.Popen3` and :class:`popen2.Popen4` basically work as
|
|
|
|
:class:`subprocess.Popen`, except that:
|
|
|
|
|
|
|
|
* :class:`Popen` raises an exception if the execution fails.
|
|
|
|
|
2019-03-04 14:12:04 -04:00
|
|
|
* The *capturestderr* argument is replaced with the *stderr* argument.
|
Merged revisions 73196,73278-73280,73299,73308,73312-73313,73317-73318,73321,73324,73331,73335,73340,73363 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r73196 | benjamin.peterson | 2009-06-03 20:40:29 -0500 (Wed, 03 Jun 2009) | 1 line
use the offical api
........
r73278 | benjamin.peterson | 2009-06-07 17:33:11 -0500 (Sun, 07 Jun 2009) | 1 line
inherit from object
........
r73279 | benjamin.peterson | 2009-06-07 17:35:00 -0500 (Sun, 07 Jun 2009) | 1 line
always inherit from an appropiate base class
........
r73280 | benjamin.peterson | 2009-06-07 17:54:35 -0500 (Sun, 07 Jun 2009) | 1 line
use booleans for flags
........
r73299 | georg.brandl | 2009-06-08 13:41:36 -0500 (Mon, 08 Jun 2009) | 1 line
Typo fix.
........
r73308 | benjamin.peterson | 2009-06-08 17:18:32 -0500 (Mon, 08 Jun 2009) | 1 line
remove useless assertion
........
r73312 | benjamin.peterson | 2009-06-08 18:44:13 -0500 (Mon, 08 Jun 2009) | 1 line
remove error checks already done in set_context()
........
r73313 | r.david.murray | 2009-06-08 19:44:22 -0500 (Mon, 08 Jun 2009) | 4 lines
Issue 2947: document how return code handling translates from
os.popen to subprocess. Also fixes reference link in the
os.spawn documentation.
........
r73317 | benjamin.peterson | 2009-06-09 12:24:26 -0500 (Tue, 09 Jun 2009) | 1 line
make ast.c depend on the grammar
........
r73318 | benjamin.peterson | 2009-06-09 12:29:51 -0500 (Tue, 09 Jun 2009) | 1 line
explain why keyword names are not just NAME
........
r73321 | benjamin.peterson | 2009-06-09 16:13:43 -0500 (Tue, 09 Jun 2009) | 1 line
update symbol.py from with statement changes
........
r73324 | amaury.forgeotdarc | 2009-06-09 17:53:16 -0500 (Tue, 09 Jun 2009) | 2 lines
Avoid invoking the parser/compiler just to test the presence of a function.
........
r73331 | benjamin.peterson | 2009-06-10 08:45:31 -0500 (Wed, 10 Jun 2009) | 1 line
fix spelling
........
r73335 | raymond.hettinger | 2009-06-10 11:15:40 -0500 (Wed, 10 Jun 2009) | 1 line
Fix signed/unsigned compiler warning.
........
r73340 | amaury.forgeotdarc | 2009-06-10 15:30:19 -0500 (Wed, 10 Jun 2009) | 2 lines
Fix a typo spotted by Nick Coghlan.
........
r73363 | benjamin.peterson | 2009-06-11 12:51:17 -0500 (Thu, 11 Jun 2009) | 1 line
use multi-with syntax
........
2009-06-11 19:54:11 -03:00
|
|
|
|
|
|
|
* ``stdin=PIPE`` and ``stdout=PIPE`` must be specified.
|
|
|
|
|
|
|
|
* popen2 closes all file descriptors by default, but you have to specify
|
2010-12-13 02:45:02 -04:00
|
|
|
``close_fds=True`` with :class:`Popen` to guarantee this behavior on
|
|
|
|
all platforms or past Python versions.
|
2011-04-15 01:23:26 -03:00
|
|
|
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
Legacy Shell Invocation Functions
|
2011-11-08 07:50:58 -04:00
|
|
|
---------------------------------
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
This module also provides the following legacy functions from the 2.x
|
|
|
|
``commands`` module. These operations implicitly invoke the system shell and
|
|
|
|
none of the guarantees described above regarding security and exception
|
|
|
|
handling consistency are valid for these functions.
|
|
|
|
|
2022-04-30 03:53:29 -03:00
|
|
|
.. function:: getstatusoutput(cmd, *, encoding=None, errors=None)
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2017-09-06 21:39:23 -03:00
|
|
|
Return ``(exitcode, output)`` of executing *cmd* in a shell.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2013-11-05 08:57:25 -04:00
|
|
|
Execute the string *cmd* in a shell with :meth:`Popen.check_output` and
|
2022-04-30 03:53:29 -03:00
|
|
|
return a 2-tuple ``(exitcode, output)``.
|
|
|
|
*encoding* and *errors* are used to decode output;
|
2013-11-05 08:57:25 -04:00
|
|
|
see the notes on :ref:`frequently-used-arguments` for more details.
|
2013-11-03 14:24:50 -04:00
|
|
|
|
|
|
|
A trailing newline is stripped from the output.
|
2017-09-06 21:39:23 -03:00
|
|
|
The exit code for the command can be interpreted as the return code
|
|
|
|
of subprocess. Example::
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
>>> subprocess.getstatusoutput('ls /bin/ls')
|
|
|
|
(0, '/bin/ls')
|
|
|
|
>>> subprocess.getstatusoutput('cat /bin/junk')
|
2017-09-06 21:39:23 -03:00
|
|
|
(1, 'cat: /bin/junk: No such file or directory')
|
2011-11-08 06:49:23 -04:00
|
|
|
>>> subprocess.getstatusoutput('/bin/junk')
|
2017-09-06 21:39:23 -03:00
|
|
|
(127, 'sh: /bin/junk: not found')
|
|
|
|
>>> subprocess.getstatusoutput('/bin/kill $$')
|
|
|
|
(-15, '')
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2022-07-29 11:42:09 -03:00
|
|
|
.. availability:: Unix, Windows.
|
2014-03-07 21:04:17 -04:00
|
|
|
|
|
|
|
.. versionchanged:: 3.3.4
|
2017-09-06 21:39:23 -03:00
|
|
|
Windows support was added.
|
|
|
|
|
|
|
|
The function now returns (exitcode, output) instead of (status, output)
|
2018-09-21 17:18:20 -03:00
|
|
|
as it did in Python 3.3.3 and earlier. exitcode has the same value as
|
|
|
|
:attr:`~Popen.returncode`.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2022-04-30 03:53:29 -03:00
|
|
|
.. versionadded:: 3.11
|
|
|
|
Added *encoding* and *errors* arguments.
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2022-04-30 03:53:29 -03:00
|
|
|
.. function:: getoutput(cmd, *, encoding=None, errors=None)
|
2011-11-08 06:49:23 -04:00
|
|
|
|
|
|
|
Return output (stdout and stderr) of executing *cmd* in a shell.
|
|
|
|
|
2018-09-21 17:18:20 -03:00
|
|
|
Like :func:`getstatusoutput`, except the exit code is ignored and the return
|
2011-11-08 06:49:23 -04:00
|
|
|
value is a string containing the command's output. Example::
|
|
|
|
|
|
|
|
>>> subprocess.getoutput('ls /bin/ls')
|
|
|
|
'/bin/ls'
|
|
|
|
|
2022-07-29 11:42:09 -03:00
|
|
|
.. availability:: Unix, Windows.
|
2014-03-07 21:04:17 -04:00
|
|
|
|
|
|
|
.. versionchanged:: 3.3.4
|
|
|
|
Windows support added
|
2011-11-08 06:49:23 -04:00
|
|
|
|
2022-04-30 03:53:29 -03:00
|
|
|
.. versionadded:: 3.11
|
|
|
|
Added *encoding* and *errors* arguments.
|
|
|
|
|
2011-11-08 07:50:58 -04:00
|
|
|
|
2011-04-15 01:23:26 -03:00
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
|
|
|
|
.. _converting-argument-sequence:
|
|
|
|
|
|
|
|
Converting an argument sequence to a string on Windows
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
On Windows, an *args* sequence is converted to a string that can be parsed
|
|
|
|
using the following rules (which correspond to the rules used by the MS C
|
|
|
|
runtime):
|
|
|
|
|
|
|
|
1. Arguments are delimited by white space, which is either a
|
|
|
|
space or a tab.
|
|
|
|
|
|
|
|
2. A string surrounded by double quotation marks is
|
|
|
|
interpreted as a single argument, regardless of white space
|
|
|
|
contained within. A quoted string can be embedded in an
|
|
|
|
argument.
|
|
|
|
|
|
|
|
3. A double quotation mark preceded by a backslash is
|
|
|
|
interpreted as a literal double quotation mark.
|
|
|
|
|
|
|
|
4. Backslashes are interpreted literally, unless they
|
|
|
|
immediately precede a double quotation mark.
|
|
|
|
|
|
|
|
5. If backslashes immediately precede a double quotation mark,
|
|
|
|
every pair of backslashes is interpreted as a literal
|
|
|
|
backslash. If the number of backslashes is odd, the last
|
|
|
|
backslash escapes the next double quotation mark as
|
|
|
|
described in rule 3.
|
|
|
|
|
2011-04-15 01:26:28 -03:00
|
|
|
|
2011-07-27 13:29:31 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
:mod:`shlex`
|
|
|
|
Module which provides function to parse and escape command lines.
|
2022-04-25 20:19:39 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. _disable_vfork:
|
|
|
|
.. _disable_posix_spawn:
|
|
|
|
|
|
|
|
Disabling use of ``vfork()`` or ``posix_spawn()``
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
On Linux, :mod:`subprocess` defaults to using the ``vfork()`` system call
|
|
|
|
internally when it is safe to do so rather than ``fork()``. This greatly
|
|
|
|
improves performance.
|
|
|
|
|
2022-07-05 06:16:10 -03:00
|
|
|
If you ever encounter a presumed highly unusual situation where you need to
|
2022-04-25 20:19:39 -03:00
|
|
|
prevent ``vfork()`` from being used by Python, you can set the
|
2023-07-21 06:34:30 -03:00
|
|
|
:const:`subprocess._USE_VFORK` attribute to a false value.
|
2022-04-25 20:19:39 -03:00
|
|
|
|
2022-12-24 16:17:39 -04:00
|
|
|
::
|
|
|
|
|
2022-04-25 20:19:39 -03:00
|
|
|
subprocess._USE_VFORK = False # See CPython issue gh-NNNNNN.
|
|
|
|
|
|
|
|
Setting this has no impact on use of ``posix_spawn()`` which could use
|
|
|
|
``vfork()`` internally within its libc implementation. There is a similar
|
2023-07-21 06:34:30 -03:00
|
|
|
:const:`subprocess._USE_POSIX_SPAWN` attribute if you need to prevent use of
|
2022-04-25 20:19:39 -03:00
|
|
|
that.
|
|
|
|
|
2022-12-24 16:17:39 -04:00
|
|
|
::
|
|
|
|
|
2022-04-25 20:19:39 -03:00
|
|
|
subprocess._USE_POSIX_SPAWN = False # See CPython issue gh-NNNNNN.
|
|
|
|
|
|
|
|
It is safe to set these to false on any Python version. They will have no
|
|
|
|
effect on older versions when unsupported. Do not assume the attributes are
|
|
|
|
available to read. Despite their names, a true value does not indicate that the
|
2023-03-12 13:58:02 -03:00
|
|
|
corresponding function will be used, only that it may be.
|
2022-04-25 20:19:39 -03:00
|
|
|
|
|
|
|
Please file issues any time you have to use these private knobs with a way to
|
|
|
|
reproduce the issue you were seeing. Link to that issue from a comment in your
|
|
|
|
code.
|
|
|
|
|
|
|
|
.. versionadded:: 3.8 ``_USE_POSIX_SPAWN``
|
|
|
|
.. versionadded:: 3.11 ``_USE_VFORK``
|