[PATCH stable] pycompat: fix crash when default locale is unknown
Yuya Nishihara
yuya at tcha.org
Wed Jun 24 11:36:46 UTC 2020
On Wed, 24 Jun 2020 04:25:57 +0200, Manuel Jacob wrote:
> # HG changeset patch
> # User Manuel Jacob <me at manueljacob.de>
> # Date 1592965534 -7200
> # Wed Jun 24 04:25:34 2020 +0200
> # Branch stable
> # Node ID 5d2b9295f7607e6f1bc6004af2948f95c83ec4ee
> # Parent 3d41172f2ac9ae7b644f3e5f239c28e895f1c4fa
> # EXP-Topic locale
> pycompat: fix crash when default locale is unknown
>
> Instead, fall back to the filesystem encoding if the default locale is unknown.
>
> diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py
> --- a/mercurial/pycompat.py
> +++ b/mercurial/pycompat.py
> @@ -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]
Seems fine, but it might be more correct to remove getdefaultlocale() at all.
The comment mentions mbstowcs(), which is a locale-dependent function.
If setlocale(LC_CTYPE, '') wouldn't be called yet, I think mbstowcs()
would behave as if the locale were C. So locale.getlocale(LC_CTYPE) should
return the encoding which mbstowcs() would see.
More information about the Mercurial-devel
mailing list