Mercurial > hg-stable
changeset 17235:3745ae495ce5 stable
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.
author | Martin Geisler <mg@aragost.com> |
---|---|
date | Mon, 23 Jul 2012 15:55:22 -0600 |
parents | 0cfece81e051 |
children | 9fb8312dbdbd |
files | mercurial/encoding.py |
diffstat | 1 files changed, 3 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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):