diff mercurial/templatefilters.py @ 26856:baa77652be68 stable

templatefilters: try round-trip utf-8 conversion by json filter (issue4933) As JSON string is known to be a unicode, we should try round-trip conversion for localstr type. This patch tests localstr type explicitly because encoding.fromlocal() may raise Abort for undecodable str, which is probably not what we want. Maybe we can refactor json filter to use encoding module more later. Still "{desc|json}" can't round-trip because showdescription() modifies a localstr object.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 04 Nov 2015 23:48:15 +0900
parents 7012be5ab5bd
children f9984f76fd90
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Tue Nov 03 12:16:54 2015 -0800
+++ b/mercurial/templatefilters.py	Wed Nov 04 23:48:15 2015 +0900
@@ -197,7 +197,11 @@
         return {None: 'null', False: 'false', True: 'true'}[obj]
     elif isinstance(obj, int) or isinstance(obj, float):
         return str(obj)
+    elif isinstance(obj, encoding.localstr):
+        u = encoding.fromlocal(obj).decode('utf-8')  # can round-trip
+        return '"%s"' % jsonescape(u)
     elif isinstance(obj, str):
+        # no encoding.fromlocal() because it may abort if obj can't be decoded
         u = unicode(obj, encoding.encoding, 'replace')
         return '"%s"' % jsonescape(u)
     elif isinstance(obj, unicode):