Skip to content

Commit f9a091a

Browse files
committed
pythongh-87901: Add encoding to os.popen
1 parent 0729b31 commit f9a091a

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

Doc/library/os.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3897,12 +3897,13 @@ written in Python, such as a mail server's external command delivery program.
38973897
.. availability:: Unix.
38983898

38993899

3900-
.. function:: popen(cmd, mode='r', buffering=-1)
3900+
.. function:: popen(cmd, mode='r', buffering=-1, encoding=None)
39013901

39023902
Open a pipe to or from command *cmd*.
39033903
The return value is an open file object
39043904
connected to the pipe, which can be read or written depending on whether *mode*
3905-
is ``'r'`` (default) or ``'w'``. The *buffering* argument has the same meaning as
3905+
is ``'r'`` (default) or ``'w'``.
3906+
The *buffering* and *encoding* arguments have the same meaning as
39063907
the corresponding argument to the built-in :func:`open` function. The
39073908
returned file object reads or writes text strings rather than bytes.
39083909

@@ -3925,6 +3926,9 @@ written in Python, such as a mail server's external command delivery program.
39253926
documentation for more powerful ways to manage and communicate with
39263927
subprocesses.
39273928

3929+
.. versionchanged: 3.11
3930+
Added the *encoding* parameter.
3931+
39283932
39293933
.. function:: posix_spawn(path, argv, env, *, file_actions=None, \
39303934
setpgroup=None, resetids=False, setsid=False, setsigmask=(), \

Lib/os.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,14 +974,15 @@ def spawnlpe(mode, file, *args):
974974
# command in a shell can't be supported.
975975
if sys.platform != 'vxworks':
976976
# Supply os.popen()
977-
def popen(cmd, mode="r", buffering=-1):
977+
def popen(cmd, mode="r", buffering=-1, encoding=None):
978978
if not isinstance(cmd, str):
979979
raise TypeError("invalid cmd type (%s, expected string)" % type(cmd))
980980
if mode not in ("r", "w"):
981981
raise ValueError("invalid mode %r" % mode)
982982
if buffering == 0 or buffering is None:
983983
raise ValueError("popen() does not support unbuffered streams")
984984
import subprocess, io
985+
encoding = io.text_encoding(encoding)
985986
if mode == "r":
986987
proc = subprocess.Popen(cmd,
987988
shell=True, text=True,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add the *encoding* parameter to :func:`os.popen`.

0 commit comments

Comments
 (0)