comparison mercurial/templater.py @ 24280:6c55e37ba5f2

templater: allow piping generator-type function output to filters Template functions use "yield"s assuming that the result will be combined into a string, which means both "f -> str" and "f -> generator" should behave in the same way. Before this patch, piping generator function resulted in a cryptic error. We had to insert "|stringify" in this case. $ hg log --template '{if(author, author)|user}\n' abort: template filter 'userfilter' is not compatible with keyword '[(<function runsymbol at 0x7f5af2e8d8c0>, 'author'), (<function runsymbol at 0x7f5af2e8d8c0>, 'author')]'
author Yuya Nishihara <yuya@tcha.org>
date Tue, 24 Feb 2015 00:04:55 +0900
parents bd504d90588d
children 15afda349b11
comparison
equal deleted inserted replaced
24279:7cf9a9e0cf89 24280:6c55e37ba5f2
160 filt = getfilter(exp[2], context) 160 filt = getfilter(exp[2], context)
161 return (runfilter, (func, data, filt)) 161 return (runfilter, (func, data, filt))
162 162
163 def runfilter(context, mapping, data): 163 def runfilter(context, mapping, data):
164 func, data, filt = data 164 func, data, filt = data
165 # func() may return string, generator of strings or arbitrary object such
166 # as date tuple, but filter does not want generator.
167 thing = func(context, mapping, data)
168 if isinstance(thing, types.GeneratorType):
169 thing = stringify(thing)
165 try: 170 try:
166 return filt(func(context, mapping, data)) 171 return filt(thing)
167 except (ValueError, AttributeError, TypeError): 172 except (ValueError, AttributeError, TypeError):
168 if isinstance(data, tuple): 173 if isinstance(data, tuple):
169 dt = data[1] 174 dt = data[1]
170 else: 175 else:
171 dt = data 176 dt = data