From a09cfcda270a16454400ea02b125119aab9f6e93 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Mon, 16 May 2022 15:29:25 +0900 Subject: [PATCH 1/3] gh-87901: os.popen: Remove the encoding argument --- Doc/library/os.rst | 14 ++++++++++---- Lib/os.py | 5 ++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 3c189bb40e234c..c40c334a119ef6 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -3916,13 +3916,13 @@ written in Python, such as a mail server's external command delivery program. .. availability:: Unix. -.. function:: popen(cmd, mode='r', buffering=-1, encoding=None) +.. function:: popen(cmd, mode='r', buffering=-1) 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* and *encoding* arguments have the same meaning as + The *buffering* argument 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. @@ -3945,8 +3945,14 @@ 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. + .. note:: + On Unix, the *cmd* argument is encoded with + :term:`file system encoding `. + The returned file object connected to the pipe uses + :func:`locale.getpreferredencoding(False) `_. + The :ref:`Python UTF-8 Mode ` affects both encodings. + + Use :class:`subprocess.Popen` to control encoding and many other options. .. function:: posix_spawn(path, argv, env, *, file_actions=None, \ diff --git a/Lib/os.py b/Lib/os.py index 67662ca7ad8588..648188e0f13490 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -974,15 +974,14 @@ def spawnlpe(mode, file, *args): # command in a shell can't be supported. if sys.platform != 'vxworks': # Supply os.popen() - def popen(cmd, mode="r", buffering=-1, encoding=None): + def popen(cmd, mode="r", buffering=-1): if not isinstance(cmd, str): raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) if mode not in ("r", "w"): raise ValueError("invalid mode %r" % mode) if buffering == 0 or buffering is None: raise ValueError("popen() does not support unbuffered streams") - import subprocess, io - encoding = io.text_encoding(encoding) + import subprocess if mode == "r": proc = subprocess.Popen(cmd, shell=True, text=True, From 393ec004548692db7c59de15b06cae38c9818cb3 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 18 May 2022 17:45:39 +0900 Subject: [PATCH 2/3] Remove subprocess details about encoding --- Doc/library/os.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index c40c334a119ef6..dc0f2e4158ac0e 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -3946,13 +3946,12 @@ written in Python, such as a mail server's external command delivery program. subprocesses. .. note:: - On Unix, the *cmd* argument is encoded with - :term:`file system encoding `. - The returned file object connected to the pipe uses - :func:`locale.getpreferredencoding(False) `_. - The :ref:`Python UTF-8 Mode ` affects both encodings. + The :ref:`Python UTF-8 Mode ` affects encodings used + for *cmd* and pipe contents. - Use :class:`subprocess.Popen` to control encoding and many other options. + :func:`popen` is a simple wrapper around :class:`subprocess.Popen`. + Use :class:`subprocess.Popen` or :func:`subprocess.run` to + control options like encodings. .. function:: posix_spawn(path, argv, env, *, file_actions=None, \ From 47b5089f5676dbe0951a3910e47a2d6462e8a5aa Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Wed, 18 May 2022 21:04:12 +0900 Subject: [PATCH 3/3] add news --- .../next/Library/2022-05-18-21-04-09.gh-issue-87901.lnf041.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2022-05-18-21-04-09.gh-issue-87901.lnf041.rst diff --git a/Misc/NEWS.d/next/Library/2022-05-18-21-04-09.gh-issue-87901.lnf041.rst b/Misc/NEWS.d/next/Library/2022-05-18-21-04-09.gh-issue-87901.lnf041.rst new file mode 100644 index 00000000000000..3488541eb3d77c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-18-21-04-09.gh-issue-87901.lnf041.rst @@ -0,0 +1,2 @@ +Removed the ``encoding`` argument from :func:`os.popen` that was added in +3.11b1.