TurboGears / tg2

Python web framework with full-stack layer implemented on top of a microframework core with support for SQL DBMS, MongoDB and Pluggable Applications
http://www.turbogears.org/
Other
806 stars 78 forks source link

py3: make LazyString objects iterable #68

Closed nphilipp closed 9 years ago

nphilipp commented 9 years ago

This is necessary so that Genshi > 0.7 can render LazyString objects with Python 3.

amol- commented 9 years ago

Can you point out a short example to reproduce the issue so that I can embed it in the test suite? (just using genshi+LazyString without a TGApp should suffice)

nphilipp commented 9 years ago

Here you go:

from genshi.template.markup import MarkupTemplate
from tg.util import LazyString

markup = """<b xmlns:py="http://genshi.edgewall.org/">${foo}</b>"""
template = MarkupTemplate(markup)
print(template.generate(foo=LazyString(lambda: "bar")))
...
TypeError: 'LazyString' object is not iterable

This will work:

from genshi.template.markup import MarkupTemplate
from tg.util import LazyString

class MyLazyString(LazyString):
    def __iter__(self):
        return iter(self.eval())

markup = """<b xmlns:py="http://genshi.edgewall.org/">${foo}</b>"""
template = MarkupTemplate(markup)
print(template.generate(foo=MyLazyString(lambda: "bar")))
...
<b>bar</b>
amol- commented 9 years ago

committed as 2c816b9