Skip to content
Myr

unicode-support.md

This file

Path

docs/unicode-support.md

100 lines · 4.2 KiB · Markdown

Last changed by

Merge stable into main (#3001)Rowlando13before adoption834e04a

History of this file

Take it

Merge stable into main (#3001)Rowlando13before adoption834e04a

lines 1–100why, one step back
1# Unicode Support
23```{currentmodule} click4```56Click has to take extra care to support Unicode text in different environments.78- The command line in Unix is traditionally bytes, not Unicode. While there are encoding hints, there are some9 situations where this can break. The most common one is SSH connections to machines with different locales.1011 Misconfigured environments can cause a wide range of Unicode problems due to the lack of support for roundtripping12 surrogate escapes. This will not be fixed in Click itself!1314- Standard input and output is opened in text mode by default. Click has to reopen the stream in binary mode in certain15 situations. Because there is no standard way to do this, it might not always work. Primarily this can become a problem16 when testing command-line applications.1718 This is not supported:1920 ```python21 sys.stdin = io.StringIO('Input here')22 sys.stdout = io.StringIO()23 ```2425 Instead you need to do this:2627 ```python28 input = 'Input here'29 in_stream = io.BytesIO(input.encode('utf-8'))30 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')31 out_stream = io.BytesIO()32 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')33 ```3435 Remember in that case, you need to use `out_stream.getvalue()` and not `sys.stdout.getvalue()` if you want to access36 the buffer contents as the wrapper will not forward that method.3738- `sys.stdin`, `sys.stdout` and `sys.stderr` are by default text-based. When Click needs a binary stream, it attempts to39 discover the underlying binary stream.4041- `sys.argv` is always text. This means that the native type for input values to the types in Click is Unicode, not42 bytes.4344 This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case, the45 Unicode string will contain error bytes encoded as surrogate escapes.4647- When dealing with files, Click will always use the Unicode file system API by using the operating system's reported or48 guessed filesystem encoding. Surrogates are supported for filenames, so it should be possible to open files through49 the {func}`File` type even if the environment is misconfigured.5051## Surrogate Handling5253Click does all the Unicode handling in the standard library and is subject to its behavior. Unicode requires extra care.54The reason for this is that the encoding detection is done in the interpreter, and on Linux and certain other operating55systems, its encoding handling is problematic.5657The biggest source of frustration is that Click scripts invoked by init systems, deployment tools, or cron jobs will58refuse to work unless a Unicode locale is exported.5960If Click encounters such an environment it will prevent further execution to force you to set a locale. This is done61because Click cannot know about the state of the system once it's invoked and restore the values before Python's Unicode62handling kicked in.6364If you see something like this error:6566```console67Traceback (most recent call last):68 ...69RuntimeError: Click will abort further execution because Python was70 configured to use ASCII as encoding for the environment. Consult71 https://click.palletsprojects.com/unicode-support/ for mitigation72 steps.73```7475You are dealing with an environment where Python thinks you are restricted to ASCII data. The solution to these problems76is different depending on which locale your computer is running in.7778For instance, if you have a German Linux machine, you can fix the problem by exporting the locale to `de_DE.utf-8`:7980```console81export LC_ALL=de_DE.utf-882export LANG=de_DE.utf-883```8485If you are on a US machine, `en_US.utf-8` is the encoding of choice. On some newer Linux systems, you could also try86`C.UTF-8` as the locale:8788```console89export LC_ALL=C.UTF-890export LANG=C.UTF-891```9293On some systems it was reported that `UTF-8` has to be written as `UTF8` and vice versa. To see which locales are94supported you can invoke `locale -a`.9596You need to export the values before you invoke your Python script.9798In Python 3.7 and later you will no longer get a `RuntimeError` in many cases thanks to {pep}`538` and {pep}`540`, which99changed the default assumption in unconfigured environments. This doesn't change the general issue that your locale may100be misconfigured.