Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-87901: Add encoding to os.popen#92374

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
methane merged 2 commits intopython:mainfrommethane:os-popen-encoding
May 6, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
8 changes: 6 additions & 2 deletionsDoc/library/os.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3897,12 +3897,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.

Expand All@@ -3925,6 +3926,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=(), \
Expand Down
3 changes: 2 additions & 1 deletionLib/os.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -974,14 +974,15 @@ 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):
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"):
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Maybe I'm missing something obvious, but shouldn't this be passed to subprocess.Popen?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you. I was too rushed.
I will fix it in follow up PR.
#92415

if mode == "r":
proc = subprocess.Popen(cmd,
shell=True, text=True,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Add the *encoding* parameter to :func:`os.popen`.

[8]ページ先頭

©2009-2025 Movatter.jp