Mercurial > public > mercurial-scm > hg
comparison mercurial/encoding.py @ 25660:328739ea70c3
global: mass rewrite to use modern exception syntax
Python 2.6 introduced the "except type as instance" syntax, replacing
the "except type, instance" syntax that came before. Python 3 dropped
support for the latter syntax. Since we no longer support Python 2.4 or
2.5, we have no need to continue supporting the "except type, instance".
This patch mass rewrites the exception syntax to be Python 2.6+ and
Python 3 compatible.
This patch was produced by running `2to3 -f except -w -n .`.
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 23 Jun 2015 22:20:08 -0700 |
parents | 1c533e23ce95 |
children | cf47bdb2183c |
comparison
equal
deleted
inserted
replaced
25659:d60678a567a9 | 25660:328739ea70c3 |
---|---|
136 return r | 136 return r |
137 return localstr(u.encode('UTF-8'), r) | 137 return localstr(u.encode('UTF-8'), r) |
138 except UnicodeDecodeError: | 138 except UnicodeDecodeError: |
139 u = s.decode("utf-8", "replace") # last ditch | 139 u = s.decode("utf-8", "replace") # last ditch |
140 return u.encode(encoding, "replace") # can't round-trip | 140 return u.encode(encoding, "replace") # can't round-trip |
141 except LookupError, k: | 141 except LookupError as k: |
142 raise error.Abort(k, hint="please check your locale settings") | 142 raise error.Abort(k, hint="please check your locale settings") |
143 | 143 |
144 def fromlocal(s): | 144 def fromlocal(s): |
145 """ | 145 """ |
146 Convert a string from the local character encoding to UTF-8 | 146 Convert a string from the local character encoding to UTF-8 |
156 if isinstance(s, localstr): | 156 if isinstance(s, localstr): |
157 return s._utf8 | 157 return s._utf8 |
158 | 158 |
159 try: | 159 try: |
160 return s.decode(encoding, encodingmode).encode("utf-8") | 160 return s.decode(encoding, encodingmode).encode("utf-8") |
161 except UnicodeDecodeError, inst: | 161 except UnicodeDecodeError as inst: |
162 sub = s[max(0, inst.start - 10):inst.start + 10] | 162 sub = s[max(0, inst.start - 10):inst.start + 10] |
163 raise error.Abort("decoding near '%s': %s!" % (sub, inst)) | 163 raise error.Abort("decoding near '%s': %s!" % (sub, inst)) |
164 except LookupError, k: | 164 except LookupError as k: |
165 raise error.Abort(k, hint="please check your locale settings") | 165 raise error.Abort(k, hint="please check your locale settings") |
166 | 166 |
167 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. | 167 # How to treat ambiguous-width characters. Set to 'wide' to treat as wide. |
168 wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" | 168 wide = (os.environ.get("HGENCODINGAMBIGUOUS", "narrow") == "wide" |
169 and "WFA" or "WF") | 169 and "WFA" or "WF") |
328 if u == lu: | 328 if u == lu: |
329 return s # preserve localstring | 329 return s # preserve localstring |
330 return lu.encode(encoding) | 330 return lu.encode(encoding) |
331 except UnicodeError: | 331 except UnicodeError: |
332 return s.lower() # we don't know how to fold this except in ASCII | 332 return s.lower() # we don't know how to fold this except in ASCII |
333 except LookupError, k: | 333 except LookupError as k: |
334 raise error.Abort(k, hint="please check your locale settings") | 334 raise error.Abort(k, hint="please check your locale settings") |
335 | 335 |
336 def upper(s): | 336 def upper(s): |
337 "best-effort encoding-aware case-folding of local string s" | 337 "best-effort encoding-aware case-folding of local string s" |
338 try: | 338 try: |
351 if u == uu: | 351 if u == uu: |
352 return s # preserve localstring | 352 return s # preserve localstring |
353 return uu.encode(encoding) | 353 return uu.encode(encoding) |
354 except UnicodeError: | 354 except UnicodeError: |
355 return s.upper() # we don't know how to fold this except in ASCII | 355 return s.upper() # we don't know how to fold this except in ASCII |
356 except LookupError, k: | 356 except LookupError as k: |
357 raise error.Abort(k, hint="please check your locale settings") | 357 raise error.Abort(k, hint="please check your locale settings") |
358 | 358 |
359 class normcasespecs(object): | 359 class normcasespecs(object): |
360 '''what a platform's normcase does to ASCII strings | 360 '''what a platform's normcase does to ASCII strings |
361 | 361 |