Skip to content
Myr

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

2 files+35 added−6 removed

modified:click/core.py+6 added-4 removed
2 unmodified lines
3import codecs4from itertools import chain56from .types import convert_type, BOOL7from .utils import echo7from .utils import echo, make_str8from .exceptions import UsageError, Abort9from .helpers import prompt, confirm1011from . import _optparse
251 unmodified lines
263 value, args = param.handle_parse_result(ctx, opts, args)264265 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))))269270 ctx.args = args271272 return ctx
43 unmodified lines
316 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 lines
402 echo(ctx.format_help())403 ctx.exit()404 ctx.fail('Missing command')405404 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 lines
modified:click/utils.py+29 added-2 removed
1import sys2import codecs34from ._compat import PY2, text_type56
6 unmodified lines
13 pass14 return wrapper151617def 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 False232425def 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 enc313233def 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()444546def 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)