[PATCH 4 of 5 perfarce] add support for a fallback encoding

Dan Villiom Podlaski Christiansen dan at cabo.dk
Sat Nov 29 14:52:11 UTC 2014


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <dan at cabo.dk>
# Date 1417271360 -3600
#      Sat Nov 29 15:29:20 2014 +0100
# Node ID 1f548aff09cd75a949bce95d88e4a6f80e449304
# Parent  d85252d09b88e14b703f0db17a6b5dd5cadbfbed
add support for a fallback encoding

...and default to UTF-8 encoding if none specified, as it almost never
results in false positives.

diff --git a/perfarce.py b/perfarce.py
--- a/perfarce.py
+++ b/perfarce.py
@@ -427,16 +427,31 @@ class p4client(object):
         e = os.environ.get("P4CHARSET")
         if e:
             return emap.get(e,e)
-        return self.ui.config('perfarce', 'encoding', None)
+        return self.ui.config('perfarce', 'encoding', 'utf-8')
+
+    @propertycache
+    def fallbackencoding(self):
+        return self.ui.config('perfarce', 'fallbackencoding', None)
 
     def decode(self, text):
         'decode text in p4 character set as utf-8'
 
-        if self.encoding:
+        try:
+            text = text.decode(self.encoding)
+        except LookupError, e:
+            raise error.Abort("%s, please check your locale settings" % e)
+        except UnicodeDecodeError:
+            if not self.fallbackencoding:
+                raise
+
             try:
-                return text.decode(self.encoding).encode(encoding.encoding)
+                text = text.decode(self.fallbackencoding)
             except LookupError, e:
-                raise error.Abort("%s, please check your locale settings" % e)
+                msg = "%s, please check your locale settings" % e
+                raise error.Abort(msg)
+
+        text = text.encode(encoding.encoding)
+
         return text
 
     def encode(self, text):


More information about the Mercurial-devel mailing list