Warn about the use of unicode_literals
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 36f83d9 · Browse files · 6 files·+67 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+67 added−2 removed
- other:+67 added−2 removed
Provenance
- Commit
5f33770- Tree
a0d1a6a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
36f83d9
6 files changed+67 added−2 removed
modified:click/__init__.py+5 added
86 unmodified lines87 'OptionParser',88 ]89 90 91+# Controls if click should emit the warning about the use of unicode92+# literals.93+disable_unicode_literals_warning = False94+95+96 __version__ = '5.0-dev'modified:click/_compat.py+35 added
3 unmodified lines4 import sys5 import codecs6 from weakref import WeakKeyDictionary7 8+import click9 10+11 PY2 = sys.version_info[0] == 212 WIN = sys.platform.startswith('win')13 DEFAULT_COLUMNS = 8014 15 16 _ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')17 18 19+def _find_unicode_literals_frame():20+ import __future__21+ frm = sys._getframe(1)22+ idx = 123+ while frm is not None:24+ if frm.f_globals.get('__name__').startswith('click.'):25+ frm = frm.f_back26+ idx += 127+ elif frm.f_code.co_flags & __future__.unicode_literals.compiler_flag:28+ return idx29+ else:30+ break31+ return 032+33+34+def _check_for_unicode_literals():35+ if not __debug__:36+ return37+ if not PY2 or click.disable_unicode_literals_warning:38+ return39+ bad_frame = _find_unicode_literals_frame()40+ if bad_frame <= 0:41+ return42+ from warnings import warn43+ warn(Warning('Click detected the use of the unicode_literals '44+ '__future__ import. This is heavily discouraged '45+ 'because it can introduce subtle bugs in your '46+ 'code. You should instead use explicit u"" literals '47+ 'for your unicode strings. For more information see '48+ 'http://click.pocoo.org/python3/'),49+ stacklevel=bad_frame)50+51+52 def get_filesystem_encoding():53 return sys.getfilesystemencoding() or sys.getdefaultencoding()54 55 569 unmodified linesmodified:click/core.py+3 added-1 removed
12 unmodified lines13 from .formatting import HelpFormatter, join_options14 from .parser import OptionParser, split_opt15 from .globals import push_context, pop_context16 17-from ._compat import PY2, isidentifier, iteritems17+from ._compat import PY2, isidentifier, iteritems, _check_for_unicode_literals18 19 20 _missing = object()21 635 unmodified lines657 'ASCII as encoding for the environment. '658 'Either switch to Python 2 or consult '659 'http://click.pocoo.org/python3/ '660 'for mitigation steps.')661+ else:662+ _check_for_unicode_literals()663 664 if args is None:665 args = sys.argv[1:]666 else:1030 unmodified linesmodified:click/decorators.py+2 added-1 removed
1 unmodified line2 import inspect3 4 from functools import update_wrapper5 6-from ._compat import iteritems6+from ._compat import iteritems, _check_for_unicode_literals7 from .utils import echo8 from .globals import get_current_context9 10 72 unmodified lines83 help = help.decode('utf-8')84 else:85 help = inspect.cleandoc(help)86 attrs['help'] = help87+ _check_for_unicode_literals()88 return cls(name=name or f.__name__.lower(),89 callback=f, params=params, **attrs)90 91 211 unmodified linesmodified:docs/python3.rst+21 added
150 unmodified lines151 <http://bugs.python.org/issue19846>`_152 * `LC_CTYPE=C: pydoc leaves terminal in an unusable state153 <http://bugs.python.org/issue21398>`_ (this is relevant to Click154 because the pager support is provided by the stdlib pydoc module)155+156+Unicode Literals157+----------------158+159+Starting with Click 5.0 there will be a warning for the use of the160+``unicode_literals`` future import in Python 2. This has been done due to161+the negative consequences of this import with regards to unintentionally162+causing bugs due to introducing Unicode data to APIs that are incapable of163+handling them.164+165+If you use ``unicode_literals`` in any file that defines a Click command166+or that invokes a click command you will be given a warning. You are167+strongly encouraged to not use ``unicode_literals`` and instead use168+explicit ``u`` prefixes for your Unicode strings.169+170+If you do want to ignore the warning and continue using171+``unicode_literals`` on your own peril, you can disable the warning as172+follows::173+174+ import click175+ click.disable_unicode_literals_warning = Truemodified:CHANGES+1 added
19 unmodified lines20 - introduced `Context.scope`.21 - The `echo` function is now threadsafe: It calls the `write` method of the22 underlying object only once.23 - `prompt(hide_input=True)` now prints a newline on `^C`.24+- Click will now warn if users are using ``unicode_literals``.25 26 Version 4.127 -----------28 221 unmodified lines