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 fileTake it
move header to top of fileJaron Rosenaubefore adoptiond52eaad
lines 1–2why, one step back1# Unicode Support2add currentmodule back in myst form and remove click. additionsdenuowebbefore adoptionbea2a2e
lines 3–4why, one step back3```{currentmodule} click4```change extension to .mddenuowebbefore adoptionc002e08
line 5why, one step back5convert from reST to MySTdenuowebbefore adoption29eb946
line 6why, one step back6Click has to take extra care to support Unicode text in different environments.change extension to .mddenuowebbefore adoptionc002e08
line 7why, one step back7convert from reST to MySTdenuowebbefore adoption29eb946
lines 8–9why, one step back8- 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.change extension to .mddenuowebbefore adoptionc002e08
line 10why, one step back10convert from reST to MySTdenuowebbefore adoption29eb946
lines 11–12why, one step back11 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!change extension to .mddenuowebbefore adoptionc002e08
line 13why, one step back13convert from reST to MySTdenuowebbefore adoption29eb946
lines 14–16why, one step back14- 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.change extension to .mddenuowebbefore adoptionc002e08
line 17why, one step back17convert from reST to MySTdenuowebbefore adoption29eb946
line 18why, one step back18 This is not supported:change extension to .mddenuowebbefore adoptionc002e08
line 19why, one step back19convert from reST to MySTdenuowebbefore adoption29eb946
lines 20–23why, one step back20 ```python21 sys.stdin = io.StringIO('Input here')22 sys.stdout = io.StringIO()23 ```change extension to .mddenuowebbefore adoptionc002e08
line 24why, one step back24convert from reST to MySTdenuowebbefore adoption29eb946
line 25why, one step back25 Instead you need to do this:change extension to .mddenuowebbefore adoptionc002e08
line 26why, one step back26convert from reST to MySTdenuowebbefore adoption29eb946
lines 27–33why, one step back27 ```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 ```change extension to .mddenuowebbefore adoptionc002e08
line 34why, one step back34convert from reST to MySTdenuowebbefore adoption29eb946
lines 35–36why, one step back35 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.change extension to .mddenuowebbefore adoptionc002e08
line 37why, one step back37convert from reST to MySTdenuowebbefore adoption29eb946
lines 38–39why, one step back38- `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.change extension to .mddenuowebbefore adoptionc002e08
line 40why, one step back40convert from reST to MySTdenuowebbefore adoption29eb946
lines 41–42why, one step back41- `sys.argv` is always text. This means that the native type for input values to the types in Click is Unicode, not42 bytes.change extension to .mddenuowebbefore adoptionc002e08
line 43why, one step back43convert from reST to MySTdenuowebbefore adoption29eb946
lines 44–45why, one step back44 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.change extension to .mddenuowebbefore adoptionc002e08
line 46why, one step back46convert from reST to MySTdenuowebbefore adoption29eb946
lines 47–48why, one step back47- 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 throughremove click. with currentmoduledenuowebbefore adoption4ef94fc
line 49why, one step back49 the {func}`File` type even if the environment is misconfigured.change extension to .mddenuowebbefore adoptionc002e08
line 50why, one step back50convert from reST to MySTdenuowebbefore adoption29eb946
line 51why, one step back51## Surrogate Handlingchange extension to .mddenuowebbefore adoptionc002e08
line 52why, one step back52convert from reST to MySTdenuowebbefore adoption29eb946
lines 53–55why, one step back53Click 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 back56convert from reST to MySTdenuowebbefore adoption29eb946
lines 57–58why, one step back57The 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.change extension to .mddenuowebbefore adoptionc002e08
line 59why, one step back59convert from reST to MySTdenuowebbefore adoption29eb946
lines 60–62why, one step back60If 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.change extension to .mddenuowebbefore adoptionc002e08
line 63why, one step back63convert from reST to MySTdenuowebbefore adoption29eb946
line 64why, one step back64If you see something like this error:change extension to .mddenuowebbefore adoptionc002e08
line 65why, one step back65convert from reST to MySTdenuowebbefore adoption29eb946
lines 66–73why, one step back66```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```change extension to .mddenuowebbefore adoptionc002e08
line 74why, one step back74convert from reST to MySTdenuowebbefore adoption29eb946
lines 75–76why, one step back75You 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.change extension to .mddenuowebbefore adoptionc002e08
line 77why, one step back77convert from reST to MySTdenuowebbefore adoption29eb946
line 78why, one step back78For 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 back79convert from reST to MySTdenuowebbefore adoption29eb946
lines 80–83why, one step back80```console81export LC_ALL=de_DE.utf-882export LANG=de_DE.utf-883```change extension to .mddenuowebbefore adoptionc002e08
line 84why, one step back84convert from reST to MySTdenuowebbefore adoption29eb946
lines 85–86why, one step back85If 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:change extension to .mddenuowebbefore adoptionc002e08
line 87why, one step back87convert from reST to MySTdenuowebbefore adoption29eb946
lines 88–91why, one step back88```console89export LC_ALL=C.UTF-890export LANG=C.UTF-891```change extension to .mddenuowebbefore adoptionc002e08
line 92why, one step back92convert from reST to MySTdenuowebbefore adoption29eb946
lines 93–94why, one step back93On 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`.change extension to .mddenuowebbefore adoptionc002e08
lines 95–97why, one step back9596You need to export the values before you invoke your Python script.97convert from reST to MySTdenuowebbefore adoption29eb946
lines 98–100why, one step back98In 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.