Mercurial > kallithea
changeset 8712:1f8eaa4c1dff
lib: move locale.py to locales.py to avoid shadowing of standard module
"Fix" spurious problem, seen for example as:
$ python kallithea/lib/annotate.py
Traceback (most recent call last):
File ".../lib64/python3.8/site-packages/mercurial/encoding.py", line 107, in <module>
encoding = locale.getpreferredencoding().encode('ascii') or b'ascii'
AttributeError: module 'locale' has no attribute 'getpreferredencoding'
That happened when something in some other module tried to import stdlib locale
... but somehow would pick up the kallithea locale module and things would
fail.
Stay out of that kind of trouble by using a name that doesn't collide.
author | Mads Kiilerich <mads@kiilerich.com> |
---|---|
date | Wed, 28 Oct 2020 21:24:13 +0100 |
parents | 410934dd09f4 |
children | 180effeba219 |
files | kallithea/bin/kallithea_cli_config.py kallithea/config/app_cfg.py kallithea/lib/locale.py kallithea/lib/locales.py |
diffstat | 4 files changed, 53 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/bin/kallithea_cli_config.py Fri Oct 30 22:38:39 2020 +0100 +++ b/kallithea/bin/kallithea_cli_config.py Wed Oct 28 21:24:13 2020 +0100 @@ -21,7 +21,7 @@ import mako.exceptions import kallithea.bin.kallithea_cli_base as cli_base -import kallithea.lib.locale +import kallithea.lib.locales from kallithea.lib import inifile @@ -66,7 +66,7 @@ 'git_hook_interpreter': sys.executable, 'user_home_path': os.path.expanduser('~'), 'kallithea_cli_path': cli_base.kallithea_cli_path, - 'ssh_locale': kallithea.lib.locale.get_current_locale(), + 'ssh_locale': kallithea.lib.locales.get_current_locale(), } ini_settings = defaultdict(dict)
--- a/kallithea/config/app_cfg.py Fri Oct 30 22:38:39 2020 +0100 +++ b/kallithea/config/app_cfg.py Wed Oct 28 21:24:13 2020 +0100 @@ -30,7 +30,7 @@ from sqlalchemy import create_engine from tg import FullStackApplicationConfigurator -import kallithea.lib.locale +import kallithea.lib.locales import kallithea.model.base import kallithea.model.meta from kallithea.lib import celerypylons @@ -99,7 +99,7 @@ def setup_configuration(app): config = app.config - if not kallithea.lib.locale.current_locale_is_valid(): + if not kallithea.lib.locales.current_locale_is_valid(): log.error("Terminating ...") sys.exit(1)
--- a/kallithea/lib/locale.py Fri Oct 30 22:38:39 2020 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. -import logging -import os -import sys - - -log = logging.getLogger(__name__) - -def current_locale_is_valid(): - """Verify that things work when Dulwich passes unicode paths to the file system layer. - - Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also - work under the right circumstances.""" - try: - '\xe9'.encode(sys.getfilesystemencoding()) # Test using é (é) - except UnicodeEncodeError: - log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding()) - for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: - if var in os.environ: - val = os.environ[var] - log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val) - break - else: - log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'") - return False - return True - -def get_current_locale(): - """Return the current locale based on environment variables. - There does not seem to be a good (and functional) way to get it via Python. - """ - for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: - val = os.environ.get(var) - if val: - log.debug('Determined current locale via environment variable %s (%s)', var, val) - return val - return None
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kallithea/lib/locales.py Wed Oct 28 21:24:13 2020 +0100 @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +import logging +import os +import sys + + +log = logging.getLogger(__name__) + +def current_locale_is_valid(): + """Verify that things work when Dulwich passes unicode paths to the file system layer. + + Note: UTF-8 is preferred, but for example ISO-8859-1 or mbcs should also + work under the right circumstances.""" + try: + '\xe9'.encode(sys.getfilesystemencoding()) # Test using é (é) + except UnicodeEncodeError: + log.error("Cannot encode Unicode paths to file system encoding %r", sys.getfilesystemencoding()) + for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: + if var in os.environ: + val = os.environ[var] + log.error("Note: Environment variable %s is %r - perhaps change it to some other value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'", var, val) + break + else: + log.error("Note: No locale setting found in environment variables - perhaps set LC_CTYPE to some value from 'locale -a', like 'C.UTF-8' or 'en_US.UTF-8'") + return False + return True + +def get_current_locale(): + """Return the current locale based on environment variables. + There does not seem to be a good (and functional) way to get it via Python. + """ + for var in ['LC_ALL', 'LC_CTYPE', 'LANG']: + val = os.environ.get(var) + if val: + log.debug('Determined current locale via environment variable %s (%s)', var, val) + return val + return None