diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 3c189bb40e234c..538ad955971548 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, *, encoding=None, errors=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* and *encoding* arguments have the same meaning as + The *buffering*, *encoding*, and *errors* 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. @@ -3946,7 +3946,7 @@ written in Python, such as a mail server's external command delivery program. subprocesses. .. versionchanged:: 3.11 - Added the *encoding* parameter. + Added the *encoding* and *errors* parameters. .. function:: posix_spawn(path, argv, env, *, file_actions=None, \ diff --git a/Lib/os.py b/Lib/os.py index 67662ca7ad8588..cd41ffb37d1258 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -974,7 +974,7 @@ 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, *, encoding=None, errors=None): if not isinstance(cmd, str): raise TypeError("invalid cmd type (%s, expected string)" % type(cmd)) if mode not in ("r", "w"): @@ -985,13 +985,13 @@ def popen(cmd, mode="r", buffering=-1, encoding=None): encoding = io.text_encoding(encoding) if mode == "r": proc = subprocess.Popen(cmd, - shell=True, text=True, + shell=True, encoding=encoding, errors=errors, stdout=subprocess.PIPE, bufsize=buffering) return _wrap_close(proc.stdout, proc) else: proc = subprocess.Popen(cmd, - shell=True, text=True, + shell=True, encoding=encoding, errors=errors, stdin=subprocess.PIPE, bufsize=buffering) return _wrap_close(proc.stdin, proc) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index 36ad587760d701..b7d30dcec9e6a4 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -167,6 +167,13 @@ def test_getcwdb(self): self.assertIsInstance(cwd, bytes) self.assertEqual(os.fsdecode(cwd), os.getcwd()) + @unittest.skipUnless(hasattr(os, 'popen'), "needs os.popen()") + @support.requires_subprocess() + def test_popen_encoding(self): + cmd = f"""{sys.executable} -c "import sys; sys.stdout.buffer.write(b'\\xc0')" """ + with os.popen(cmd, encoding="latin1") as p: + self.assertEqual(p.read(), "\xc0") + # Tests creating TESTFN class FileTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2022-05-13-05-27-07.gh-issue-87901.gct4iY.rst b/Misc/NEWS.d/next/Library/2022-05-13-05-27-07.gh-issue-87901.gct4iY.rst new file mode 100644 index 00000000000000..dc936cba232e03 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-13-05-27-07.gh-issue-87901.gct4iY.rst @@ -0,0 +1,2 @@ +Fix ``encoding`` argument of :func:`os.popen` and added ``errors`` argument +too.