# HG changeset patch # User Yuya Nishihara # Date 1521359264 -32400 # Node ID 05db42732fce64c50d30aed9e0d16bdab0827ed7 # Parent a0b17f744cbce9c4e194e8d7e25cbab0c389183b templatefilters: handle TypeError by count() Prepares for removing the weird exception catcher from runfilter(). diff -r a0b17f744cbc -r 05db42732fce mercurial/templatefilters.py --- a/mercurial/templatefilters.py Sun Mar 18 17:02:39 2018 +0900 +++ b/mercurial/templatefilters.py Sun Mar 18 16:47:44 2018 +0900 @@ -11,6 +11,7 @@ import re import time +from .i18n import _ from . import ( encoding, error, @@ -101,7 +102,10 @@ @templatefilter('count') def count(i): """List or text. Returns the length as an integer.""" - return len(i) + try: + return len(i) + except TypeError: + raise error.ParseError(_('not countable')) @templatefilter('dirname', intype=bytes) def dirname(path): diff -r a0b17f744cbc -r 05db42732fce tests/test-command-template.t --- a/tests/test-command-template.t Sun Mar 18 17:02:39 2018 +0900 +++ b/tests/test-command-template.t Sun Mar 18 16:47:44 2018 +0900 @@ -2277,6 +2277,11 @@ o 0: children: 1, tags: 0, file_adds: 1, ancestors: 1 + $ hg log -l1 -T '{termwidth|count}\n' + hg: parse error: not countable + (template filter 'count' is not compatible with keyword 'termwidth') + [255] + Upper/lower filters: $ hg log -r0 --template '{branch|upper}\n' @@ -3266,7 +3271,8 @@ Test laziness of if() then/else clause $ hg debugtemplate '{count(0)}' - abort: incompatible use of template filter 'count' + hg: parse error: not countable + (incompatible use of template filter 'count') [255] $ hg debugtemplate '{if(true, "", count(0))}' $ hg debugtemplate '{if(false, count(0), "")}'