Patchwork [1,of,4] templater: fix truth testing of integer 0 taken from a list/dict

login
register
mail settings
Submitter Yuya Nishihara
Date June 23, 2018, 9:51 a.m.
Message ID <720fc7b592ef6dd5dc11.1529747498@mimosa>
Download mbox | patch
Permalink /patch/32388/
State New
Headers show

Comments

Yuya Nishihara - June 23, 2018, 9:51 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1529219438 -32400
#      Sun Jun 17 16:10:38 2018 +0900
# Node ID 720fc7b592ef6dd5dc11a7ec48a715ee39c1006a
# Parent  36edfbac7281c731b1aed8e73555b22c2ccb96c0
templater: fix truth testing of integer 0 taken from a list/dict

Broken at f9c426385853. bool(python_value) shouldn't be used here since
an integer 0 has to be truthy for backward compatibility.

Patch

diff --git a/mercurial/templateutil.py b/mercurial/templateutil.py
--- a/mercurial/templateutil.py
+++ b/mercurial/templateutil.py
@@ -350,7 +350,8 @@  class hybriditem(mappable, wrapped):
         return gen
 
     def tobool(self, context, mapping):
-        return bool(self.tovalue(context, mapping))
+        w = makewrapped(context, mapping, self._value)
+        return w.tobool(context, mapping)
 
     def tovalue(self, context, mapping):
         return _unthunk(context, mapping, self._value)
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -932,6 +932,8 @@  Test boolean expression/literal passed t
   rev 0 is True
   $ hg log -r 0 -T '{if(0, "literal 0 is True as well")}\n'
   literal 0 is True as well
+  $ hg log -r 0 -T '{if(min(revset(r"0")), "0 of hybriditem is also True")}\n'
+  0 of hybriditem is also True
   $ hg log -r 0 -T '{if("", "", "empty string is False")}\n'
   empty string is False
   $ hg log -r 0 -T '{if(revset(r"0 - 0"), "", "empty list is False")}\n'