# HG changeset patch # User Matt Mackall # Date 1165689961 21600 # Node ID abaa2cd00d2b7dcaffb4c36b8dd305f510130d28 # Parent 47c634bf1e924733a8e4da433dc5e33b499bcc14 make transcoding more robust default to ASCII abort if unknown encoding wrap abort strings with _ add test diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -17,7 +17,8 @@ from demandload import * demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile") demandload(globals(), "os threading time calendar ConfigParser locale") -_encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() +_encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \ + or "ascii" _encodingmode = os.environ.get("HGENCODINGMODE", "strict") _fallbackencoding = 'ISO-8859-1' @@ -35,6 +36,8 @@ def tolocal(s): try: u = s.decode(e) # attempt strict decoding return u.encode(_encoding, "replace") + except LookupError, k: + raise Abort(_("%s, please check your locale settings") % k) except UnicodeDecodeError: pass u = s.decode("utf-8", "replace") # last ditch @@ -54,7 +57,9 @@ def fromlocal(s): return s.decode(_encoding, _encodingmode).encode("utf-8") except UnicodeDecodeError, inst: sub = s[max(0, inst.start-10):inst.start+10] - raise Abort("decoding near '%s': %s!\n" % (sub, inst)) + raise Abort("decoding near '%s': %s!" % (sub, inst)) + except LookupError, k: + raise Abort(_("%s, please check your locale settings") % k) def locallen(s): """Find the length in characters of a local string""" @@ -70,7 +75,7 @@ def localsub(s, a, b=None): return u.encode(_encoding, _encodingmode) except UnicodeDecodeError, inst: sub = s[max(0, inst.start-10), inst.start+10] - raise Abort("decoding near '%s': %s!\n" % (sub, inst)) + raise Abort(_("decoding near '%s': %s!\n") % (sub, inst)) # used by parsedate defaultdateformats = ( diff --git a/tests/test-encoding b/tests/test-encoding --- a/tests/test-encoding +++ b/tests/test-encoding @@ -52,3 +52,5 @@ echo '[ui]' >> .hg/hgrc echo 'fallbackencoding = koi8-r' >> .hg/hgrc echo % utf-8 HGENCODING=utf-8 hg log + +HGENCODING=dolphin hg log \ No newline at end of file diff --git a/tests/test-encoding.out b/tests/test-encoding.out --- a/tests/test-encoding.out +++ b/tests/test-encoding.out @@ -10,7 +10,6 @@ M a ? latin-1-tag ? utf-8 abort: decoding near ' encoded: é': 'ascii' codec can't decode byte 0xe9 in position 20: ordinal not in range(128)! - transaction abort! rollback completed % these should work @@ -165,3 +164,4 @@ user: test date: Mon Jan 12 13:46:40 1970 +0000 summary: latin-1 e': И = u'\xe9' +abort: unknown encoding: dolphin, please check your locale settings