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

Martin Geisler mg at lazybytes.net
Mon Jul 23 15:59:52 CDT 2012


# 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
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