summaryrefslogtreecommitdiffstats
path: root/Lib/subprocess.py
Commit message (Collapse)AuthorAgeFilesLines
* bpo-43776: Remove list call from args in Popen repr (GH-25338)M. Kocher2021-04-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Removes the `list` call in the Popen `repr`. Current implementation: For cmd = `python --version`, with `shell=True`. ```bash <Popen: returncode: None args: ['p', 'y', 't', 'h', 'o', 'n', ' ', '-', '-',...> ``` For `shell=False` and args=`['python', '--version']`, the output is correct: ```bash <Popen: returncode: None args: ['python', '--version']> ``` With the new changes the `repr` yields: For cmd = `python --version`, with `shell=True`: ```bash <Popen: returncode: None args: 'python --version'> ``` For `shell=False` and args=`['python', '--version']`, the output: ```bash <Popen: returncode: None args: ['python', '--version']> ``` Automerge-Triggered-By: GH:gpshead
* bpo-43510: Implement PEP 597 opt-in EncodingWarning. (GH-19481)Inada Naoki2021-03-291-1/+8
| | | | | | | | | | | See [PEP 597](https://www.python.org/dev/peps/pep-0597/). * Add `-X warn_default_encoding` and `PYTHONWARNDEFAULTENCODING`. * Add EncodingWarning * Add io.text_encoding() * open(), TextIOWrapper() emits EncodingWarning when encoding is omitted and warn_default_encoding is enabled. * _pyio.TextIOWrapper() uses UTF-8 as fallback default encoding used when failed to import locale module. (used during building Python) * bz2, configparser, gzip, lzma, pathlib, tempfile modules use io.text_encoding(). * What's new entry
* bpo-41718: subprocess imports grp and pwd on demand (GH-24987)Victor Stinner2021-03-231-12/+9
| | | | | The shutil and subprocess modules now only import the grp and pwd modules when they are needed, which is not the case by default in subprocess.
* bpo-43423 Fix IndexError in subprocess _communicate function (GH-24777)Chris Griffith2021-03-111-4/+2
| | | | | Check to make sure stdout and stderr are not empty before selecting an item from them in Windows subprocess._communicate. Co-authored-by: Gregory P. Smith <greg@krypto.org>
* bpo-43391: Remove the broken Python 2.4 link from the comment (GH-24736)Kamil Turek2021-03-041-1/+0
|
* bpo-42388: Fix subprocess.check_output input=None when text=True (GH-23467)Gregory P. Smith2020-12-251-1/+5
| | | | | | | | | | When the modern text= spelling of the universal_newlines= parameter was added for Python 3.7, check_output's special case around input=None was overlooked. So it behaved differently with universal_newlines=True vs text=True. This reconciles the behavior to be consistent and adds a test to guarantee it. Also clarifies the existing check_output documentation. Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
* bpo-40550: Fix time-of-check/time-of-action issue in ↵Filipe Laíns2020-11-211-1/+5
| | | | | | | subprocess.Popen.send_signal. (GH-20010) send_signal() now swallows the exception if the process it thought was still alive winds up not to exist anymore (always a plausible race condition despite the checks). Co-authored-by: Gregory P. Smith <greg@krypto.org>
* bpo-41586: Add pipesize parameter to subprocess & F_GETPIPE_SZ and ↵Ruben Vorderman2020-10-191-1/+18
| | | | | | | | | | | | | | | | | F_SETPIPE_SZ to fcntl. (GH-21921) * Add F_SETPIPE_SZ and F_GETPIPE_SZ to fcntl module * Add pipesize parameter for subprocess.Popen class This will allow the user to control the size of the pipes. On linux the default is 64K. When a pipe is full it blocks for writing. When a pipe is empty it blocks for reading. On processes that are very fast this can lead to a lot of wasted CPU cycles. On a typical Linux system the max pipe size is 1024K which is much better. For high performance-oriented libraries such as xopen it is nice to be able to set the pipe size. The workaround without this feature is to use my_popen_process.stdout.fileno() in conjuction with fcntl and 1031 (value of F_SETPIPE_SZ) to acquire this behavior.
* bpo-40939: Remove the old parser (GH-20768)Pablo Galindo2020-06-111-1/+1
| | | This commit removes the old parser, the deprecated parser module, the old parser compatibility flags and environment variables and all associated support code and documentation.
* bpo-40334: Rename PyConfig.use_peg to _use_peg_parser (GH-19670)Victor Stinner2020-04-231-1/+1
| | | | | | | | | | | * Rename PyConfig.use_peg to _use_peg_parser * Document PyConfig._use_peg_parser and mark it a deprecated * Mark -X oldparser option and PYTHONOLDPARSER env var as deprecated in the documentation. * Add use_old_parser() and skip_if_new_parser() to test.support * Remove sys.flags.use_peg: use_old_parser() uses _testinternalcapi.get_configs() instead. * Enhance test_embed tests * subprocess._args_from_interpreter_flags() copies -X oldparser
* bpo-39481: Implementation for PEP 585 (#18239)Guido van Rossum2020-04-071-21/+3
| | | | | | | | | | | | This implements things like `list[int]`, which returns an object of type `types.GenericAlias`. This object mostly acts as a proxy for `list`, but has attributes `__origin__` and `__args__` that allow recovering the parts (with values `list` and `(int,)`. There is also an approximate notion of type variables; e.g. `list[T]` has a `__parameters__` attribute equal to `(T,)`. Type variables are objects of type `typing.TypeVar`.
* bpo-40094: Add os.waitstatus_to_exitcode() (GH-19201)Victor Stinner2020-04-011-12/+6
| | | | | | | | | | | | | | Add os.waitstatus_to_exitcode() function to convert a wait status to an exitcode. Suggest waitstatus_to_exitcode() usage in the documentation when appropriate. Use waitstatus_to_exitcode() in: * multiprocessing, os, subprocess and _bootsubprocess modules; * test.support.wait_process(); * setup.py: run_command(); * and many tests.
* bpo-39489: Remove COUNT_ALLOCS special build (GH-18259)Victor Stinner2020-02-031-1/+1
| | | | | | | | | | | Remove: * COUNT_ALLOCS macro * sys.getcounts() function * SHOW_ALLOC_COUNT code in listobject.c * SHOW_TRACK_COUNT code in tupleobject.c * PyConfig.show_alloc_count field * -X showalloccount command line option * @test.support.requires_type_collecting decorator
* bpo-35182: fix communicate() crash after child closes its pipes (GH-17020) ↵Alex Rebert2020-01-221-2/+2
| | | | | | | | | (GH-18117) When communicate() is called in a loop, it crashes when the child process has already closed any piped standard stream, but still continues to be running Co-authored-by: Andriy Maletsky <andriy.maletsky@gmail.com>
* bpo-38630: Fix subprocess.Popen.send_signal() race condition (GH-16984)Victor Stinner2020-01-151-3/+25
| | | | | | | On Unix, subprocess.Popen.send_signal() now polls the process status. Polling reduces the risk of sending a signal to the wrong process if the process completed, the Popen.returncode attribute is still None, and the pid has been reassigned (recycled) to a new different process.
* bpo-39019: Implement missing __class_getitem__ for subprocess classes (GH-17558)Batuhan Taşkaya2019-12-301-0/+24
|
* bpo-38724: Implement subprocess.Popen.__repr__ (GH-17151)Andrey Doroschenko2019-11-171-0/+9
|
* bpo-38417: Add umask support to subprocess (GH-16726)Gregory P. Smith2019-10-121-5/+9
| | | | On POSIX systems, allow the umask to be set in the child process before we exec.
* bpo-36046: posix_spawn() doesn't support uid/gid (GH-16384)Victor Stinner2019-09-251-1/+4
| | | | | * subprocess.Popen now longer uses posix_spawn() if uid, gid or gids are set. * test_subprocess: add "nobody" and "nfsnobody" group names for test_group(). * test_subprocess: test_user() and test_group() are now also tested with close_fds=False.
* bpo-36046: Add user and group parameters to subprocess (GH-11950)Patrick McLean2019-09-121-5/+100
| | | | | | | | | | | | | | * subprocess: Add user, group and extra_groups paremeters to subprocess.Popen This adds a `user` parameter to the Popen constructor that will call setreuid() in the child before calling exec(). This allows processes running as root to safely drop privileges before running the subprocess without having to use a preexec_fn. This also adds a `group` parameter that will call setregid() in the child process before calling exec(). Finally an `extra_groups` parameter was added that will call setgroups() to set the supplimental groups.
* bpo-37424: Avoid a hang in subprocess.run timeout output capture (GH-14490)Gregory P. Smith2019-09-111-9/+27
| | | | | | | Fixes a possible hang when using a timeout on subprocess.run() while capturing output. If the child process spawned its own children or otherwise connected its stdout or stderr handles with another process, we could hang after the timeout was reached and our child was killed when attempting to read final output from the pipes.
* Fix subprocess docstring typo (GH-15812)Matthias2019-09-101-1/+1
|
* bpo-37380: subprocess: don't use _active on win (GH-14360)Ruslan Kuprieiev2019-06-281-16/+32
| | | | | | | | | | | | | | | | | | | As noted by @eryksun in [1] and [2], using _cleanup and _active(in __del__) is not necessary on Windows, since: > Unlike Unix, a process in Windows doesn't have to be waited on by > its parent to avoid a zombie. Keeping the handle open will actually > create a zombie until the next _cleanup() call, which may be never > if Popen() isn't called again. This patch simply defines `subprocess._active` as `None`, for which we already have the proper logic in place in `subprocess.Popen.__del__`, that prevents it from trying to append the process to the `_active`. This patch also defines `subprocess._cleanup` as a noop for Windows. [1] https://bugs.python.org/issue37380#msg346333 [2] https://bugs.python.org/issue36067#msg336262 Signed-off-by: Ruslan Kuprieiev <ruslan@iterative.ai>
* bpo-37363: Add audit events for a range of modules (GH-14301)Steve Dower2019-06-241-1/+8
|
* bpo-34886: Fix subprocess.run handling of exclusive arguments (GH-11727)Rémi Lapeyre2019-06-081-2/+2
| | | | | | Fix an unintended ValueError from :func:`subprocess.run` when checking for conflicting `input` and `stdin` or `capture_output` and `stdout` or `stderr` args when they were explicitly provided but with `None` values within a passed in `**kwargs` dict rather than as passed directly by name.
* bpo-31961: Fix support of path-like executables in subprocess. (GH-5914)Serhiy Storchaka2019-05-281-3/+22
|
* bpo-36793: Remove unneeded __str__ definitions. (GH-13081)Serhiy Storchaka2019-05-061-1/+0
| | | | Classes that define __str__ the same as __repr__ can just inherit it from object.
* subprocess: close pipes/fds by using ExitStack (GH-11686)Giampaolo Rodola2019-01-291-17/+18
| | | | | | | Close pipes/fds in subprocess by using ExitStack. "In case of premature failure on X.Close() or os.close(X) the remaining pipes/fds will remain "open". Perhaps it makes sense to use contextlib.ExitStack." - Rationale: https://github.com/python/cpython/pull/11575#discussion_r250288394
* Fix docstr/comment typos in _use_posix_spawn(). (GH-11684)Gregory P. Smith2019-01-261-11/+10
|
* bpo-35537: subprocess can use posix_spawn with pipes (GH-11575)Victor Stinner2019-01-231-28/+64
| | | | | | * subprocess.Popen can now also use os.posix_spawn() with pipes, but only if pipe file descriptors are greater than 2. * Fix Popen._posix_spawn(): set '_child_created' attribute to True. * Add Popen._close_pipe_fds() helper function to factorize the code.
* Revert "bpo-35537: subprocess can now use os.posix_spawnp (GH-11579)" (GH-11582)Victor Stinner2019-01-161-10/+3
| | | This reverts commit 07858894689047c77f9c12ddc061d30681368d19.
* bpo-35537: subprocess can now use os.posix_spawnp (GH-11579)Victor Stinner2019-01-161-3/+10
| | | | The subprocess module can now use the os.posix_spawnp() function, if it is available, to locate the program in the PATH.
* bpo-35537: subprocess uses os.posix_spawn in some cases (GH-11452)Victor Stinner2019-01-151-0/+82
| | | | | | | | | | | | The subprocess module can now use the os.posix_spawn() function in some cases for better performance. Currently, it is only used on macOS and Linux (using glibc 2.24 or newer) if all these conditions are met: * executable path contains a directory * close_fds=False * preexec_fn, pass_fds, cwd, stdin, stdout, stderr and start_new_session parameters are not set Co-authored-by: Joannah Nanjekye <nanjekyejoannah@gmail.com>
* bpo-34812: subprocess._args_from_interpreter_flags(): add isolated (GH-10675)Victor Stinner2018-11-231-2/+8
| | | | | | | | The "-I" command line option (run Python in isolated mode) is now also copied by the multiprocessing and distutils modules when spawning child processes. Previously, only -E and -s options (enabled by -I) were copied. subprocess._args_from_interpreter_flags() now copies the -I flag.
* bpo-32236: open() emits RuntimeWarning if buffering=1 for binary mode (GH-4842)Alexey Izbyshev2018-10-201-1/+10
| | | | | | | | | If buffering=1 is specified for open() in binary mode, it is silently treated as buffering=-1 (i.e., the default buffer size). Coupled with the fact that line buffering is always supported in Python 2, such behavior caused several issues (e.g., bpo-10344, bpo-21332). Warn that line buffering is not supported if open() is called with binary mode and buffering=1.
* closes bpo-32490: Fix filename duplication in subprocess exception message. ↵Zackery Spytz2018-09-111-2/+0
| | | | | (GH-9163) 8621bb5d93239316f97281826461b85072ff6db7 sets the filename in directly in the FileNotFoundError, so we may revert the earlier fix 5f780400572508a8179de6a6c13b58b7be417ef5.
* bpo-8110: Refactor platform detection in subprocess (GH-9053)Zachary Ware2018-09-101-55/+56
| | | | Check for functionality via imports rather than checking sys.platform specifically for Windows
* bpo-34044: subprocess.Popen copies startupinfo (GH-8090)Victor Stinner2018-07-051-0/+17
| | | | | | | subprocess.Popen now copies the startupinfo argument to leave it unchanged: it will modify the copy, so that the same STARTUPINFO object can be used multiple times. Add subprocess.STARTUPINFO.copy() method.
* Revert "bpo-31961: subprocess now accepts path-like args (GH-4329)" (#5912)Serhiy Storchaka2018-02-271-10/+2
| | | | | | * Revert "bpo-31961: subprocess now accepts path-like args (GH-4329)" This reverts commit dd42cb71f2cb02f3a32f016137b12a146bc0d0e2.
* bpo-31961: subprocess now accepts path-like args (GH-4329)Anders Lorentsen2018-01-301-2/+10
| | | Allow os.PathLike args in subprocess APIs.
* bpo-32102 Add "capture_output=True" to subprocess.run (GH-5149)Bo Bayles2018-01-301-1/+9
| | | | Add "capture_output=True" option to subprocess.run, this is equivalent to setting stdout=PIPE, stderr=PIPE but is much more readable.
* bpo-25942: make subprocess more graceful on ^C (GH-5026)Gregory P. Smith2018-01-301-13/+72
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Do not allow receiving a SIGINT to cause the subprocess module to trigger an immediate SIGKILL of the child process. SIGINT is normally sent to all child processes by the OS at the same time already as was the established normal behavior in 2.7 and 3.2. This behavior change was introduced during the fix to https://bugs.python.org/issue12494 and is generally surprising to command line tool users who expect other tools launched in child processes to get their own SIGINT and do their own cleanup. In Python 3.3-3.6 subprocess.call and subprocess.run would immediately SIGKILL the child process upon receiving a SIGINT (which raises a KeyboardInterrupt). We now give the child a small amount of time to exit gracefully before resorting to a SIGKILL. This is also the case for subprocess.Popen.__exit__ which would previously block indefinitely waiting for the child to die. This was hidden from many users by virtue of subprocess.call and subprocess.run sending the signal immediately. Behavior change: subprocess.Popen.__exit__ will not block indefinitely when the exiting exception is a KeyboardInterrupt. This is done for user friendliness as people expect their ^C to actually happen. This could cause occasional orphaned Popen objects when not using `call` or `run` with a child process that hasn't exited. Refactoring involved: The Popen.wait method deals with the KeyboardInterrupt second chance, existing platform specific internals have been renamed to _wait(). Also fixes comment typos.
* bpo-19764: Implemented support for subprocess.Popen(close_fds=True) on ↵Segev Finer2017-12-181-19/+46
| | | | | | | | | | | | | | | | | Windows (#1218) Even though Python marks any handles it opens as non-inheritable there is still a race when using `subprocess.Popen` since creating a process with redirected stdio requires temporarily creating inheritable handles. By implementing support for `subprocess.Popen(close_fds=True)` we fix this race. In order to implement this we use PROC_THREAD_ATTRIBUTE_HANDLE_LIST which is available since Windows Vista. Which allows to pass an explicit list of handles to inherit when creating a process. This commit also adds `STARTUPINFO.lpAttributeList["handle_list"]` which can be used to control PROC_THREAD_ATTRIBUTE_HANDLE_LIST directly.
* bpo-29240: PEP 540: Add a new UTF-8 Mode (#855)Victor Stinner2017-12-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | * Add -X utf8 command line option, PYTHONUTF8 environment variable and a new sys.flags.utf8_mode flag. * If the LC_CTYPE locale is "C" at startup: enable automatically the UTF-8 mode. * Add _winapi.GetACP(). encodings._alias_mbcs() now calls _winapi.GetACP() to get the ANSI code page * locale.getpreferredencoding() now returns 'UTF-8' in the UTF-8 mode. As a side effect, open() now uses the UTF-8 encoding by default in this mode. * Py_DecodeLocale() and Py_EncodeLocale() now use the UTF-8 encoding in the UTF-8 Mode. * Update subprocess._args_from_interpreter_flags() to handle -X utf8 * Skip some tests relying on the current locale if the UTF-8 mode is enabled. * Add test_utf8mode.py. * _Py_DecodeUTF8_surrogateescape() gets a new optional parameter to return also the length (number of wide characters). * pymain_get_global_config() and pymain_set_global_config() now always copy flag values, rather than only copying if the new value is greater than the old value.
* bpo-32230: Set sys.warnoptions with -X dev (#4820)Victor Stinner2017-12-121-4/+14
| | | | | | | | | | | | | | Rather than supporting dev mode directly in the warnings module, this instead adjusts the initialisation code to add an extra 'default' entry to sys.warnoptions when dev mode is enabled. This ensures that dev mode behaves *exactly* as if `-Wdefault` had been passed on the command line, including in the way it interacts with `sys.warnoptions`, and with other command line flags like `-bb`. Fix also bpo-20361: have -b & -bb options take precedence over any other warnings options. Patch written by Nick Coghlan, with minor modifications of Victor Stinner.
* bpo-32089: Fix warnings filters in dev mode (#4482)Victor Stinner2017-11-211-6/+2
| | | | | | | | | | The developer mode (-X dev) now creates all default warnings filters to order filters in the correct order to always show ResourceWarning and make BytesWarning depend on the -b option. Write a functional test to make sure that ResourceWarning is logged twice at the same location in the developer mode. Add a new 'dev_mode' field to _PyCoreConfig.
* bpo-32094: Update subprocess for -X dev (#4480)Victor Stinner2017-11-201-1/+22
| | | | | | | | Modify subprocess._args_from_interpreter_flags() to handle -X dev option. Add also unit tests for test.support.args_from_interpreter_flags() and test.support.optim_args_from_interpreter_flags().
* bpo-31884 subprocess: add Windows constants for process priority (#4150)James2017-11-081-2/+12
|
* Fix miscellaneous typos (#4275)luzpaz2017-11-051-1/+1
|
* bpo-31756: subprocess.run should alias universal_newlines to text (#4049)andyclegg2017-10-231-26/+52
| | | | | | | | | Improve human friendliness of the Popen API: Add text=False as a keyword-only argument to subprocess.Popen along with a Popen attribute .text_mode and set this based on the encoding/errors/universal_newlines/text arguments. The universal_newlines parameter and attribute are maintained for backwards compatibility.