Skip to content
Myr

Added notes on unicode on python 3

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 535a835 · Browse files · 2 files+65 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−3 removed

  • other:+65 added−3 removed

2 files+65 added−3 removed

modified:click/core.py+1 added-1 removed
307 unmodified lines
308 raise RuntimeError('Click will abort further execution '309 'because Python 3 was configured to use '310 'ASCII as encoding for the environment. '311 'Either switch to Python 2 or consult '312 'http://bugs.python.org/issue13643 '312 'http://click.pocoo.org/python3/ '313 'for mitigation steps.')314315 if args is None:316 args = sys.argv[1:]
558 unmodified lines
modified:docs/python3.rst+64 added-2 removed
23 unmodified lines
24 connections to machines with different locales.2526 Misconfigured environments can currently cause a wide range of unicode27 problems on Python 3 due to the lack of support for roundtripping28 surrogate escapes. This will be fixed on a case-by-case basis going29 forward.28 surrogate escapes. This will not be fixed in Click itself!2930 For more information see :ref:`python3-surrogates`.3132* Standard input and output on Python 3 is opened in unicode mode by33 default. Click has to reopen the stream in binary mode in certain34 situations. Because there is no standardized way to do this, this35 might not always work. Primarily this can become a problem when
52 unmodified lines
88 API calls by using the operating system's reported or guessed89 filesystem encoding. Surrogates are supported for filenames, so it90 should be possible to open files through the :class:`File` type even91 if the environment is misconfigured.9293.. _python3-surrogates:9495Python 3 Surrogate Handling96---------------------------9798Click on Python 3 does all the unicode handling in the standard library99and is subject to it's behavior. On Python 2 click does all the unicode100handling itself which means there are differences in error behavior.101102The most glaring difference is that on Python 2 Unicode will "just work"103but requires extra care on Python 3. The reason for this is that on104Python 3 the encoding detection is done in the interpreter and on Linux105and certain other operating systems it's encoding handling is problematic.106107The biggest source of frustration there is that click scripts invoked by108init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,109puppet) or cronjobs (cron) will refuse to work unless a unicode locale is110exported.111112If click encounters such an environment it will prevent further execution113to force you to force a locale. This is done because click cannot know114about the state of the system once it's invoked and restore the values115before Python's unicode handling kicked in.116117If you see something like this error on Python 3::118119 Traceback (most recent call last):120 ...121 RuntimeError: Click will abort further execution because Python 3 was122 configured to use ASCII as encoding for the environment. Either switch123 to Python 2 or consult for http://click.pocoo.org/python3/124 mitigation steps.125126You are dealing with an environment where Python 3 thinks you are127restricted to ASCII data. The solution for these problems is different128depending on which locale your computer is running in.129130For instance if you have a German linux machine you can fix the problem131by exporting the locale to ``de_DE.utf-8``::132133 export LC_ALL=de_DE.utf-8134 export LANG=de_DE.utf-8135136If you are on an american machine ``en_EN.utf-8`` is the encoding of137choice. On some newer linux systems you can also try ``C.UTF-8`` as138locale::139140 export LC_ALL=C.UTF-8141 export LANG=C.UTF-8142143You need to do this before you invoke your Python script. If you are144curious about the reasons for this you can join the discussions in the145Python 3 bug tracker:146147* `ASCII is a bad filesystem default encoding148 <http://bugs.python.org/issue13643#msg149941>`_149* `Use surrogateescape as default error handler150 <http://bugs.python.org/issue19977>`_151* `Python 3 raises Unicode errors in the C locale152 <http://bugs.python.org/issue19846>`_