diff mercurial/templatefilters.py @ 28212:d4419c01532b

templatefilters: make json filter be byte-transparent (BC) (issue4926) This is necessary to preserve filename encoding over JSON. Instead, this patch inserts "|utf8" where non-ascii local-encoding texts can be passed to "|json". See also the commit that introduced "utf8" filter.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 27 Dec 2015 17:59:57 +0900
parents 8ddf893560fa
children 93b5c540db69
line wrap: on
line diff
--- a/mercurial/templatefilters.py	Mon Jan 04 23:05:09 2016 +0900
+++ b/mercurial/templatefilters.py	Sun Dec 27 17:59:57 2015 +0900
@@ -197,15 +197,8 @@
         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):
-        return '"%s"' % jsonescape(obj)
+        return '"%s"' % encoding.jsonescape(obj, paranoid=True)
     elif util.safehasattr(obj, 'keys'):
         out = []
         for k, v in sorted(obj.iteritems()):