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.
--- 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):