python-click/src/click/_unicodefun.py

101 lines
3.1 KiB
Python
Raw Normal View History

2020-07-21 08:23:42 +02:00
import codecs
import os
2021-10-10 03:31:57 +02:00
from gettext import gettext as _
2020-07-21 08:23:42 +02:00
2021-10-10 03:31:57 +02:00
def _verify_python_env() -> None:
"""Ensures that the environment is good for Unicode."""
2020-07-21 08:23:42 +02:00
try:
2021-10-10 03:31:57 +02:00
from locale import getpreferredencoding
2020-07-21 08:23:42 +02:00
2021-10-10 03:31:57 +02:00
fs_enc = codecs.lookup(getpreferredencoding()).name
2020-07-21 08:23:42 +02:00
except Exception:
fs_enc = "ascii"
2021-10-10 03:31:57 +02:00
2020-07-21 08:23:42 +02:00
if fs_enc != "ascii":
return
2021-10-10 03:31:57 +02:00
extra = [
_(
"Click will abort further execution because Python was"
" configured to use ASCII as encoding for the environment."
" Consult https://click.palletsprojects.com/unicode-support/"
" for mitigation steps."
)
]
2020-07-21 08:23:42 +02:00
if os.name == "posix":
import subprocess
try:
rv = subprocess.Popen(
2021-10-10 03:31:57 +02:00
["locale", "-a"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="ascii",
errors="replace",
2020-07-21 08:23:42 +02:00
).communicate()[0]
except OSError:
2021-10-10 03:31:57 +02:00
rv = ""
2020-07-21 08:23:42 +02:00
good_locales = set()
has_c_utf8 = False
for line in rv.splitlines():
locale = line.strip()
2021-10-10 03:31:57 +02:00
2020-07-21 08:23:42 +02:00
if locale.lower().endswith((".utf-8", ".utf8")):
good_locales.add(locale)
2021-10-10 03:31:57 +02:00
2020-07-21 08:23:42 +02:00
if locale.lower() in ("c.utf8", "c.utf-8"):
has_c_utf8 = True
if not good_locales:
2021-10-10 03:31:57 +02:00
extra.append(
_(
"Additional information: on this system no suitable"
" UTF-8 locales were discovered. This most likely"
" requires resolving by reconfiguring the locale"
" system."
)
2020-07-21 08:23:42 +02:00
)
elif has_c_utf8:
2021-10-10 03:31:57 +02:00
extra.append(
_(
"This system supports the C.UTF-8 locale which is"
" recommended. You might be able to resolve your"
" issue by exporting the following environment"
" variables:"
)
2020-07-21 08:23:42 +02:00
)
2021-10-10 03:31:57 +02:00
extra.append(" export LC_ALL=C.UTF-8\n export LANG=C.UTF-8")
2020-07-21 08:23:42 +02:00
else:
2021-10-10 03:31:57 +02:00
extra.append(
_(
"This system lists some UTF-8 supporting locales"
" that you can pick from. The following suitable"
" locales were discovered: {locales}"
).format(locales=", ".join(sorted(good_locales)))
2020-07-21 08:23:42 +02:00
)
bad_locale = None
2021-10-10 03:31:57 +02:00
for env_locale in os.environ.get("LC_ALL"), os.environ.get("LANG"):
if env_locale and env_locale.lower().endswith((".utf-8", ".utf8")):
bad_locale = env_locale
if env_locale is not None:
2020-07-21 08:23:42 +02:00
break
2021-10-10 03:31:57 +02:00
2020-07-21 08:23:42 +02:00
if bad_locale is not None:
2021-10-10 03:31:57 +02:00
extra.append(
_(
"Click discovered that you exported a UTF-8 locale"
" but the locale system could not pick up from it"
" because it does not exist. The exported locale is"
" {locale!r} but it is not supported."
).format(locale=bad_locale)
2020-07-21 08:23:42 +02:00
)
2021-10-10 03:31:57 +02:00
raise RuntimeError("\n\n".join(extra))