[PATCH 1 of 2] encoding: use s.decode to trigger UnicodeDecodeError

Adrian Buehlmann adrian at cadifra.com
Mon Jul 23 16:13:23 CDT 2012


On 2012-07-23 22:59, Martin Geisler wrote:
> # HG changeset patch
> # User Martin Geisler <mg at aragost.com>
> # Date 1343076802 21600
> # Branch stable
> # Node ID 483526b958bcbc483f00f559db0de7221067dc12
> # Parent  d1b49b02bc161faed1f397cb8fe13a38299bbc01
> encoding: use s.decode to trigger UnicodeDecodeError
> 
> When calling encode on a str, the string is first decoded using the
> default encoding and then encoded. So
> 
>   s.encode('ascii') == s.decode().encode('ascii')
> 
> We don't care about the encode step here -- we're just after the
> UnicodeDecodeException raised by decode if it finds a non-ASCII
  UnicodeDecodeError exception raised by ...

> character.
> 
> diff --git a/mercurial/encoding.py b/mercurial/encoding.py
> --- a/mercurial/encoding.py
> +++ b/mercurial/encoding.py
> @@ -168,8 +168,9 @@
>  def lower(s):
>      "best-effort encoding-aware case-folding of local string s"
>      try:
> -        return s.encode('ascii').lower()
> -    except UnicodeError:
> +        s.decode('ascii') # throw exception for non-ASCII character
> +        return s.lower()
> +    except UnicodeDecodeError:
>          pass
>      try:
>          if isinstance(s, localstr):


More information about the Mercurial-devel mailing list