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
Provenance
- Commit
7951a73- Tree
65812d4- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
40705b9
5 files changed+38 added−23 removed
modified:click/_compat.py+7 added
549 unmodified lines550 except Exception:551 return False552 553 554+def _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()559+560+561 def _make_cached_stream_func(src_func, wrapper_func):562 cache = WeakKeyDictionary()563 def func():564 stream = src_func()32 unmodified linesmodified:click/types.py+9 added-9 removed
1 import os2-import sys2 import stat3 4 from ._compat import open_stream, text_type, filename_to_ui, \6- get_filesystem_encoding, get_streerror5+ get_filesystem_encoding, get_streerror, _get_argv_encoding6 from .exceptions import BadParameter7 from .utils import safecall, LazyFile8 9 97 unmodified lines107 name = 'text'108 109 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 value123 124 def __repr__(self):399 unmodified linesmodified:tests/test_arguments.py+3 added-3 removed
233 unmodified lines234 click.echo(arg)235 236 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 lines251 click.echo(arg)252 253 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 lines3 import os4 import click5 import pytest6 7+from click._compat import text_type8 9+10 def 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 lines145 146 147 def 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 pass155 156 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.output160 161+162 def 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)169 167- 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)172 170- 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.exception176 177+178 def test_nargs_envvar(runner):179 @click.command()180 @click.option('--arg', nargs=2)181 def cmd(arg):156 unmodified linesmodified:CHANGES+3 added
17 unmodified lines18 - 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.25 26 Version 5.127 -----------28 251 unmodified lines