summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorInada Naoki <songofacandy@gmail.com>2022-05-06 05:48:36 (GMT)
committerGitHub <noreply@github.com>2022-05-06 05:48:36 (GMT)
commit2b563f1ad31af0bb0a9947e6f1f76e58dbf170f0 (patch)
treecb37aa7795169c2b095ebbc7f67316ff0aca215c
parent926854e8583125eaadd2a19b580c09d7c12389e9 (diff)
downloadcpython-2b563f1ad31af0bb0a9947e6f1f76e58dbf170f0.zip
cpython-2b563f1ad31af0bb0a9947e6f1f76e58dbf170f0.tar.gz
cpython-2b563f1ad31af0bb0a9947e6f1f76e58dbf170f0.tar.bz2
gh-87901: Add encoding to os.popen (GH-92374)
-rw-r--r--Doc/library/os.rst8
-rw-r--r--Lib/os.py3
-rw-r--r--Misc/NEWS.d/next/Library/2022-05-06-13-53-10.gh-issue-87901.NnkUVr.rst1
3 files changed, 9 insertions, 3 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 21c2073..3c189bb 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -3916,12 +3916,13 @@ written in Python, such as a mail server's external command delivery program.
.. availability:: Unix.
-.. function:: popen(cmd, mode='r', buffering=-1)
+.. function:: popen(cmd, mode='r', buffering=-1, encoding=None)
Open a pipe to or from command *cmd*.
The return value is an open file object
connected to the pipe, which can be read or written depending on whether *mode*
- is ``'r'`` (default) or ``'w'``. The *buffering* argument has the same meaning as
+ is ``'r'`` (default) or ``'w'``.
+ The *buffering* and *encoding* arguments have the same meaning as
the corresponding argument to the built-in :func:`open` function. The
returned file object reads or writes text strings rather than bytes.
@@ -3944,6 +3945,9 @@ written in Python, such as a mail server's external command delivery program.
documentation for more powerful ways to manage and communicate with
subprocesses.
+ .. versionchanged:: 3.11
+ Added the *encoding* parameter.
+
.. function:: posix_spawn(path, argv, env, *, file_actions=None, \
setpgroup=None, resetids=False, setsid=False, setsigmask=(), \
diff --git a/Lib/os.py b/Lib/os.py
index 4626d1f..67662ca 100644
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -974,7 +974,7 @@ otherwise return -SIG, where SIG is the signal that killed it. """
# command in a shell can't be supported.
if sys.platform != 'vxworks':
# Supply os.popen()
- def popen(cmd, mode="r", buffering=-1):
+ def popen(cmd, mode="r", buffering=-1, encoding=None):
if not isinstance(cmd, str):
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
if mode not in ("r", "w"):
@@ -982,6 +982,7 @@ if sys.platform != 'vxworks':
if buffering == 0 or buffering is None:
raise ValueError("popen() does not support unbuffered streams")
import subprocess, io
+ encoding = io.text_encoding(encoding)
if mode == "r":
proc = subprocess.Popen(cmd,
shell=True, text=True,
diff --git a/Misc/NEWS.d/next/Library/2022-05-06-13-53-10.gh-issue-87901.NnkUVr.rst b/Misc/NEWS.d/next/Library/2022-05-06-13-53-10.gh-issue-87901.NnkUVr.rst
new file mode 100644
index 0000000..7f50dfb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-06-13-53-10.gh-issue-87901.NnkUVr.rst
@@ -0,0 +1 @@
+Add the *encoding* parameter to :func:`os.popen`.