changeset 44977:afcad425a0b6 stable

pycompat: fix crash when default locale is unknown Instead, fall back to the filesystem encoding if the default locale is unknown.
author Manuel Jacob <me@manueljacob.de>
date Wed, 24 Jun 2020 04:25:34 +0200
parents 3d41172f2ac9
children 95c672c07116 93aa152d4295
files mercurial/pycompat.py tests/test-locale.t
diffstat 2 files changed, 10 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/pycompat.py	Mon Jun 22 22:15:57 2020 -0700
+++ b/mercurial/pycompat.py	Wed Jun 24 04:25:34 2020 +0200
@@ -178,9 +178,16 @@
         if os.name == r'nt':
             sysargv = [a.encode("mbcs", "ignore") for a in sys.argv]
         else:
+
+            def getdefaultlocale_if_known():
+                try:
+                    return locale.getdefaultlocale()
+                except ValueError:
+                    return None, None
+
             encoding = (
                 locale.getlocale()[1]
-                or locale.getdefaultlocale()[1]
+                or getdefaultlocale_if_known()[1]
                 or sys.getfilesystemencoding()
             )
             sysargv = [a.encode(encoding, "surrogateescape") for a in sys.argv]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-locale.t	Wed Jun 24 04:25:34 2020 +0200
@@ -0,0 +1,2 @@
+  $ LANG=nonexistent LC_ALL=nonexistent LANGUAGE=nonexistent hg version -q
+  Mercurial Distributed SCM (version *) (glob)