Skip to content

gh-87901: os.popen: Fix new encoding argument. #92415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/library/os.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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, \
Expand Down
6 changes: 3 additions & 3 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand All @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``encoding`` argument of :func:`os.popen` and added ``errors`` argument
too.