encoding: use s.decode to trigger UnicodeDecodeError stable
authorMartin Geisler <mg@aragost.com>
Mon, 23 Jul 2012 15:55:22 -0600
branchstable
changeset 17235 3745ae495ce5
parent 17234 0cfece81e051
child 17236 9fb8312dbdbd
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 UnicodeDecodeError raised by decode if it finds a non-ASCII character. This way is also marginally faster since it saves the construction of the extra str object.
mercurial/encoding.py
--- a/mercurial/encoding.py	Sun Jul 22 13:16:45 2012 +0200
+++ b/mercurial/encoding.py	Mon Jul 23 15:55:22 2012 -0600
@@ -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):