mercurial/util.py
changeset 3843 abaa2cd00d2b
parent 3835 d1ce5461beed
child 3854 4f6db0233606
equal deleted inserted replaced
3842:47c634bf1e92 3843:abaa2cd00d2b
    15 from i18n import gettext as _
    15 from i18n import gettext as _
    16 from demandload import *
    16 from demandload import *
    17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
    17 demandload(globals(), "cStringIO errno getpass popen2 re shutil sys tempfile")
    18 demandload(globals(), "os threading time calendar ConfigParser locale")
    18 demandload(globals(), "os threading time calendar ConfigParser locale")
    19 
    19 
    20 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding()
    20 _encoding = os.environ.get("HGENCODING") or locale.getpreferredencoding() \
       
    21             or "ascii"
    21 _encodingmode = os.environ.get("HGENCODINGMODE", "strict")
    22 _encodingmode = os.environ.get("HGENCODINGMODE", "strict")
    22 _fallbackencoding = 'ISO-8859-1'
    23 _fallbackencoding = 'ISO-8859-1'
    23 
    24 
    24 def tolocal(s):
    25 def tolocal(s):
    25     """
    26     """
    33     """
    34     """
    34     for e in ('UTF-8', _fallbackencoding):
    35     for e in ('UTF-8', _fallbackencoding):
    35         try:
    36         try:
    36             u = s.decode(e) # attempt strict decoding
    37             u = s.decode(e) # attempt strict decoding
    37             return u.encode(_encoding, "replace")
    38             return u.encode(_encoding, "replace")
       
    39         except LookupError, k:
       
    40             raise Abort(_("%s, please check your locale settings") % k)
    38         except UnicodeDecodeError:
    41         except UnicodeDecodeError:
    39             pass
    42             pass
    40     u = s.decode("utf-8", "replace") # last ditch
    43     u = s.decode("utf-8", "replace") # last ditch
    41     return u.encode(_encoding, "replace")
    44     return u.encode(_encoding, "replace")
    42 
    45 
    52     """
    55     """
    53     try:
    56     try:
    54         return s.decode(_encoding, _encodingmode).encode("utf-8")
    57         return s.decode(_encoding, _encodingmode).encode("utf-8")
    55     except UnicodeDecodeError, inst:
    58     except UnicodeDecodeError, inst:
    56         sub = s[max(0, inst.start-10):inst.start+10]
    59         sub = s[max(0, inst.start-10):inst.start+10]
    57         raise Abort("decoding near '%s': %s!\n" % (sub, inst))
    60         raise Abort("decoding near '%s': %s!" % (sub, inst))
       
    61     except LookupError, k:
       
    62         raise Abort(_("%s, please check your locale settings") % k)
    58 
    63 
    59 def locallen(s):
    64 def locallen(s):
    60     """Find the length in characters of a local string"""
    65     """Find the length in characters of a local string"""
    61     return len(s.decode(_encoding, "replace"))
    66     return len(s.decode(_encoding, "replace"))
    62 
    67 
    68         else:
    73         else:
    69             u = u[:a]
    74             u = u[:a]
    70         return u.encode(_encoding, _encodingmode)
    75         return u.encode(_encoding, _encodingmode)
    71     except UnicodeDecodeError, inst:
    76     except UnicodeDecodeError, inst:
    72         sub = s[max(0, inst.start-10), inst.start+10]
    77         sub = s[max(0, inst.start-10), inst.start+10]
    73         raise Abort("decoding near '%s': %s!\n" % (sub, inst))
    78         raise Abort(_("decoding near '%s': %s!\n") % (sub, inst))
    74 
    79 
    75 # used by parsedate
    80 # used by parsedate
    76 defaultdateformats = (
    81 defaultdateformats = (
    77     '%Y-%m-%d %H:%M:%S',
    82     '%Y-%m-%d %H:%M:%S',
    78     '%Y-%m-%d %I:%M:%S%p',
    83     '%Y-%m-%d %I:%M:%S%p',