[Python-ideas] Subprocess: Add an encoding argument

MRAB python at mrabarnett.plus.com
Mon Sep 1 22:05:48 CEST 2014


On 2014-09-01 20:14, Akira Li wrote:
> 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)
>
You can parenthesise multiple context managers like that, and, anyway,
I think it would be clearer as:

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

> Or the same, all at once:
>
>     lines = check_output(cmd).decode('utf-8').splitlines() #XXX issue22232
>



More information about the Python-ideas mailing list