Skip to content
Myr

python3.rst

This file

Path

docs/python3.rst

90 lines · 3.7 KiB

Last changed by

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

History of this file

Take it

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 1–6why, one step back
1Python 3 Support
2================34.. currentmodule:: click56Click supports Python 3 but like all other command line utility libraries,

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

lines 7–8why, one step back
7it suffers from the Unicode text model in Python 3. All examples in the
8documentation were written so that they run on both Python 2.x and

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 9–11why, one step back
9Python 3.3 or higher.
1011At the moment the strong recommendation is to use Python 2 for these

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 12why, one step back
12utilities unless Python 3 is a hard requirement.

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 13–21why, one step back
13
14.. _python3-limitations:1516Python 3 Limitations17--------------------1819At the moment click suffers from a few problems on Python 3:2021* The command line in Unix traditionally is in bytes and not unicode.

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 22why, one step back
22 While there are encoding hints for all of this, there are generally

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

line 23why, one step back
23 some situations where this can break. The most common one is SSH

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 24why, one step back
24 connections to machines with different locales.

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

line 25why, one step back
25

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

lines 26–27why, one step back
26 Misconfigured environments can currently cause a wide range of unicode
27 problems on Python 3 due to the lack of support for roundtripping

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 28why, one step back
28 surrogate escapes. This will be fixed on a case-by-case basis going

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 29why, one step back
29 forward.

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 30–33why, one step back
30
31* Standard input and output on Python 3 is opened in unicode mode by32 default. Click has to reopen the stream in binary mode in certain33 situations. Because there is no standardized way to do this, this

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 34why, one step back
34 might not always work. Primarily this can become a problem when

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 35why, one step back
35 testing command-line applications.

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 36–45why, one step back
36
37 This is not supported::3839 sys.stdin = io.StringIO('Input here')40 sys.stdout = io.StringIO()4142 Instead you need to do this::4344 input = 'Input here'45 in_stream = io.BytesIO(input.encode('utf-8'))

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 46why, one step back
46 sys.stdin = io.TextIOWrapper(in_stream, encoding='utf-8')

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 47–49why, one step back
47 out_stream = io.BytesIO()
48 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')49

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 50why, one step back
50 Remember that in that case you need to use ``out_stream.getvalue()``

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 51–62why, one step back
51 and not ``sys.stdout.getvalue()`` if you want to access the buffer
52 contents as the wrapper will not forward that method.5354Python 2 / 3 Differences55------------------------5657Click attempts to minimize the differences between Python 2 and Python 358by following the best practices for both languages.5960On Python 2 the following is true:6162* ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` are opened in binary

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 63why, one step back
63 mode but under some circumstances they support unicode output. Click

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

line 64why, one step back
64 attempts to not subvert this but provides support for forcing streams

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

lines 65–66why, one step back
65 to be unicode-based.
66* ``sys.argv`` is always bytes-based. Click will pass bytes to all

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 67–70why, one step back
67 input types and convert as necessary. The :class:`STRING` type
68 automatically will decode properly the input value into a string by69 trying the most appropriate encodings.70* When dealing with files, click will never go through the unicode APIs

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

lines 71–72why, one step back
71 and will instead use the operating system's byte APIs to open the
72 files.

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 73–75why, one step back
73
74On Python 3 the following is true:75

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

line 76why, one step back
76* ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by default

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 77why, one step back
77 text-based. When click needs a binary stream, it attempts to discover

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoption99cada4

lines 78–80why, one step back
78 the underlying binary stream. See :ref:`python3-limitations` for how
79 this works.80* ``sys.argv`` is always unicode-based. This also means that the native

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 81–85why, one step back
81 type for input values to the types in click is unicode and not bytes.
8283 This causes problems when the terminal is incorrectly set and Python84 does not figure out the encoding. In that case the unicode string85 will contain error bytes encoded as surrogate escapes.

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 86why, one step back
86* When dealing with files, click will always use the unicode file system

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

line 87why, one step back
87 API calls by using the operating system's reported or guessed

Merge pull request #39 from erikrose/patch-1Armin Ronacherbefore adoptiondc2381a

line 88why, one step back
88 filesystem encoding. Surrogates are supported for filenames, so it

Added notes on Python 3 supportArmin Ronacherbefore adoptionfcd6d32

lines 89–90why, one step back
89 should be possible to open files through the :class:`File` type even
90 if the environment is misconfigured.