pfalcon / utemplate

Micro template engine in Python with low memory usage, designed for Pycopy, a minimalist Python dialect, but also compatible with other Pythons.
https://github.com/pfalcon/pycopy
76 stars 8 forks source link

Immediate inclusion breaks predictability of function names in compiled templates #5

Closed cefn closed 7 years ago

cefn commented 7 years ago

This is demonstrated by https://github.com/cefn/utemplate/blob/demo_render_breakage/examples/immediateinclude.tpl which immediately hands off to another template, without sending even a character beforehand.

The compiled code from... python3 ./utemplate_util.py run examples/immediateinclude.tpl ...looks as follows, with no render() function and two render1() functions...

# Autogenerated file
# Autogenerated file
def render1(*a, **d):
    for i in range(5):
        yield """| """
        yield str(i)
        yield """ | """
        yield str("%2d" % i ** 2)
        yield """ |
"""
def render1(*a, **d):
    yield from render1()

It is therefore impossible to run the template and utemplate_util.py predictably dies with...

Traceback (most recent call last):
  File "./utemplate_util.py", line 48, in <module>
    for x in ns["render"](*sys.argv[3:]):
KeyError: 'render'
cefn commented 7 years ago

Just for reference, (forgot to mention), a workaround to prevent this issue is to add a space at the beginning of the template, so it looks like... {% include 'squares.tpl' %} ...instead of {% include 'squares.tpl' %}

At that point whatever logic allocates the seq number as a suffix for the function name starts functioning properly. I'm a bit lost following how seq and renderX closures are used, (yield is new to me), so I haven't fathomed what change can solve this - don't have a good handle on how it works.

pfalcon commented 7 years ago

Should be fixed with b5540ca68fecfa606726c829e6605b73c93bbb13. (commit updated)