diff --git a/hgext/histedit.py b/hgext/histedit.py --- a/hgext/histedit.py +++ b/hgext/histedit.py @@ -201,7 +201,6 @@ except ImportError: termios = None import functools -import locale import os import struct @@ -1710,11 +1709,8 @@ def _chistedit(ui, repo, freeargs, opts) ctxs = [] for i, r in enumerate(revs): ctxs.append(histeditrule(ui, repo[r], i)) - # Curses requires setting the locale or it will default to the C - # locale. This sets the locale to the user's default system - # locale. - locale.setlocale(locale.LC_ALL, '') - rc = curses.wrapper(functools.partial(_chisteditmain, repo, ctxs)) + with util.with_lc_ctype(): + rc = curses.wrapper(functools.partial(_chisteditmain, repo, ctxs)) curses.echo() curses.endwin() if rc is False: diff --git a/mercurial/crecord.py b/mercurial/crecord.py --- a/mercurial/crecord.py +++ b/mercurial/crecord.py @@ -10,7 +10,6 @@ from __future__ import absolute_import -import locale import os import re import signal @@ -574,14 +573,12 @@ def chunkselector(ui, headerlist, operat """ ui.write(_(b'starting interactive selection\n')) chunkselector = curseschunkselector(headerlist, ui, operation) - # This is required for ncurses to display non-ASCII characters in - # default user locale encoding correctly. --immerrr - locale.setlocale(locale.LC_ALL, '') origsigtstp = sentinel = object() if util.safehasattr(signal, b'SIGTSTP'): origsigtstp = signal.getsignal(signal.SIGTSTP) try: - curses.wrapper(chunkselector.main) + with util.with_lc_ctype(): + curses.wrapper(chunkselector.main) if chunkselector.initexc is not None: raise chunkselector.initexc # ncurses does not restore signal handler for SIGTSTP diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -22,6 +22,7 @@ import errno import gc import hashlib import itertools +import locale import mmap import os import platform as pyplatform @@ -3626,3 +3627,32 @@ def uvarintdecodestream(fh): 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