Patchwork [10,of,11] templatefilters: raise ProgrammingError if unencodable type passed to json()

login
register
mail settings
Submitter Yuya Nishihara
Date March 31, 2018, 1:49 a.m.
Message ID <bf00dda4a053a9dbf716.1522460978@mimosa>
Download mbox | patch
Permalink /patch/30047/
State Accepted
Headers show

Comments

Yuya Nishihara - March 31, 2018, 1:49 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521359588 -32400
#      Sun Mar 18 16:53:08 2018 +0900
# Node ID bf00dda4a053a9dbf7163e993a454dd9dc03cb1f
# Parent  68b69a593cf0865a1485d10a87c58f1c3254fb76
templatefilters: raise ProgrammingError if unencodable type passed to json()

This shouldn't happen for any template data types (though I know it does
because of some templater bugs.) Let's clarify it is a bug.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -263,8 +263,7 @@  def json(obj, paranoid=True):
     elif util.safehasattr(obj, '__iter__'):
         out = [json(i, paranoid) for i in obj]
         return '[' + ', '.join(out) + ']'
-    else:
-        raise TypeError('cannot encode type %s' % obj.__class__.__name__)
+    raise error.ProgrammingError('cannot encode %r' % obj)
 
 @templatefilter('lower', intype=bytes)
 def lower(text):