Mercurial > public > mercurial-scm > hg-stable
diff mercurial/encoding.py @ 30034:e4a6b439acc5
py3: provide encoding.environ which is a dict of bytes
This can't be moved to pycompat.py since we need encoding.tolocal() to
build bytes dict from unicode os.environ.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Wed, 28 Sep 2016 20:05:34 +0900 |
parents | 02dbfaa6df0b |
children | ce36fa9b140c |
line wrap: on
line diff
--- a/mercurial/encoding.py Wed Sep 28 20:39:06 2016 +0900 +++ b/mercurial/encoding.py Wed Sep 28 20:05:34 2016 +0900 @@ -47,6 +47,19 @@ s = s.replace(c, '') return s +# encoding.environ is provided read-only, which may not be used to modify +# the process environment +_nativeenviron = (not pycompat.ispy3 or os.supports_bytes_environ) +if not pycompat.ispy3: + environ = os.environ +elif _nativeenviron: + environ = os.environb +else: + # preferred encoding isn't known yet; use utf-8 to avoid unicode error + # and recreate it once encoding is settled + environ = dict((k.encode(u'utf-8'), v.encode(u'utf-8')) + for k, v in os.environ.items()) + def _getpreferredencoding(): ''' On darwin, getpreferredencoding ignores the locale environment and @@ -78,13 +91,13 @@ } try: - encoding = os.environ.get("HGENCODING") + encoding = environ.get("HGENCODING") if not encoding: encoding = locale.getpreferredencoding() or 'ascii' encoding = _encodingfixers.get(encoding, lambda: encoding)() except locale.Error: encoding = 'ascii' -encodingmode = os.environ.get("HGENCODINGMODE", "strict") +encodingmode = environ.get("HGENCODINGMODE", "strict") fallbackencoding = 'ISO-8859-1' class localstr(str): @@ -183,8 +196,14 @@ except LookupError as k: raise error.Abort(k, hint="please check your locale settings") +if not _nativeenviron: + # now encoding and helper functions are available, recreate the environ + # dict to be exported to other modules + environ = dict((tolocal(k.encode(u'utf-8')), tolocal(v.encode(u'utf-8'))) + for k, v in os.environ.items()) + # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. -wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" +wide = (environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" and "WFA" or "WF") def colwidth(s):