Skip to content
Myr

unicode-support.rst

This file

Path

docs/unicode-support.rst

112 lines · 4.3 KiB

Last changed by

Not known.

History of this file

Take it

1Unicode Support2===============34.. currentmodule:: click56Click has to take extra care to support Unicode text in different7environments.89*   The command line in Unix is traditionally bytes, not Unicode. While10    there are encoding hints, there are some situations where this can11    break. The most common one is SSH connections to machines with12    different locales.1314    Misconfigured environments can cause a wide range of Unicode15    problems due to the lack of support for roundtripping surrogate16    escapes. This will not be fixed in Click itself!1718*   Standard input and output is opened in text mode by default. Click19    has to reopen the stream in binary mode in certain situations.20    Because there is no standard way to do this, it might not always21    work. Primarily this can become a problem when testing command-line22    applications.2324    This is not supported::2526        sys.stdin = io.StringIO('Input here')27        sys.stdout = io.StringIO()2829    Instead you need to do this::3031        input = 'Input here'32        in_stream = io.BytesIO(input.encode('utf-8'))33        sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')34        out_stream = io.BytesIO()35        sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')3637    Remember in that case, you need to use ``out_stream.getvalue()``38    and not ``sys.stdout.getvalue()`` if you want to access the buffer39    contents as the wrapper will not forward that method.4041*   ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by default42    text-based. When Click needs a binary stream, it attempts to43    discover the underlying binary stream.4445*   ``sys.argv`` is always text. This means that the native type for46    input values to the types in Click is Unicode, not bytes.4748    This causes problems if the terminal is incorrectly set and Python49    does not figure out the encoding. In that case, the Unicode string50    will contain error bytes encoded as surrogate escapes.5152*   When dealing with files, Click will always use the Unicode file53    system API by using the operating system's reported or guessed54    filesystem encoding. Surrogates are supported for filenames, so it55    should be possible to open files through the :class:`File` type even56    if the environment is misconfigured.575859Surrogate Handling60------------------6162Click does all the Unicode handling in the standard library and is63subject to its behavior. Unicode requires extra care. The reason for64this is that the encoding detection is done in the interpreter, and on65Linux and certain other operating systems, its encoding handling is66problematic.6768The biggest source of frustration is that Click scripts invoked by init69systems, deployment tools, or cron jobs will refuse to work unless a70Unicode locale is exported.7172If Click encounters such an environment it will prevent further73execution to force you to set a locale. This is done because Click74cannot know about the state of the system once it's invoked and restore75the values before Python's Unicode handling kicked in.7677If you see something like this error::7879    Traceback (most recent call last):80      ...81    RuntimeError: Click will abort further execution because Python was82      configured to use ASCII as encoding for the environment. Consult83      https://click.palletsprojects.com/unicode-support/ for mitigation84      steps.8586You are dealing with an environment where Python thinks you are87restricted to ASCII data. The solution to these problems is different88depending on which locale your computer is running in.8990For instance, if you have a German Linux machine, you can fix the91problem by exporting the locale to ``de_DE.utf-8``::9293    export LC_ALL=de_DE.utf-894    export LANG=de_DE.utf-89596If you are on a US machine, ``en_US.utf-8`` is the encoding of choice.97On some newer Linux systems, you could also try ``C.UTF-8`` as the98locale::99100    export LC_ALL=C.UTF-8101    export LANG=C.UTF-8102103On some systems it was reported that ``UTF-8`` has to be written as104``UTF8`` and vice versa. To see which locales are supported you can105invoke ``locale -a``.106107You need to export the values before you invoke your Python script.108109In Python 3.7 and later you will no longer get a ``RuntimeError`` in110many cases thanks to :pep:`538` and :pep:`540`, which changed the111default assumption in unconfigured environments. This doesn't change the112general issue that your locale may be misconfigured.