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
Provenance
- Commit
51ebc82- Tree
a818f63- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
535a835
2 files changed+65 added−3 removed
modified:click/core.py+1 added-1 removed
307 unmodified lines308 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.')314 315 if args is None:316 args = sys.argv[1:]558 unmodified linesmodified:docs/python3.rst+64 added-2 removed
23 unmodified lines24 connections to machines with different locales.25 26 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!29 30+ For more information see :ref:`python3-surrogates`.31+32 * 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 when52 unmodified lines88 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.92+93+.. _python3-surrogates:94+95+Python 3 Surrogate Handling96+---------------------------97+98+Click on Python 3 does all the unicode handling in the standard library99+and is subject to it's behavior. On Python 2 click does all the unicode100+handling itself which means there are differences in error behavior.101+102+The most glaring difference is that on Python 2 Unicode will "just work"103+but requires extra care on Python 3. The reason for this is that on104+Python 3 the encoding detection is done in the interpreter and on Linux105+and certain other operating systems it's encoding handling is problematic.106+107+The biggest source of frustration there is that click scripts invoked by108+init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,109+puppet) or cronjobs (cron) will refuse to work unless a unicode locale is110+exported.111+112+If click encounters such an environment it will prevent further execution113+to force you to force a locale. This is done because click cannot know114+about the state of the system once it's invoked and restore the values115+before Python's unicode handling kicked in.116+117+If you see something like this error on Python 3::118+119+ 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.125+126+You are dealing with an environment where Python 3 thinks you are127+restricted to ASCII data. The solution for these problems is different128+depending on which locale your computer is running in.129+130+For instance if you have a German linux machine you can fix the problem131+by exporting the locale to ``de_DE.utf-8``::132+133+ export LC_ALL=de_DE.utf-8134+ export LANG=de_DE.utf-8135+136+If you are on an american machine ``en_EN.utf-8`` is the encoding of137+choice. On some newer linux systems you can also try ``C.UTF-8`` as138+locale::139+140+ export LC_ALL=C.UTF-8141+ export LANG=C.UTF-8142+143+You need to do this before you invoke your Python script. If you are144+curious about the reasons for this you can join the discussions in the145+Python 3 bug tracker:146+147+* `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>`_