greenbone / autohooks

Library for managing git hooks
https://greenbone.github.io/autohooks/
GNU General Public License v3.0
187 stars 22 forks source link

commit fails on Windows with Python encoding error #161

Closed nextstate closed 3 years ago

nextstate commented 3 years ago

I setup the git pre-commit hook using autohooks. My first attempt at a commit resulted in the following traceback:

Traceback (most recent call last):
  File "F:\dev\project\user\formatting\.git\hooks\pre-commit", line 8, in <module>
    sys.exit(run())
  File "G:\miniconda\envs\myenv\lib\site-packages\autohooks\precommit\run.py", line 103, in run
    term.bold_info('autohooks => pre-commit')
  File "G:\miniconda\envs\myenv\lib\site-packages\autohooks\terminal.py", line 116, in bold_info
    self._print_status(message, Signs.INFO, cf.cyan, style)
  File "G:\miniconda\envs\myenv\lib\site-packages\autohooks\terminal.py", line 79, in _print_status
    print(style(output))
  File "G:\miniconda\envs\myenv\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2139' in position 0: character maps to <undefined>
y0urself commented 3 years ago

So your console/terminal is not able to print unicode characters?

nextstate commented 3 years ago

I am using powershell in a Windows terminal. I think it can print unicode characters.

y0urself commented 3 years ago

it is this symbol: ℹ

I wonder if it is a general problem on windows with python, because I don't even use that character by that UTF-code, but like this: https://github.com/greenbone/autohooks/blob/master/autohooks/terminal.py#L34

nextstate commented 3 years ago

The following line in a script prints out that symbol in my terminal:

print(u'\N{INFORMATION SOURCE}')

y0urself commented 3 years ago

I am really not into powershell or windows terminals ... maybe something like this (a wrong shell setup in the virtual environment?)?

nextstate commented 3 years ago

That link helped fix the issue locally for me. The following lines worked in powershell:

chcp 65001
$env:PYTHONIOENCODING = "utf-8"
y0urself commented 3 years ago

Since I think this is a shell-setup issue, I will close this for now, but will keep in mind, that we might want to use non utf-8 backup characters ...