[issue37275] GetConsole(Output)CP is used even when stdin/stdout is redirected

Inada Naoki report at bugs.python.org
Fri Jun 14 19:22:05 EDT 2019


Inada Naoki <songofacandy at gmail.com> added the comment:

On Sat, Jun 15, 2019 at 2:43 AM Eryk Sun <report at bugs.python.org> wrote:
>
> Eryk Sun <eryksun at gmail.com> added the comment:
>
> > # Power Shell 6 (use cp65001 by default)
> > PS C:¥> python3 -c "print('おはよう')" > ps.txt
>
> PowerShell standard I/O redirection is different from any shell I've ever used. In this case, it runs Python with StandardOutput set to a handle for a pipe instead of a handle for the file. It decodes Python's output using whatever encoding is configured for input and re-encodes it with whatever encoding is configured for output.

I'm sorry,  I mixed my assumption.  I checked `os.device_encoding()` in cmd,
but forgot to check it on Power Shell.  All I said about Power Shell was just
my assumption and it was wrong.  And thank you for clarifying.

I confirmed writing UTF-8 to pipe cause mojibake, because Power Shell decodes
it using cp932.

```
PS C:\Users\inada-n> python3 -Xutf8 -c "import os,sys;
print(os.device_encoding(1), sys.stdout.encoding, file=sys.stderr);
print('こんにちは')" >x
None utf-8
PS C:\Users\inada-n> type x
```

Hmm, how can I teach to Power Shell about python3 is using
UTF-8 for stdout?
It seems cmd.exe with chcp 65001 and PYTHONUTF8=1 is better
than PowerShell when I want to use UTF-8 on Windows.

Anyway, nothing is wrong about python.  I just didn't understand
PowerShell at all.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37275>
_______________________________________


More information about the Python-bugs-list mailing list