[PATCH] template engine: convert generator-based iterator to list-based iterator

Weiwen Liu weiwen at fb.com
Wed Nov 28 17:17:54 CST 2012


# HG changeset patch
# User Weiwen <weiwen at fb.com>
# Date 1354143342 28800
# Node ID e2a5f8356df726f19460a5423a430b5bbd4fe87d
# Parent  83aa4359c49f67bcb98fb9c7d885ed4ac7443239
template engine: convert generator-based iterator to list-based iterator

If a template iterator is implemented with generator, the iterator is exhausted
after we use it.  This leads to undesired behavior in template.  This change
converts a generator-based iterator to list-based iterator when template engine
first detects a generator-based iterator.  All future usages of iterator will
use list instead.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -8,6 +8,7 @@
 from i18n import _
 import sys, os, re
 import util, config, templatefilters, parser, error
+import types

 # template parsing

@@ -140,6 +141,10 @@
         v = context._defaults.get(key, '')
     if util.safehasattr(v, '__call__'):
         return v(**mapping)
+    if isinstance(v, types.GeneratorType):
+        v = list(v)
+        mapping[key] = v
+        return v
     return v

 def buildfilter(exp, context):

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://selenic.com/pipermail/mercurial-devel/attachments/20121128/808f4edc/attachment.html>


More information about the Mercurial-devel mailing list