Weird issue I've found on Windows images in Azure Devops Pipelines and
Github actions. Printing Unicode characters fails on these images because, for some reason, the encoding is mapped to cp1252. What is particularly
weird about the code page being set to 1252 is that if you execute "chcp"
it shows that the code page is 65001.
any idea why this would be happening in this situation? AFAIK, stdout
*is* a console when these images are running the python process.
is there a way I can check the locale and code page values that you mentioned? I assume I could call GetACP using ctypes, but maybe
there is a simpler way?
Two questions: any idea why this would be happening in this situation? AFAIK, stdout *is* a console when these images are running the python process. Second - is there a way I can check the locale and code page values that you mentioned? I assume Icould call GetACP using ctypes, but maybe there is a simpler way?
On Sat, Nov 12, 2022 at 10:21 AM 12Jessicasmith34 <12jessicasmith34@gmail.com> wrote:
could call GetACP using ctypes, but maybe there is a simpler way?Two questions: any idea why this would be happening in this situation? AFAIK, stdout *is* a console when these images are running the python process. Second - is there a way I can check the locale and code page values that you mentioned? I assume I
Maybe, python doesn't write to console in this case.
python -(pipe)-> PowerShell -> Console
In this case, python uses ACP for writing to pipe.
And PowerShell uses OutputEncoding for reading from pipe.
If you want to use UTF-8 on PowerShell in Windows,
* Set PYTHONUTF8=1 (Python uses UTF-8 for writing into pipe).
* Set `$OutputEncoding =
[System.Text.Encoding]::GetEncoding('utf-8')` in PowerShell profile.
If you want to use UTF-8 on PowerShell in Windows,
* Set PYTHONUTF8=1 (Python uses UTF-8 for writing into pipe).
* Set `$OutputEncoding =
[System.Text.Encoding]::GetEncoding('utf-8')` in PowerShell profile.
If sys.std* are console files, then in Python 3.6+, sys.std*.buffer.raw will be _io._WindowsConsoleIO
io.TextIOWrapper uses locale.getpreferredencoding(False) as the default encoding
On Fri, Nov 11, 2022 at 8:16 PM Eryk Sun <ery...@gmail.com> wrote:
If sys.std* are console files, then in Python 3.6+, sys.std*.buffer.raw will be _io._WindowsConsoleIOThank you for your replies - checking the sys.stdout.buffer.raw value
io.TextIOWrapper uses locale.getpreferredencoding(False) as the default encoding
is what finally helped me understand. Turns out, the Windows agent is redirecting the output of all python commands to a file, so sys.stdout
is a file using the locale encoding of cp1252, instead of being a
stream using encoding utf8. I wrote up a gist with my findings to
hopefully help out some other poor soul in the future: https://gist.github.com/NodeJSmith/e7e37f2d3f162456869f015f842bcf15
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 58:12:56 |
Calls: | 6,712 |
Files: | 12,243 |
Messages: | 5,355,629 |