pycompat: fix crash when default locale is unknown stable
authorManuel Jacob <me@manueljacob.de>
Wed, 24 Jun 2020 04:25:34 +0200
branchstable
changeset 44821 afcad425a0b6
parent 44820 3d41172f2ac9
child 44822 95c672c07116
child 45017 93aa152d4295
pycompat: fix crash when default locale is unknown Instead, fall back to the filesystem encoding if the default locale is unknown.
mercurial/pycompat.py
tests/test-locale.t
--- 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)