comparison mercurial/util.py @ 45011: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
comparison
equal deleted inserted replaced
45004:2632c1ed8f34 45011:1bab6b61b62b
20 import contextlib 20 import contextlib
21 import errno 21 import errno
22 import gc 22 import gc
23 import hashlib 23 import hashlib
24 import itertools 24 import itertools
25 import locale
25 import mmap 26 import mmap
26 import os 27 import os
27 import platform as pyplatform 28 import platform as pyplatform
28 import re as remod 29 import re as remod
29 import shutil 30 import shutil
3624 byte = ord(readexactly(fh, 1)) 3625 byte = ord(readexactly(fh, 1))
3625 result |= (byte & 0x7F) << shift 3626 result |= (byte & 0x7F) << shift
3626 if not (byte & 0x80): 3627 if not (byte & 0x80):
3627 return result 3628 return result
3628 shift += 7 3629 shift += 7
3630
3631
3632 # Passing the '' locale means that the locale should be set according to the
3633 # user settings (environment variables).
3634 # Python sometimes avoids setting the global locale settings. When interfacing
3635 # with C code (e.g. the curses module or the Subversion bindings), the global
3636 # locale settings must be initialized correctly. Python 2 does not initialize
3637 # the global locale settings on interpreter startup. Python 3 sometimes
3638 # initializes LC_CTYPE, but not consistently at least on Windows. Therefore we
3639 # explicitly initialize it to get consistent behavior if it's not already
3640 # initialized. Since CPython commit 177d921c8c03d30daa32994362023f777624b10d,
3641 # LC_CTYPE is always initialized. If we require Python 3.8+, we should re-check
3642 # if we can remove this code.
3643 @contextlib.contextmanager
3644 def with_lc_ctype():
3645 oldloc = locale.setlocale(locale.LC_CTYPE, None)
3646 if oldloc == 'C':
3647 try:
3648 try:
3649 locale.setlocale(locale.LC_CTYPE, '')
3650 except locale.Error:
3651 # The likely case is that the locale from the environment
3652 # variables is unknown.
3653 pass
3654 yield
3655 finally:
3656 locale.setlocale(locale.LC_CTYPE, oldloc)
3657 else:
3658 yield