Patchwork [09,of,11] templatefilters: handle TypeError by count()

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

Comments

Yuya Nishihara - March 31, 2018, 1:49 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1521359264 -32400
#      Sun Mar 18 16:47:44 2018 +0900
# Node ID 68b69a593cf0865a1485d10a87c58f1c3254fb76
# Parent  068cf6f31d4733d74d084d0cca61a9ff04939ea5
templatefilters: handle TypeError by count()

Prepares for removing the weird exception catcher from runfilter().

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -11,6 +11,7 @@  import os
 import re
 import time
 
+from .i18n import _
 from . import (
     encoding,
     error,
@@ -101,7 +102,10 @@  def basename(path):
 @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 --git a/tests/test-command-template.t b/tests/test-command-template.t
--- a/tests/test-command-template.t
+++ b/tests/test-command-template.t
@@ -2277,6 +2277,11 @@  Count filter:
   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 min/max of if() result
 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), "")}'