[PATCH 2 of 2 V3] encoding: use unicode literals to appease Python 3

Yuya Nishihara yuya at tcha.org
Wed Jul 13 08:33:45 EDT 2016


On Mon, 04 Jul 2016 11:44:10 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc at gmail.com>
> # Date 1467657692 25200
> #      Mon Jul 04 11:41:32 2016 -0700
> # Node ID d0a5fd1255a1c7d9f09bcdc3d054e095a2e9bba9
> # Parent  99a1ac83fe44142a9793a93e55b41aa052954842
> encoding: use unicode literals to appease Python 3

>  try:
> -    encoding = os.environ.get("HGENCODING")
> +    encoding = os.environ.get(u'HGENCODING')
>      if not encoding:
> -        encoding = locale.getpreferredencoding() or 'ascii'
> +        encoding = locale.getpreferredencoding() or u'ascii'
>          encoding = _encodingfixers.get(encoding, lambda: encoding)()
>  except locale.Error:
> -    encoding = 'ascii'
> -encodingmode = os.environ.get("HGENCODINGMODE", "strict")
> -fallbackencoding = 'ISO-8859-1'
> +    encoding = u'ascii'
> +encodingmode = os.environ.get(u'HGENCODINGMODE', u'strict')
> +fallbackencoding = u'ISO-8859-1'

encoding.encoding should be str. Otherwise "hg debuginstall" would concatenate
unicode and str.

Mixing unicode and str in public API would be a source of unicode bugs
no developer would notice.

> -wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide"
> -        and "WFA" or "WF")
> +wide = (os.environ.get(u'HGENCODINGAMBIGUOUS', u'narrow') == u'wide'
> +        and u'WFA' or u'WF')

Comparison between str and unicode can emit UnicodeWarning: Unicode equal
comparison failed to convert both arguments to Unicode - interpreting them
as being unequal


More information about the Mercurial-devel mailing list