New submission from Inada Naoki <songofaca...@gmail.com>: When stdout is redirected to file and cp65001 is used, stdout encoding is unexpectable:
# Power Shell 6 (use cp65001 by default) PS C:¥> python3 -c "print('おはよう')" > ps.txt # cmd.exe C:¥> chcp 65001 C:¥> python3 -c "print('おはよう')" > cmd.txt Now, ps.txt is encoded by UTF-8, but cmd.txt is encoded by cp932 (ACP). This is because: * TextIOWrapper tries `os.device_encoding(1)` * `os.device_encoding(1)` use GetConsoleOutputCP() without checking stdout is console In the example above, a console is attached when python is called from Power Shell 6, but it is not attached when python is called from cmd.exe. I think using GetConsoleOutputCP() for non console is abusing. --- There is a relating issue: UTF-8 mode doesn't override stdin,stdout,stderr encoding when console is attached. On Unix, os.device_encoding() uses locale encoding and UTF-8 mode overrides locale encoding. Good. But on Windows, os.device_encoding() uses GetConsole(Output)CP(). UTF-8 mode doesn't override it. If we stop abusing GetConsoleOutputCP(), this issue is fixed automatically. But if we keep using GetConsoleOutputCP() for stdout which is not a console, UTF-8 mode should override it. ---------- components: Windows messages: 345551 nosy: inada.naoki, paul.moore, steve.dower, tim.golden, vstinner, zach.ware priority: normal severity: normal status: open title: GetConsole(Output)CP is used even when stdin/stdout is redirected type: behavior versions: Python 3.8, Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37275> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com