[Python-ideas] Subprocess: Add an encoding argument

Akira Li 4kir4.1i at gmail.com
Mon Sep 1 22:33:38 CEST 2014


MRAB <python at mrabarnett.plus.com> writes:

> 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,
You mean: "can't". I know [1]

[1] https://mail.python.org/pipermail/python-dev/2014-August/135769.html

> 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)
>
It is a habit to use the explicit with-statement for file-like objects.
You are right -- it is not necessary in this case. Though with-statement 
forces file.close() in time and you don't need to consider carefully 
what happens if it is called by a garbage collector (if at all) at some
indeterminate time in the future.


--
Akira



More information about the Python-ideas mailing list