##// END OF EJS Templates
templatefilters: try round-trip utf-8 conversion by json filter (issue4933)...
Yuya Nishihara -
r26856:baa77652 stable
parent child Browse files
Show More
@@ -197,7 +197,11 b' def json(obj):'
197 return {None: 'null', False: 'false', True: 'true'}[obj]
197 return {None: 'null', False: 'false', True: 'true'}[obj]
198 elif isinstance(obj, int) or isinstance(obj, float):
198 elif isinstance(obj, int) or isinstance(obj, float):
199 return str(obj)
199 return str(obj)
200 elif isinstance(obj, encoding.localstr):
201 u = encoding.fromlocal(obj).decode('utf-8') # can round-trip
202 return '"%s"' % jsonescape(u)
200 elif isinstance(obj, str):
203 elif isinstance(obj, str):
204 # no encoding.fromlocal() because it may abort if obj can't be decoded
201 u = unicode(obj, encoding.encoding, 'replace')
205 u = unicode(obj, encoding.encoding, 'replace')
202 return '"%s"' % jsonescape(u)
206 return '"%s"' % jsonescape(u)
203 elif isinstance(obj, unicode):
207 elif isinstance(obj, unicode):
@@ -3479,3 +3479,26 b' Test broken string escapes:'
3479 $ hg log -T "\\xy" -R a
3479 $ hg log -T "\\xy" -R a
3480 hg: parse error: invalid \x escape
3480 hg: parse error: invalid \x escape
3481 [255]
3481 [255]
3482
3483 Set up repository for non-ascii encoding tests:
3484
3485 $ hg init nonascii
3486 $ cd nonascii
3487 $ python <<EOF
3488 > open('utf-8', 'w').write('\xc3\xa9')
3489 > EOF
3490 $ HGENCODING=utf-8 hg branch -q `cat utf-8`
3491 $ HGENCODING=utf-8 hg ci -qAm 'non-ascii branch' utf-8
3492
3493 json filter should try round-trip conversion to utf-8:
3494
3495 $ HGENCODING=ascii hg log -T "{branch|json}\n" -r0
3496 "\u00e9"
3497
3498 json filter should not abort if it can't decode bytes:
3499 (not sure the current behavior is right; we might want to use utf-8b encoding?)
3500
3501 $ HGENCODING=ascii hg log -T "{'`cat utf-8`'|json}\n" -l1
3502 "\ufffd\ufffd"
3503
3504 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now