Skip to content
Myr

unicode-support.md

This file

Path

docs/unicode-support.md

100 lines · 4.2 KiB · Markdown

Last changed by

move header to top of fileJaron Rosenaubefore adoptiond52eaad

History of this file

Take it

move header to top of fileJaron Rosenaubefore adoptiond52eaad

lines 1–2why, one step back
1# Unicode Support
2

add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e

lines 3–4why, one step back
3```{currentmodule} click
4```

change extension to .mddenuowebbefore adoptionc002e08

line 5why, one step back
5

convert from reST to MySTdenuowebbefore adoption29eb946

line 6why, one step back
6Click has to take extra care to support Unicode text in different environments.

change extension to .mddenuowebbefore adoptionc002e08

line 7why, one step back
7

convert from reST to MySTdenuowebbefore adoption29eb946

lines 8–9why, one step back
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.

change extension to .mddenuowebbefore adoptionc002e08

line 10why, one step back
10

convert from reST to MySTdenuowebbefore adoption29eb946

lines 11–12why, one step back
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!

change extension to .mddenuowebbefore adoptionc002e08

line 13why, one step back
13

convert from reST to MySTdenuowebbefore adoption29eb946

lines 14–16why, one step back
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 problem16 when testing command-line applications.

change extension to .mddenuowebbefore adoptionc002e08

line 17why, one step back
17

convert from reST to MySTdenuowebbefore adoption29eb946

line 18why, one step back
18 This is not supported:

change extension to .mddenuowebbefore adoptionc002e08

line 19why, one step back
19

convert from reST to MySTdenuowebbefore adoption29eb946

lines 20–23why, one step back
20 ```python
21 sys.stdin = io.StringIO('Input here')22 sys.stdout = io.StringIO()23 ```

change extension to .mddenuowebbefore adoptionc002e08

line 24why, one step back
24

convert from reST to MySTdenuowebbefore adoption29eb946

line 25why, one step back
25 Instead you need to do this:

change extension to .mddenuowebbefore adoptionc002e08

line 26why, one step back
26

convert from reST to MySTdenuowebbefore adoption29eb946

lines 27–33why, one step back
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 ```

change extension to .mddenuowebbefore adoptionc002e08

line 34why, one step back
34

convert from reST to MySTdenuowebbefore adoption29eb946

lines 35–36why, one step back
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.

change extension to .mddenuowebbefore adoptionc002e08

line 37why, one step back
37

convert from reST to MySTdenuowebbefore adoption29eb946

lines 38–39why, one step back
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.

change extension to .mddenuowebbefore adoptionc002e08

line 40why, one step back
40

convert from reST to MySTdenuowebbefore adoption29eb946

lines 41–42why, one step back
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.

change extension to .mddenuowebbefore adoptionc002e08

line 43why, one step back
43

convert from reST to MySTdenuowebbefore adoption29eb946

lines 44–45why, one step back
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.

change extension to .mddenuowebbefore adoptionc002e08

line 46why, one step back
46

convert from reST to MySTdenuowebbefore adoption29eb946

lines 47–48why, one step back
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

remove click. with currentmoduledenuowebbefore adoption4ef94fc

line 49why, one step back
49 the {func}`File` type even if the environment is misconfigured.

change extension to .mddenuowebbefore adoptionc002e08

line 50why, one step back
50

convert from reST to MySTdenuowebbefore adoption29eb946

line 51why, one step back
51## Surrogate Handling

change extension to .mddenuowebbefore adoptionc002e08

line 52why, one step back
52

convert from reST to MySTdenuowebbefore adoption29eb946

lines 53–55why, one step back
53Click 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.

change extension to .mddenuowebbefore adoptionc002e08

line 56why, one step back
56

convert from reST to MySTdenuowebbefore adoption29eb946

lines 57–58why, one step back
57The biggest source of frustration is that Click scripts invoked by init systems, deployment tools, or cron jobs will
58refuse to work unless a Unicode locale is exported.

change extension to .mddenuowebbefore adoptionc002e08

line 59why, one step back
59

convert from reST to MySTdenuowebbefore adoption29eb946

lines 60–62why, one step back
60If Click encounters such an environment it will prevent further execution to force you to set a locale. This is done
61because Click cannot know about the state of the system once it's invoked and restore the values before Python's Unicode62handling kicked in.

change extension to .mddenuowebbefore adoptionc002e08

line 63why, one step back
63

convert from reST to MySTdenuowebbefore adoption29eb946

line 64why, one step back
64If you see something like this error:

change extension to .mddenuowebbefore adoptionc002e08

line 65why, one step back
65

convert from reST to MySTdenuowebbefore adoption29eb946

lines 66–73why, one step back
66```console
67Traceback (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```

change extension to .mddenuowebbefore adoptionc002e08

line 74why, one step back
74

convert from reST to MySTdenuowebbefore adoption29eb946

lines 75–76why, one step back
75You are dealing with an environment where Python thinks you are restricted to ASCII data. The solution to these problems
76is different depending on which locale your computer is running in.

change extension to .mddenuowebbefore adoptionc002e08

line 77why, one step back
77

convert from reST to MySTdenuowebbefore adoption29eb946

line 78why, one step back
78For instance, if you have a German Linux machine, you can fix the problem by exporting the locale to `de_DE.utf-8`:

change extension to .mddenuowebbefore adoptionc002e08

line 79why, one step back
79

convert from reST to MySTdenuowebbefore adoption29eb946

lines 80–83why, one step back
80```console
81export LC_ALL=de_DE.utf-882export LANG=de_DE.utf-883```

change extension to .mddenuowebbefore adoptionc002e08

line 84why, one step back
84

convert from reST to MySTdenuowebbefore adoption29eb946

lines 85–86why, one step back
85If 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:

change extension to .mddenuowebbefore adoptionc002e08

line 87why, one step back
87

convert from reST to MySTdenuowebbefore adoption29eb946

lines 88–91why, one step back
88```console
89export LC_ALL=C.UTF-890export LANG=C.UTF-891```

change extension to .mddenuowebbefore adoptionc002e08

line 92why, one step back
92

convert from reST to MySTdenuowebbefore adoption29eb946

lines 93–94why, one step back
93On some systems it was reported that `UTF-8` has to be written as `UTF8` and vice versa. To see which locales are
94supported you can invoke `locale -a`.

change extension to .mddenuowebbefore adoptionc002e08

lines 95–97why, one step back
95
96You need to export the values before you invoke your Python script.97

convert from reST to MySTdenuowebbefore adoption29eb946

lines 98–100why, one step back
98In Python 3.7 and later you will no longer get a `RuntimeError` in many cases thanks to {pep}`538` and {pep}`540`, which
99changed the default assumption in unconfigured environments. This doesn't change the general issue that your locale may100be misconfigured.