This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author methane
Recipients methane, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Date 2019-06-14.05:46:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1560491190.29.0.949105060129.issue37275@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-06-14 05:46:30methanesetrecipients: + methane, paul.moore, vstinner, tim.golden, zach.ware, steve.dower
2019-06-14 05:46:30methanesetmessageid: <1560491190.29.0.949105060129.issue37275@roundup.psfhosted.org>
2019-06-14 05:46:30methanelinkissue37275 messages
2019-06-14 05:46:29methanecreate