Edgewall Software

Ticket #428 (reopened defect)

Opened 3 years ago

Last modified 2 years ago

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) (diff)

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.

Attachments

Change History

Changed 3 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>

Changed 3 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?

Changed 3 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.

Changed 3 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'

Changed 3 years ago by cboos

  • status changed from new to closed
  • resolution set to worksforme
  • milestone 0.7 deleted

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?).

Changed 2 years ago by anonymous

  • status changed from closed to reopened
  • resolution worksforme deleted

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

Changed 2 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)

Changed 2 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.

Changed 2 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.

Changed 2 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

Add/Change #428 (UndefinedError in py:for)

Author


E-mail address and user name can be saved in the Preferences.


Change Properties
<Author field>
Action
as reopened
as The resolution will be set. Next status will be 'closed'
to The owner will change from cmlenz. Next status will be 'new'
 
Note: See TracTickets for help on using tickets.