comparison env/lib/python3.9/site-packages/click/_unicodefun.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 import codecs
2 import os
3 import sys
4
5 from ._compat import PY2
6
7
8 def _find_unicode_literals_frame():
9 import __future__
10
11 if not hasattr(sys, "_getframe"): # not all Python implementations have it
12 return 0
13 frm = sys._getframe(1)
14 idx = 1
15 while frm is not None:
16 if frm.f_globals.get("__name__", "").startswith("click."):
17 frm = frm.f_back
18 idx += 1
19 elif frm.f_code.co_flags & __future__.unicode_literals.compiler_flag:
20 return idx
21 else:
22 break
23 return 0
24
25
26 def _check_for_unicode_literals():
27 if not __debug__:
28 return
29
30 from . import disable_unicode_literals_warning
31
32 if not PY2 or disable_unicode_literals_warning:
33 return
34 bad_frame = _find_unicode_literals_frame()
35 if bad_frame <= 0:
36 return
37 from warnings import warn
38
39 warn(
40 Warning(
41 "Click detected the use of the unicode_literals __future__"
42 " import. This is heavily discouraged because it can"
43 " introduce subtle bugs in your code. You should instead"
44 ' use explicit u"" literals for your unicode strings. For'
45 " more information see"
46 " https://click.palletsprojects.com/python3/"
47 ),
48 stacklevel=bad_frame,
49 )
50
51
52 def _verify_python3_env():
53 """Ensures that the environment is good for unicode on Python 3."""
54 if PY2:
55 return
56 try:
57 import locale
58
59 fs_enc = codecs.lookup(locale.getpreferredencoding()).name
60 except Exception:
61 fs_enc = "ascii"
62 if fs_enc != "ascii":
63 return
64
65 extra = ""
66 if os.name == "posix":
67 import subprocess
68
69 try:
70 rv = subprocess.Popen(
71 ["locale", "-a"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
72 ).communicate()[0]
73 except OSError:
74 rv = b""
75 good_locales = set()
76 has_c_utf8 = False
77
78 # Make sure we're operating on text here.
79 if isinstance(rv, bytes):
80 rv = rv.decode("ascii", "replace")
81
82 for line in rv.splitlines():
83 locale = line.strip()
84 if locale.lower().endswith((".utf-8", ".utf8")):
85 good_locales.add(locale)
86 if locale.lower() in ("c.utf8", "c.utf-8"):
87 has_c_utf8 = True
88
89 extra += "\n\n"
90 if not good_locales:
91 extra += (
92 "Additional information: on this system no suitable"
93 " UTF-8 locales were discovered. This most likely"
94 " requires resolving by reconfiguring the locale"
95 " system."
96 )
97 elif has_c_utf8:
98 extra += (
99 "This system supports the C.UTF-8 locale which is"
100 " recommended. You might be able to resolve your issue"
101 " by exporting the following environment variables:\n\n"
102 " export LC_ALL=C.UTF-8\n"
103 " export LANG=C.UTF-8"
104 )
105 else:
106 extra += (
107 "This system lists a couple of UTF-8 supporting locales"
108 " that you can pick from. The following suitable"
109 " locales were discovered: {}".format(", ".join(sorted(good_locales)))
110 )
111
112 bad_locale = None
113 for locale in os.environ.get("LC_ALL"), os.environ.get("LANG"):
114 if locale and locale.lower().endswith((".utf-8", ".utf8")):
115 bad_locale = locale
116 if locale is not None:
117 break
118 if bad_locale is not None:
119 extra += (
120 "\n\nClick discovered that you exported a UTF-8 locale"
121 " but the locale system could not pick up from it"
122 " because it does not exist. The exported locale is"
123 " '{}' but it is not supported".format(bad_locale)
124 )
125
126 raise RuntimeError(
127 "Click will abort further execution because Python 3 was"
128 " configured to use ASCII as encoding for the environment."
129 " Consult https://click.palletsprojects.com/python3/ for"
130 " mitigation steps.{}".format(extra)
131 )