Mercurial > public > mercurial-scm > hg
comparison mercurial/templatefuncs.py @ 37015:a318bb154d42
templatefuncs: do not stringify result of if*() expression
Returning a generator means that the result is a byte string. I can't find
any reason to make the "if" condition lazy since it is evaluated anyway
when {if()} has to be evaluated. So let's simply make if*() return an input
expression unmodified.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 19 Mar 2018 22:10:40 +0900 |
parents | 521f6c7e1756 |
children | 0fb28899e81a |
comparison
equal
deleted
inserted
replaced
37014:a5311d7f4af8 | 37015:a318bb154d42 |
---|---|
246 # i18n: "if" is a keyword | 246 # i18n: "if" is a keyword |
247 raise error.ParseError(_("if expects two or three arguments")) | 247 raise error.ParseError(_("if expects two or three arguments")) |
248 | 248 |
249 test = evalboolean(context, mapping, args[0]) | 249 test = evalboolean(context, mapping, args[0]) |
250 if test: | 250 if test: |
251 yield evalrawexp(context, mapping, args[1]) | 251 return evalrawexp(context, mapping, args[1]) |
252 elif len(args) == 3: | 252 elif len(args) == 3: |
253 yield evalrawexp(context, mapping, args[2]) | 253 return evalrawexp(context, mapping, args[2]) |
254 | 254 |
255 @templatefunc('ifcontains(needle, haystack, then[, else])') | 255 @templatefunc('ifcontains(needle, haystack, then[, else])') |
256 def ifcontains(context, mapping, args): | 256 def ifcontains(context, mapping, args): |
257 """Conditionally execute based | 257 """Conditionally execute based |
258 on whether the item "needle" is in "haystack".""" | 258 on whether the item "needle" is in "haystack".""" |
267 found = (needle in haystack) | 267 found = (needle in haystack) |
268 except error.ParseError: | 268 except error.ParseError: |
269 found = False | 269 found = False |
270 | 270 |
271 if found: | 271 if found: |
272 yield evalrawexp(context, mapping, args[2]) | 272 return evalrawexp(context, mapping, args[2]) |
273 elif len(args) == 4: | 273 elif len(args) == 4: |
274 yield evalrawexp(context, mapping, args[3]) | 274 return evalrawexp(context, mapping, args[3]) |
275 | 275 |
276 @templatefunc('ifeq(expr1, expr2, then[, else])') | 276 @templatefunc('ifeq(expr1, expr2, then[, else])') |
277 def ifeq(context, mapping, args): | 277 def ifeq(context, mapping, args): |
278 """Conditionally execute based on | 278 """Conditionally execute based on |
279 whether 2 items are equivalent.""" | 279 whether 2 items are equivalent.""" |
282 raise error.ParseError(_("ifeq expects three or four arguments")) | 282 raise error.ParseError(_("ifeq expects three or four arguments")) |
283 | 283 |
284 test = evalstring(context, mapping, args[0]) | 284 test = evalstring(context, mapping, args[0]) |
285 match = evalstring(context, mapping, args[1]) | 285 match = evalstring(context, mapping, args[1]) |
286 if test == match: | 286 if test == match: |
287 yield evalrawexp(context, mapping, args[2]) | 287 return evalrawexp(context, mapping, args[2]) |
288 elif len(args) == 4: | 288 elif len(args) == 4: |
289 yield evalrawexp(context, mapping, args[3]) | 289 return evalrawexp(context, mapping, args[3]) |
290 | 290 |
291 @templatefunc('join(list, sep)') | 291 @templatefunc('join(list, sep)') |
292 def join(context, mapping, args): | 292 def join(context, mapping, args): |
293 """Join items in a list with a delimiter.""" | 293 """Join items in a list with a delimiter.""" |
294 if not (1 <= len(args) <= 2): | 294 if not (1 <= len(args) <= 2): |