sdispater / clikit

CliKit is a group of utilities to build beautiful and testable command line interfaces.
MIT License
72 stars 17 forks source link

UnicodeDecodeError when rendering NoSuchOptionException trace #46

Open john-ingles opened 2 years ago

john-ingles commented 2 years ago

This issue happened when I entered the wrong option to a command in the poetry package manager (which uses clikit). Poetry raised a clikit.api.args.exceptions.NoSuchOptionException. During the handling of this exception, I got a UnicodeEncodeError.

clikit.api.args.exceptions.NoSuchOptionException: The "-d" option does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\John\.poetry\bin\poetry", line 19, in <module>
    main()
  File "C:\Users\John\.poetry\lib\poetry\console\__init__.py", line 5, in main
    return Application().run()
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\console_application.py", line 142, in run
    trace.render(io, simple=isinstance(e, CliKitException))
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\ui\components\exception_trace.py", line 232, in render
    return self._render_exception(io, self._exception)
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\ui\components\exception_trace.py", line 269, in _render_exception
    self._render_snippet(io, current_frame)
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\ui\components\exception_trace.py", line 289, in _render_snippet
    self._render_line(io, code_line)
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\ui\components\exception_trace.py", line 402, in _render_line
    io.write_line("{}{}".format(indent * " ", line))
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\api\io\io.py", line 66, in write_line
    self._output.write_line(string, flags=flags)
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\api\io\output.py", line 69, in write_line
    self.write(string, flags=flags, new_line=True)
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\api\io\output.py", line 61, in write
    self._stream.write(to_str(formatted))
  File "C:\Users\John\.poetry\lib\poetry\_vendor\py3.9\clikit\io\output_stream\stream_output_stream.py", line 24, in write
    self._stream.write(string)
  File "C:\Users\John\AppData\Local\Programs\Python\Python39\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 '\u2502' in position 27: character maps to <undefined>
john-ingles commented 2 years ago

As far as I can tell _stream.write(string) is just calling stdout.write(string). I don't think stdout can detect encoding on its own.