python3.rst
This file
Path
docs/python3.rst
154 lines · 6.3 KiB
Last changed by
Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
History of this fileTake it
The walk stopped after 200 commits: lines older than de338be point at it.
Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 1–5why, one step back1Python 3 Support2================34.. currentmodule:: click5Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 6why, one step back6Click supports Python 3, but like all other command line utility libraries,Updated the repo example a littleArmin Ronacherbefore adoptionde338be
line 7why, one step back7it suffers from the Unicode text model in Python 3. All examples in theMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 8why, one step back8documentation were written so that they could run on both Python 2.x andUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 9–10why, one step back9Python 3.3 or higher.10Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 11why, one step back11At the moment, it is strongly recommended is to use Python 2 for clickUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 12–18why, one step back12utilities unless Python 3 is a hard requirement.1314.. _python3-limitations:1516Python 3 Limitations17--------------------18Documentation fixesKevin Yapbefore adoption67523af
line 19why, one step back19At the moment, click suffers from a few problems with Python 3:Updated the repo example a littleArmin Ronacherbefore adoptionde338be
line 20why, one step back20Documentation fixesKevin Yapbefore adoption67523af
line 21why, one step back21* The command line in Unix traditionally is in bytes, and not Unicode.Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 22–25why, one step back22 While there are encoding hints for all of this, there are generally23 some situations where this can break. The most common one is SSH24 connections to machines with different locales.25Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 26why, one step back26 Misconfigured environments can currently cause a wide range of UnicodeMerge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
line 27why, one step back27 problems in Python 3 due to the lack of support for roundtrippingUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 28–31why, one step back28 surrogate escapes. This will not be fixed in click itself!2930 For more information see :ref:`python3-surrogates`.31Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
line 32why, one step back32* Standard input and output in Python 3 is opened in Unicode mode byUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 33–50why, one step back33 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 when36 testing command-line applications.3738 This is not supported::3940 sys.stdin = io.StringIO('Input here')41 sys.stdout = io.StringIO()4243 Instead you need to do this::4445 input = 'Input here'46 in_stream = io.BytesIO(input.encode('utf-8'))47 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')48 out_stream = io.BytesIO()49 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')50Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 51why, one step back51 Remember that in that case, you need to use ``out_stream.getvalue()``Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 52–54why, one step back52 and not ``sys.stdout.getvalue()`` if you want to access the buffer53 contents as the wrapper will not forward that method.54Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 55why, one step back55Python 2 and 3 DifferencesFixed underlineArmin Ronacherbefore adoption525cc94
line 56why, one step back56--------------------------Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 57–60why, one step back5758Click attempts to minimize the differences between Python 2 and Python 359by following the best practices for both languages.60Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 61why, one step back61In Python 2, the following is true:Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 62–63why, one step back6263* ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binaryMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 64why, one step back64 mode, but under some circumstances they support Unicode output. ClickUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
line 65why, one step back65 attempts to not subvert this but provides support for forcing streamsMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 66why, one step back66 to be Unicode-based.Documentation fixesKevin Yapbefore adoption67523af
line 67why, one step back67* ``sys.argv`` is always byte-based. Click will pass bytes to allUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 68–70why, one step back68 input types and convert as necessary. The :class:`STRING` type69 automatically will decode properly the input value into a string by70 trying the most appropriate encodings.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 71why, one step back71* When dealing with files, click will never go through the Unicode APIsUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 72–74why, one step back72 and will instead use the operating system's byte APIs to open the73 files.74Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 75why, one step back75In Python 3, the following is true:Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 76–80why, one step back7677* ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by default78 text-based. When click needs a binary stream, it attempts to discover79 the underlying binary stream. See :ref:`python3-limitations` for how80 this works.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 81why, one step back81* ``sys.argv`` is always Unicode-based. This also means that the nativeDocumentation fixesKevin Yapbefore adoption67523af
line 82why, one step back82 type for input values to the types in click is Unicode, and not bytes.Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 83–84why, one step back8384 This causes problems when the terminal is incorrectly set and PythonMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 85why, one step back85 does not figure out the encoding. In that case, the Unicode stringUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
line 86why, one step back86 will contain error bytes encoded as surrogate escapes.Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 87why, one step back87* When dealing with files, click will always use the Unicode file systemUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 88–97why, one step back88 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---------------------------97Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
lines 98–99why, one step back98Click in Python 3 does all the Unicode handling in the standard library99and is subject to its behavior. In Python 2, click does all the UnicodeDocumentation fixesKevin Yapbefore adoption67523af
line 100why, one step back100handling itself, which means there are differences in error behavior.Updated the repo example a littleArmin Ronacherbefore adoptionde338be
line 101why, one step back101Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
lines 102–103why, one step back102The most glaring difference is that in Python 2, Unicode will "just work",103while in Python 3, it requires extra care. The reason for this is that onMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 104why, one step back104Python 3, the encoding detection is done in the interpreter and on LinuxUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 105–106why, one step back105and certain other operating systems its encoding handling is problematic.106Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 107why, one step back107The biggest source of frustration is that click scripts invoked byUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
line 108why, one step back108init systems (sysvinit, upstart, systemd, etc.), deployment tools (salt,Documentation fixesKevin Yapbefore adoption67523af
line 109why, one step back109puppet), or cron jobs (cron) will refuse to work unless a Unicode locale isUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 110–114why, one step back110exported.111112If click encounters such an environment it will prevent further execution113to force you to set a locale. This is done because click cannot know114about the state of the system once it's invoked and restore the valuesMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 115why, one step back115before Python's Unicode handling kicked in.Updated the repo example a littleArmin Ronacherbefore adoptionde338be
line 116why, one step back116Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a
line 117why, one step back117If you see something like this error in Python 3::Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 118–126why, one step back118119 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 areMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 127why, one step back127restricted to ASCII data. The solution to these problems is differentUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 128–129why, one step back128depending on which locale your computer is running in.129Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 130why, one step back130For instance, if you have a German Linux machine, you can fix the problemUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 131–136why, one step back131by 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 a US machine, ``en_EN.utf-8`` is the encoding of choice. OnMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 137why, one step back137some newer Linux systems, you could also try ``C.UTF-8`` as the locale::Updated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 138–142why, one step back138139 export LC_ALL=C.UTF-8140 export LANG=C.UTF-8141142You need to do this before you invoke your Python script. If you areMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 143why, one step back143curious about the reasons for this, you can join the discussions in theUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
lines 144–152why, one step back144Python 3 bug tracker:145146* `ASCII is a bad filesystem default encoding147 <http://bugs.python.org/issue13643#msg149941>`_148* `Use surrogateescape as default error handler149 <http://bugs.python.org/issue19977>`_150* `Python 3 raises Unicode errors in the C locale151 <http://bugs.python.org/issue19846>`_152* `LC_CTYPE=C: pydoc leaves terminal in an unusable stateMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 153why, one step back153 <http://bugs.python.org/issue21398>`_ (this is relevant to clickUpdated the repo example a littleArmin Ronacherbefore adoptionde338be
line 154why, one step back154 because the pager support is provided by the stdlib pydoc module)