Skip to content
Myr

Always work on Unicode in Python 2.

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

Previously there was a bug which caused some strings to remain byte strings in unittesting environments.

authored by Armin Ronacher · committed by Armin Ronacher · parent 40705b9 · Browse files · 5 files+38 added−23 removed
This commit

State

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

Diff stats

5 files+38 added−23 removed

  • tests:+19 added−14 removed
  • other:+19 added−9 removed

5 files+38 added−23 removed

modified:click/_compat.py+7 added
549 unmodified lines
550 except Exception:551 return False552553554def _get_argv_encoding():555 if os.name == 'nt':556 import locale557 return locale.getpreferredencoding()558 return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()559560561def _make_cached_stream_func(src_func, wrapper_func):562 cache = WeakKeyDictionary()563 def func():564 stream = src_func()
32 unmodified lines
modified:click/types.py+9 added-9 removed
1import os2import sys2import stat34from ._compat import open_stream, text_type, filename_to_ui, \6 get_filesystem_encoding, get_streerror5 get_filesystem_encoding, get_streerror, _get_argv_encoding6from .exceptions import BadParameter7from .utils import safecall, LazyFile89
97 unmodified lines
107 name = 'text'108109 def convert(self, value, param, ctx):110 if isinstance(value, bytes):111 enc = _get_argv_encoding()112 try:113 enc = getattr(sys.stdin, 'encoding', None)114 if enc is not None:115 value = value.decode(enc)113 value = value.decode(enc)114 except UnicodeError:115 fs_enc = get_filesystem_encoding()116 if fs_enc != enc:117 try:118 value = value.decode(get_filesystem_encoding())119 except UnicodeError:120 value = value.decode('utf-8', 'replace')117 try:118 value = value.decode(fs_enc)119 except UnicodeError:120 value = value.decode('utf-8', 'replace')121 return value122 return value123124 def __repr__(self):
399 unmodified lines
modified:tests/test_arguments.py+3 added-3 removed
233 unmodified lines
234 click.echo(arg)235236 result = runner.invoke(cmd, ['a', 'b', 'c'])237 assert result.output.splitlines() == [238 "('a',)",238 "(u'a',)",239 'b',240 'c',241 ]242
8 unmodified lines
251 click.echo(arg)252253 result = runner.invoke(cmd, ['a', 'b', 'c', 'd', 'e', 'f'])254 assert result.output.splitlines() == [255 "('a', 'b', 'c')",255 "(u'a', u'b', u'c')",256 'd',257 "('e', 'f')",257 "(u'e', u'f')",258 ]
modified:tests/test_options.py+16 added-11 removed
2 unmodified lines
3import os4import click5import pytest67from click._compat import text_type8910def test_prefixes(runner):11 @click.command()12 @click.option('++foo', is_flag=True, help='das foo')13 @click.option('--bar', is_flag=True, help='das bar')
131 unmodified lines
145146147def test_multiple_default_help(runner):148 @click.command()147 @click.option("--arg1", multiple=True, default=('foo', 'bar'),149 @click.option('--arg1', multiple=True, default=('foo', 'bar'),150 show_default=True)149 @click.option("--arg2", multiple=True, default=(1, 2), type=int,151 @click.option('--arg2', multiple=True, default=(1, 2), type=int,152 show_default=True)153 def cmd(arg, arg2):154 pass155156 result = runner.invoke(cmd, ['--help'])157 assert not result.exception156 assert "foo, bar" in result.output157 assert "1, 2" in result.output158 assert 'foo, bar' in result.output159 assert '1, 2' in result.output160161162def test_multiple_default_type(runner):163 @click.command()161 @click.option("--arg1", multiple=True, default=('foo', 'bar'))162 @click.option("--arg2", multiple=True, default=(1, "a"))164 @click.option('--arg1', multiple=True, default=('foo', 'bar'))165 @click.option('--arg2', multiple=True, default=(1, 'a'))166 def cmd(arg1, arg2):164 assert all(isinstance(e[0],str) for e in arg1)165 assert all(isinstance(e[1],str) for e in arg1)167 assert all(isinstance(e[0], text_type) for e in arg1)168 assert all(isinstance(e[1], text_type) for e in arg1)169167 assert all(isinstance(e[0],int) for e in arg2)168 assert all(isinstance(e[1],str) for e in arg2)170 assert all(isinstance(e[0], int) for e in arg2)171 assert all(isinstance(e[1], text_type) for e in arg2)172170 result = runner.invoke(cmd, "--arg1 a b --arg1 test 1 --arg2 2 two --arg2 4 four".split())173 result = runner.invoke(cmd, '--arg1 a b --arg1 test 1 --arg2 2 '174 'two --arg2 4 four'.split())175 assert not result.exception176177178def test_nargs_envvar(runner):179 @click.command()180 @click.option('--arg', nargs=2)181 def cmd(arg):
156 unmodified lines
modified:CHANGES+3 added
17 unmodified lines
18- Added support for short aliases for the false flag on toggles.19- Click will now attempt to aid you with debugging locale errors20 better by listing with the help of the OS what locales are21 available.22- Click used to return byte strings on Python 2 in some unit-testing23 situations. This has been fixed to correctly return unicode strings24 now.2526Version 5.127-----------28
251 unmodified lines