[Python-ideas] Subprocess: Add an encoding argument
Akira Li
4kir4.1i at gmail.com
Mon Sep 1 22:08:58 CEST 2014
Paul Moore <p.f.moore at gmail.com> writes:
> On 1 September 2014 20:14, Akira Li
> <4kir4.1i at gmail.com> wrote:
>> Could you provide examples how the final result could look like?
>
> Do you mean what I'm proposing?
>
> p = Popen(..., encoding='utf-8')
> p.stdout is now a text stream assuming the data is in UTF8, rather
> than assuming it's in the default encoding.
What if you want to specify an error handler e.g., to read a file list
from `find -print0` -like program: you could pass
errors='surrogateescape', newlines='\0' (issue1152248) to
TextIOWrapper(p.stdin).
Both errors and newlines can be different for stdin/stdout pipes.
>> 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)
>
> That looks like sort of what I had in mind as a workaround. I hadn't
> tried it to confirm it worked, though.
>
>> Or the same, all at once:
>>
>> lines = check_output(cmd).decode('utf-8').splitlines() #XXX issue22232
>
> Yes, essentially, although the need for an explicit decode feels a bit
> ugly to me...
>
--
Akira
More information about the Python-ideas
mailing list