template engine: convert generator-based iterator to list-based iterator
authorWeiwen <weiwen@fb.com>
Wed, 28 Nov 2012 14:55:42 -0800
changeset 17982 e06e9fd2d99f
parent 17980 83aa4359c49f
child 17983 c64e646af81e
template engine: convert generator-based iterator to list-based iterator If a template iterator is implemented with generator, the iterator is exhau= sted after we use it. This leads to undesired behavior in template. This chang= e converts a generator-based iterator to list-based iterator when template en= gine first detects a generator-based iterator. All future usages of iterator wi= ll use list instead.
mercurial/templater.py
--- a/mercurial/templater.py	Wed Nov 28 16:15:05 2012 -0600
+++ b/mercurial/templater.py	Wed Nov 28 14:55:42 2012 -0800
@@ -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):