pallets / click

Python composable command line interface toolkit
https://click.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
15.71k stars 1.4k forks source link

'replace' error handler not applied to stdout in Python 3 #705

Closed jwodder closed 4 years ago

jwodder commented 7 years ago

When printing text with click.echo, Click applies the 'replace' error handler in Python 2 but not in Python 3.

Consider this program:

import click

@click.command()
def echo_unicode():
    click.echo(u'\u2603')

if __name__ == '__main__':
    echo_unicode()

When run:

$ PYTHONIOENCODING=utf-8 python echo_unicode.py
☃
$ PYTHONIOENCODING=latin1 python echo_unicode.py
?
$ PYTHONIOENCODING=utf-8 python3 echo_unicode.py
☃
$ PYTHONIOENCODING=latin1 python3 echo_unicode.py
Traceback (most recent call last):
  File "echo_unicode.py", line 8, in <module>
    echo_unicode()
  File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
 716, in __call__
    return self.main(*args, **kwargs)
  File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
 696, in main
    rv = self.invoke(ctx)
  File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line
 889, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/core.py", line 534, in invoke
    return callback(*args, **kwargs)
  File "clack.py", line 5, in echo_unicode
    click.echo(u'\u2603')
  File "/home/jwodder/tmp/venv3/lib/python3.5/site-packages/click/utils.py", line 259, in echo
    file.write(message)
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2603' in position 0: ordinal not in range(256)
davidism commented 4 years ago

This was fixed in #1298, released with 7.1.