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

login
register
mail settings
Submitter Dan Villiom Podlaski Christiansen
Date Nov. 29, 2014, 2:52 p.m.
Message ID <1f548aff09cd75a949bc.1417272731@dookie.local>
Download mbox | patch
Permalink /patch/6887/
State Not Applicable
Headers show

Comments

Dan Villiom Podlaski Christiansen - Nov. 29, 2014, 2:52 p.m.
# HG changeset patch
# User Dan Villiom Podlaski Christiansen <dan@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.

Patch

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):