Clean up unicode handling
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 9ab74c3 · Browse files · 2 files·+35 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+35 added−6 removed
- other:+35 added−6 removed
Provenance
- Commit
535a835- Tree
a60f1c7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9ab74c3
2 files changed+35 added−6 removed
modified:click/core.py+6 added-4 removed
2 unmodified lines3 import codecs4 from itertools import chain5 6 from .types import convert_type, BOOL7-from .utils import echo7+from .utils import echo, make_str8 from .exceptions import UsageError, Abort9 from .helpers import prompt, confirm10 11 from . import _optparse251 unmodified lines263 value, args = param.handle_parse_result(ctx, opts, args)264 265 if args and not self.allow_extra_args:266 ctx.fail('Got unexpected extra argument%s (%s)'267- % (len(args) != 1 and 's' or '', ' '.join(args)))267+ % (len(args) != 1 and 's' or '',268+ ' '.join(map(make_str, args))))269 270 ctx.args = args271 272 return ctx43 unmodified lines316 args = sys.argv[1:]317 else:318 args = list(args)319 if prog_name is None:319- prog_name = os.path.basename(sys.argv and sys.argv[0] or __file__)320+ prog_name = make_str(os.path.basename(321+ sys.argv and sys.argv[0] or __file__))322 try:323 try:324 with self.make_context(prog_name, args, **extra) as ctx:325 self.invoke(ctx)76 unmodified lines402 echo(ctx.format_help())403 ctx.exit()404 ctx.fail('Missing command')405 404- cmd_name = ctx.args[0]406+ cmd_name = make_str(ctx.args[0])407 cmd = self.get_command(ctx, cmd_name)408 if cmd is None:409 ctx.fail('No such command "%s"' % cmd_name)410 return self.invoke_subcommand(ctx, cmd, cmd_name, ctx.args[1:])464 unmodified linesmodified:click/utils.py+29 added-2 removed
1 import sys2+import codecs3 4 from ._compat import PY2, text_type5 6 6 unmodified lines13 pass14 return wrapper15 16 17+def is_ascii_encoding(encoding):18+ """Checks if a given encoding is ascii."""19+ try:20+ return codecs.lookup(encoding).name == 'ascii'21+ except LookupError:22+ return False23+24+25+def get_best_encoding(stream):26+ """Returns the best encoding that should be used for a stream."""27+ enc = getattr(stream, 'encoding', None)28+ if enc is None or is_ascii_encoding(enc):29+ return 'utf-8'30+ return enc31+32+33 def echo(message=None, file=None):34 """Prints a message to the given file or stdout."""35 if file is None:36 file = sys.stdout37 if message:38 if PY2 and isinstance(message, text_type):22- message = message.encode(getattr(file, 'encoding', None)23- or 'utf-8', 'replace')39+ encoding = get_best_encoding(file)40+ message = message.encode(encoding, 'replace')41 file.write(message)42 file.write('\n')43 file.flush()44+45+46+def make_str(value):47+ """Converts a value into a valid string."""48+ if isinstance(value, bytes):49+ try:50+ return value.decode(sys.getfilesystemencoding())51+ except UnicodeError:52+ return value.decode('utf-8', 'replace')53+ return text_type(value)