Mercurial > hg
comparison mercurial/templater.py @ 29816:034412ca28c3
templater: fix if() to not evaluate False as bool('False')
Before, False was True. This patch fixes the issue by processing True/False
transparently. The other values (including integer 0) are tested as strings
for backward compatibility, which means "if(latesttagdistance)" never be False.
Should we change the behavior of "if(0)" as well?
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 18 Aug 2016 16:29:22 +0900 |
parents | 0d5cc0c18b4e |
children | cc11079644fc |
comparison
equal
deleted
inserted
replaced
29815:0d5cc0c18b4e | 29816:034412ca28c3 |
---|---|
287 thing = func(context, mapping, data) | 287 thing = func(context, mapping, data) |
288 if isinstance(thing, types.GeneratorType): | 288 if isinstance(thing, types.GeneratorType): |
289 thing = stringify(thing) | 289 thing = stringify(thing) |
290 return thing | 290 return thing |
291 | 291 |
292 def evalboolean(context, mapping, arg): | |
293 func, data = arg | |
294 thing = func(context, mapping, data) | |
295 if isinstance(thing, bool): | |
296 return thing | |
297 # other objects are evaluated as strings, which means 0 is True, but | |
298 # empty dict/list should be False as they are expected to be '' | |
299 return bool(stringify(thing)) | |
300 | |
292 def evalinteger(context, mapping, arg, err): | 301 def evalinteger(context, mapping, arg, err): |
293 v = evalfuncarg(context, mapping, arg) | 302 v = evalfuncarg(context, mapping, arg) |
294 try: | 303 try: |
295 return int(v) | 304 return int(v) |
296 except (TypeError, ValueError): | 305 except (TypeError, ValueError): |
558 an expression.""" | 567 an expression.""" |
559 if not (2 <= len(args) <= 3): | 568 if not (2 <= len(args) <= 3): |
560 # i18n: "if" is a keyword | 569 # i18n: "if" is a keyword |
561 raise error.ParseError(_("if expects two or three arguments")) | 570 raise error.ParseError(_("if expects two or three arguments")) |
562 | 571 |
563 test = evalstring(context, mapping, args[0]) | 572 test = evalboolean(context, mapping, args[0]) |
564 if test: | 573 if test: |
565 yield args[1][0](context, mapping, args[1][1]) | 574 yield args[1][0](context, mapping, args[1][1]) |
566 elif len(args) == 3: | 575 elif len(args) == 3: |
567 yield args[2][0](context, mapping, args[2][1]) | 576 yield args[2][0](context, mapping, args[2][1]) |
568 | 577 |