Skip to content
Myr

Added notes on Python 3 support

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 50690e4 · Browse files · 3 files+108 added−4 removed
This commit

State

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

Diff stats

3 files+108 added−4 removed

  • other:+108 added−4 removed

3 files+108 added−4 removed

modified:docs/index.rst+1 added
54 unmodified lines
55 documentation56 setuptools57 complex58 advanced59 python36061API Reference62-------------63
7 unmodified lines
added:docs/python3.rst+89 added
1Python 3 Support2================34.. currentmodule:: click56Click supports Python 3 but like all other command line utility libraries,7click suffers from the Unicode text model in Python 3. All examples in8the documentation were written so that they run on both Python 2.x and9Python 3.3 or higher.1011At the moment the strong recommendation is to use Python 2 for these12utilities unless you can avoid it.1314.. _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.22 While there are encoding hits for all of this, there are generally23 some situations where this can break. The most common one is SSH24 connections to machines with different locale.2526 As such in misconfigured environments on Python 3 currently can cause27 a wide range of unicode problems caused by lack of support for28 roundtripping surrogate escapes. This will be fixed on a case by case29 basis going forward.3031* 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, this34 might not always work. Primarily this can become a problem in when35 testing command line applications.3637 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'))46 sys.stdin = io.TextIOWrapper(stream, encoding='utf-8')47 out_stream = io.BytesIO()48 sys.stdout = io.TextIOWrapper(out_stream, encoding='utf-8')4950 Remember that in that case you ened to use ``out_stream.getvalue()``51 and not ``sys.stdout.getvalue()`` if you want to access the buffer52 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 binary63 mode but under some circumstances support unicode output. Click64 attempts to not subvert this but provides support for forcing streams65 to be unicode based.66* ``sys.argv`` is always bytes based. Click will pass bytes to all67 input types and convert as necessary. The :class:`STRING` type68 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 APIs71 and instead use the operating system's byte APIs to open the files.7273On Python 3 the following is true:7475* ``sys.stdin``, ``sys.stdout`` and ``sys.stderr`` are by text based.76 When click needs a binary stream it attempts to discover the77 nderlying binary stream. See :ref:`python3-limitations` for how this78 works.79* ``sys.argv`` is always unicode based. This also means that the native80 type for input values to the types in click is unicode and not bytes.8182 This causes problems when the terminal is incorrectly set and Python83 does not figure out the encoding. In that case the unicode string84 will contain error bytes encoded as surrogate escapes.85* When dealing with files click will always use the unicode file system86 API calls by using the operating system's reported or guessed87 filesystem encoding. Surrogates are supported for filenames so it88 should be possible to open files through the :class:`File` type even89 if the environment is misconfigured.
modified:click.py+18 added-4 removed
46 unmodified lines
47 encoding = sys.stdin.encoding48 f = _wrap_stream_for_codec(f, encoding, errors)49 return f, False50else:51 import io52 text_type = str53 raw_input = input5455 def _make_binary_stream(f, stream_name):56 buf = getattr(f, 'buffer', None)57 if buf is not None and isinstance(buf, io.BufferedReader):58 return buf59 raise TypeError('%s is a stream that does not give access to its '60 'underlying binary stream. Please do not set '61 'sys.stdout directly to a StringIO object or '62 'something similar.' % stream_name)6364 def open_stream(filename, mode='r', encoding=None, errors='strict'):65 if filename != '-':66 if encoding is not None:57 return open(filename, mode, encoding=encoding, errors=errors), True67 return open(filename, mode, encoding=encoding,68 errors=errors), True69 return open(filename, mode), True7071 if 'w' in mode:72 f = sys.stdout73 if encoding is not None or 'b' in mode:62 f = f.buffer.raw74 f = _make_binary_stream(f, 'stdout')75 if encoding is not None:76 f = _wrap_stream_for_codec(f, encoding, errors)77 else:78 f = sys.stdin79 if 'b' in mode:68 f = f.buffer.raw80 f = _make_binary_stream(f, 'stdin')81 if encoding is not None:82 f = _wrap_stream_for_codec(f, encoding, errors)8384 return f, False858687def get_terminal_size():
9 unmodified lines
97 def ioctl_gwinsz(fd):98 try:99 import fcntl100 import termios88 cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))101 cr = struct.unpack(102 'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))103 except Exception:104 return105 return cr106
1580 unmodified lines