[Python-ideas] Subprocess: Add an encoding argument

Akira Li 4kir4.1i at gmail.com
Mon Sep 1 21:14:04 CEST 2014


Paul Moore <p.f.moore at gmail.com> writes:

> I propose adding an "encoding" parameter to subprocess.Popen (and the
> various wrapper routines) to allow specifying the actual encoding to
> use.
>
> Obviously, you can simply wrap the binary streams yourself - the main
> use for this facility would be in the higher level functions like
> check_output and communicate.
>
> Does this seem like a reasonable suggestion?

Could you provide examples how the final result could look like?

For example, to read utf-8 encoded byte stream as a text with universal
newline mode enabled:

  with (Popen(cmd, stdout=PIPE, bufsize=1) as p,
        TextIOWrapper(p.stdout, encoding='utf-8') as pipe):
      for line in pipe:
          process(line)

Or the same, all at once:

   lines = check_output(cmd).decode('utf-8').splitlines() #XXX issue22232


--
Akira



More information about the Python-ideas mailing list