Debian Codegraph @

docs/unicode-support.md

raw
1 # Unicode Support
2
3 ```{currentmodule} click
4 ```
5
6 Click has to take extra care to support Unicode text in different environments.
7
8 - The command line in Unix is traditionally bytes, not Unicode. While there are encoding hints, there are some
9 situations where this can break. The most common one is SSH connections to machines with different locales.
10
11 Misconfigured environments can cause a wide range of Unicode problems due to the lack of support for roundtripping
12 surrogate escapes. This will not be fixed in Click itself!
13
14 - Standard input and output is opened in text mode by default. Click has to reopen the stream in binary mode in certain
15 situations. Because there is no standard way to do this, it might not always work. Primarily this can become a problem
16 when testing command-line applications.
17
18 This is not supported:
19
20 ```python
21 sys.stdin = io.StringIO('Input here')
22 sys.stdout = io.StringIO()
23 ```
24
25 Instead you need to do this:
26
27 ```python
28 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 ```
34
35 Remember in that case, you need to use `out_stream.getvalue()` and not `sys.stdout.getvalue()` if you want to access
36 the buffer contents as the wrapper will not forward that method.
37
38 - `sys.stdin`, `sys.stdout` and `sys.stderr` are by default text-based. When Click needs a binary stream, it attempts to
39 discover the underlying binary stream.
40
41 - `sys.argv` is always text. This means that the native type for input values to the types in Click is Unicode, not
42 bytes.
43
44 This causes problems if the terminal is incorrectly set and Python does not figure out the encoding. In that case, the
45 Unicode string will contain error bytes encoded as surrogate escapes.
46
47 - When dealing with files, Click will always use the Unicode file system API by using the operating system's reported or
48 guessed filesystem encoding. Surrogates are supported for filenames, so it should be possible to open files through
49 the {func}`File` type even if the environment is misconfigured.
50
51 ## Surrogate Handling
52
53 Click does all the Unicode handling in the standard library and is subject to its behavior. Unicode requires extra care.
54 The reason for this is that the encoding detection is done in the interpreter, and on Linux and certain other operating
55 systems, its encoding handling is problematic.
56
57 The biggest source of frustration is that Click scripts invoked by init systems, deployment tools, or cron jobs will
58 refuse to work unless a Unicode locale is exported.
59
60 If Click encounters such an environment it will prevent further execution to force you to set a locale. This is done
61 because Click cannot know about the state of the system once it's invoked and restore the values before Python's Unicode
62 handling kicked in.
63
64 If you see something like this error:
65
66 ```console
67 Traceback (most recent call last):
68 ...
69 RuntimeError: Click will abort further execution because Python was
70 configured to use ASCII as encoding for the environment. Consult
71 https://click.palletsprojects.com/unicode-support/ for mitigation
72 steps.
73 ```
74
75 You are dealing with an environment where Python thinks you are restricted to ASCII data. The solution to these problems
76 is different depending on which locale your computer is running in.
77
78 For instance, if you have a German Linux machine, you can fix the problem by exporting the locale to `de_DE.utf-8`:
79
80 ```console
81 export LC_ALL=de_DE.utf-8
82 export LANG=de_DE.utf-8
83 ```
84
85 If you are on a US machine, `en_US.utf-8` is the encoding of choice. On some newer Linux systems, you could also try
86 `C.UTF-8` as the locale:
87
88 ```console
89 export LC_ALL=C.UTF-8
90 export LANG=C.UTF-8
91 ```
92
93 On some systems it was reported that `UTF-8` has to be written as `UTF8` and vice versa. To see which locales are
94 supported you can invoke `locale -a`.
95
96 You need to export the values before you invoke your Python script.
97
98 In Python 3.7 and later you will no longer get a `RuntimeError` in many cases thanks to {pep}`538` and {pep}`540`, which
99 changed the default assumption in unconfigured environments. This doesn't change the general issue that your locale may
100 be misconfigured.