Edgewall Software

Opened 14 years ago

Last modified 12 years ago

#428 reopened defect

UndefinedError in py:for

Reported by: anonymous Owned by: cmlenz
Priority: major Milestone:
Component: Template processing Version: devel
Keywords: python3 UndefinedError Cc:

Description (last modified by cboos)

This is the code I'm running in the Python interpreter from the Documentation:

>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
...   <li py:for="item in items">${item}</li>
... </ul>''')
>>> print(tmpl.generate(items=[1, 2, 3]))
<ul>
  <li>1</li><li>2</li><li>3</li>
</ul>

And I'm getting an UndefinedError for the variable item. If I force lenient mode, the template simply does not evaluate.

Change History (10)

comment:1 Changed 14 years ago by gatlin@…

I'll come clean: I formatted that poorly. Here is the relevant code again:

>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
...   <li py:for="item in items">${item}</li>
... </ul>''')
>>> print(tmpl.generate(items=[1, 2, 3]))
<ul>
  <li>1</li><li>2</li><li>3</li>
</ul>

comment:2 Changed 14 years ago by cboos

  • Description modified (diff)

Works for me, Python 2.7 on Windows and Genshi 0.6 (and other combinations).

Can you please give more details about your version of Python and Genshi?

comment:3 Changed 14 years ago by gatlin@…

I'm on Arch Linux, using Python 2.7.1 and Genshi 0.6 from the official repositories. It gives a traceback and then genshi.template.eval.UndefinedError?: "item" not defined.

comment:4 Changed 14 years ago by cboos

I can't try on Linux right now, but on Windows 7 (x64):

Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from genshi.template import MarkupTemplate
>>> tmpl = MarkupTemplate('''<ul xmlns:py="http://genshi.edgewall.org/">
...   <li py:for="item in items">${item}</li>
... </ul>''')
>>> print(tmpl.generate(items=[1, 2, 3]))
<ul>
  <li>1</li><li>2</li><li>3</li>
</ul>
>>>
>>> import genshi
>>> genshi.__version__
'0.6'

comment:5 Changed 13 years ago by cboos

  • Milestone 0.7 deleted
  • Resolution set to worksforme
  • Status changed from new to closed

As expected, it also works on Linux for me (python 2.7.1, Genshi 0.6).

Probably an installation issue (an older version that gets loaded first?).

comment:6 Changed 12 years ago by anonymous

  • Resolution worksforme deleted
  • Status changed from closed to reopened

Doesn't work for me either. I use python 3.2 and Genshi-0.7dev-py3.2.egg

comment:7 Changed 12 years ago by anonymous

  • Priority changed from major to critical
  • Version changed from 0.6 to devel

Upon closer examination/debugging, the "item" variable is not available in any namespace by the time the lookup_name function parses it. It seems the entire for statement + the contained statement is parsed at the same time, so the "item" variable hasn't become available.

def lookup_name(cls, data, name):

traceback_hide = True val = data.get(name, UNDEFINED) if val is UNDEFINED:

val = BUILTINS.get(name, val) if val is UNDEFINED:

val = cls.undefined(name)

comment:8 Changed 12 years ago by anonymous

  • Keywords python3 UndefinedError added

Even stranger, len(menu) claims that is has 2 records. Still, this only yields one "Hello.":

      <li py:for="menuitem in ${menu}">
         Hello.
      </li>

And once again, I run python 3.2.

comment:9 Changed 12 years ago by anonymous

  • Component changed from General to Template processing

It seems to be even worse, iteration doesn't seem work at all in my setup, this:

      <li py:for="i in range(0,3)">
        sdfsdfsdf
      </li>

produces only one row.

comment:10 Changed 12 years ago by anonymous

  • Priority changed from critical to major

I'll keep posting on this issue:

Strangely, If I use a construct like this:

  <div py:match="left_content">
     <ol>
      <li py:for="i in range(0,3)">
        sdfsdfsdf
      </li>
    </ol>
  </div>

and calls it from an included file, the target tag gets the proper 3 items in it's list. So suddenly, it works! It must be a different context or something.

Anyway, now there is a workaround. Simply put, it works if you put is in an example like this: http://genshi.edgewall.org/wiki/GenshiRecipes/PyLayoutEquivalent

Note: See TracTickets for help on using tickets.