Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 44826:1bab6b61b62b stable
curses: do not initialize LC_ALL to user settings (issue6358)
701341f57ceb moved the setlocale() call to right before curses was used. This
didn?t fully solve the problem it was supposed to solve (locale-dependent
functions, like date formatting/parsing and str methods on Python 2), but only
postponed it.
Initializing LC_CTYPE seems to be sufficient for curses to work correctly.
Therefore LC_CTYPE is set while curses is used and reset afterwards. Some
locale-dependent str methods might behave differently on Python 2 while curses
is used, but that shouldn?d be a problem.
author | Manuel Jacob <me@manueljacob.de> |
---|---|
date | Sun, 28 Jun 2020 18:02:45 +0200 |
parents | d37975386798 |
children | 4a503c1b664a |
line wrap: on
line diff
--- a/mercurial/util.py Thu Jun 25 03:46:07 2020 +0200 +++ b/mercurial/util.py Sun Jun 28 18:02:45 2020 +0200 @@ -22,6 +22,7 @@ import gc import hashlib import itertools +import locale import mmap import os import platform as pyplatform @@ -3626,3 +3627,32 @@ if not (byte & 0x80): return result shift += 7 + + +# Passing the '' locale means that the locale should be set according to the +# user settings (environment variables). +# Python sometimes avoids setting the global locale settings. When interfacing +# with C code (e.g. the curses module or the Subversion bindings), the global +# locale settings must be initialized correctly. Python 2 does not initialize +# the global locale settings on interpreter startup. Python 3 sometimes +# initializes LC_CTYPE, but not consistently at least on Windows. Therefore we +# explicitly initialize it to get consistent behavior if it's not already +# initialized. Since CPython commit 177d921c8c03d30daa32994362023f777624b10d, +# LC_CTYPE is always initialized. If we require Python 3.8+, we should re-check +# if we can remove this code. +@contextlib.contextmanager +def with_lc_ctype(): + oldloc = locale.setlocale(locale.LC_CTYPE, None) + if oldloc == 'C': + try: + try: + locale.setlocale(locale.LC_CTYPE, '') + except locale.Error: + # The likely case is that the locale from the environment + # variables is unknown. + pass + yield + finally: + locale.setlocale(locale.LC_CTYPE, oldloc) + else: + yield