SublimeLinter-pycodestyle icon indicating copy to clipboard operation
SublimeLinter-pycodestyle copied to clipboard

On Windows the error column is wrong when non-ASCII characters come earlier on the line (stdin decoded as cp1252)

Open dpc00 opened this issue 2 months ago • 0 comments

On Windows the error column is wrong when non-ASCII characters come earlier on the line (pycodestyle reads stdin with the locale encoding)

Version: SublimeLinter-pycodestyle 3.x from Package Control, pycodestyle 2.15.0, Python 3.12 on Windows, SublimeLinter 4, Sublime Text 4215. Reproduced in a clean install.

m.py (UTF-8):

import os
s = "é😀"; x=1

Linting the file directly gives the right column for the semicolon (E702):

m.py:2:9: E702 multiple statements on one line (semicolon)

The linter runs pycodestyle - and sends the buffer on stdin, and then the same error is reported at column 13:

stdin:2:13: E702 multiple statements on one line (semicolon)

In Sublime the highlight lands 4 characters too far to the right (on the 1 of x=1 instead of the ;). Every non-ASCII character earlier on the line moves it further (é: 1, 😀: 3 on this line).

Cause: pycodestyle reads stdin with io.TextIOWrapper(sys.stdin.buffer, errors='ignore').read() (stdin_get_value), which uses the locale encoding, cp1252 on Windows, although SublimeLinter sends UTF-8. The two bytes of é are read as two characters, and the four bytes of 😀 as four. (Bytes that cp1252 does not define are dropped, because of errors='ignore'.) PYTHONIOENCODING does not help, but Python's UTF-8 mode does:

> pycodestyle - < m.py
stdin:2:13: E702 ...
> set PYTHONUTF8=1
> pycodestyle - < m.py
stdin:2:9: E702 ...

Verified in Sublime: adding

"linters": { "pycodestyle": { "env": { "PYTHONUTF8": "1" } } }

to the SublimeLinter settings gives the correct highlight.

Suggested fix: put 'env': {'PYTHONUTF8': '1'} in the linter's defaults, so it works out of the box (it is harmless on other platforms). The underlying problem is in pycodestyle's stdin reader, so it may be worth a report there too; I have not filed one.

dpc00 avatar Sep 29 '26 21:09 dpc00