Title: Value of `sys.stdout.encoding` changed in some cases on Windows
Type: Severity: normal
Components: Versions:
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: pekka.klarck
Priority: Keywords:

Created on 2020-03-10.23:36:24 by pekka.klarck, last changed 2020-03-10.23:41:17 by pekka.klarck.

msg13010 (view) Author: Pekka Klärck (pekka.klarck) Date: 2020-03-10.23:36:24
While running our projects acceptance tests with Jython 2.7.2rc1 we noticed that the value of `sys.stdout.encoding` is different to what it was with earlier versions if using Windows, the current code page is different to the default code page, and stdout is redirected.

For example, on my Windows machine the default code page is 850 and by default the output of both of these commands is cp850:

  jython -c "import sys; print sys.stdout.encoding"
  jython -c "import sys; print sys.stdout.encoding"  > out.txt

If I change the code page to 437 with `chcp 437`, then the first command above correctly prints cp437  but output of the latter is cp850. With earlier Jython versions the result was cp437 in both cases.

It seems that when output is redirected, `sys.stdout.encoding` always returns the system wide code page when it should return the code page the console is using. We actually had a similar bug in our code base and there the problem was that we used Windows API function GetOEMCP when we should have used GetConsoleOutputCP.
msg13012 (view) Author: Pekka Klärck (pekka.klarck) Date: 2020-03-10.23:41:17
It could also be argued that `sys.stdout.encoding` returning the encoding of the console when stdout is redirected to a file is wrong in general. CPython 2.7 returns `None` in that case and CPython 3 returns the encoding of the generated file which is cp1252 in my environment.
Date User Action Args
2020-03-10 23:41:17pekka.klarcksetmessages: + msg13012
2020-03-10 23:36:24pekka.klarckcreate