Mercurial > public > mercurial-scm > hg-stable
diff mercurial/templatekw.py @ 34426:12bfecd0ffe6
formatter: fix default list/dict generator to be evaluated more than once
Before, _hybrid.gen must be a generator which could be consumed only once.
It was okay in templatekw.py since template keywords are functions which
create temporary hybrid objects, but the formatter doesn't work in that way.
To work around the issue, this patch makes _hybrid.gen optionally be a
function returning a generator.
Thanks to Pulkit for finding this issue.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 01 Oct 2017 08:37:04 +0100 |
parents | 89aec1834a86 |
children | b3073e175c17 |
line wrap: on
line diff
--- a/mercurial/templatekw.py Wed Sep 27 21:38:48 2017 +0900 +++ b/mercurial/templatekw.py Sun Oct 01 08:37:04 2017 +0100 @@ -39,15 +39,12 @@ def __init__(self, gen, values, makemap, joinfmt): if gen is not None: - self.gen = gen + self.gen = gen # generator or function returning generator self._values = values self._makemap = makemap self.joinfmt = joinfmt - @util.propertycache def gen(self): - return self._defaultgen() - def _defaultgen(self): - """Generator to stringify this as {join(self, ' ')}""" + """Default generator to stringify this as {join(self, ' ')}""" for i, x in enumerate(self._values): if i > 0: yield ' ' @@ -104,9 +101,12 @@ def unwraphybrid(thing): """Return an object which can be stringified possibly by using a legacy template""" - if not util.safehasattr(thing, 'gen'): + gen = getattr(thing, 'gen', None) + if gen is None: return thing - return thing.gen + if callable(gen): + return gen() + return gen def unwrapvalue(thing): """Move the inner value object out of the wrapper""" @@ -685,7 +685,7 @@ # Format the successorssets def render(d): t = [] - for i in d.gen: + for i in d.gen(): t.append(i) return "".join(t)