Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnicodeDecodeError when running yamllint under certain Windows code pages #711

Closed

Conversation

tahakiziltepe
Copy link

This PR addresses an issue where running yamllint (via pre-commit) on Windows with a non-UTF-8 code page (like CP1254) could trigger a UnicodeDecodeError. The problem occurred because yamllint was attempting to read and decode the file contents using the default system encoding, which didn’t properly handle certain characters.

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\<pyenv>\Scripts\yamllint.EXE\__main__.py", line 7, in <module>   
  File "C:\<pyenv>\Lib\site-packages\yamllint\cli.py", line 223, in run     
    problems = linter.run(f, conf, filepath)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\<pyenv>\Lib\site-packages\yamllint\linter.py", line 232, in run  
    content = input.read()
              ^^^^^^^^^^^^
  File "C:\<python>\Lib\encodings\cp1254.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 296: character maps to <undefined>

To fix this, I updated the file-reading logic to:

  • Check if input.encoding is set and if it’s neither UTF-8 nor buffer.
  • If so, read from input.buffer directly.
  • Otherwise, read and decode with UTF-8.

And it worked well:
image

Thanks for reviewing.

@tahakiziltepe tahakiziltepe deleted the fix-encoding_handling branch March 3, 2025 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant