Edgewall Software

Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#229 closed defect (invalid)

Problem using dictionary with defined()

Reported by: anonymous Owned by: cmlenz
Priority: major Milestone:
Component: General Version: 0.4.4
Keywords: Cc: simon@…

Description

Hi,

I'm not sure if I'm doing something wrong (I'm new to Genshi), but the following code gives me an error on python2.5 on Mac OS X 10.4.11:

import genshi.template

template = """\
<p xmlns:py="http://genshi.edgewall.org/">
<py:if test="defined(my_dict)">
<strong>${my_dict.key}</strong>
</py:if>
</p>
"""

print genshi.template.MarkupTemplate(template) \
                     .generate(my_dict={"key" : "value"}) \
                     .render()
Traceback (most recent call last):
  File "genshi-test.py", line 12, in <module>
    .generate(my_dict={"key" : "value"}) \
  File "/opt/local/lib/python2.5/site-packages/genshi/core.py", line 154, in render
    return encode(generator, method=method, encoding=encoding)
  File "/opt/local/lib/python2.5/site-packages/genshi/output.py", line 45, in encode
    output = u''.join(list(iterator))
  File "/opt/local/lib/python2.5/site-packages/genshi/output.py", line 176, in __call__
    for kind, data, pos in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/output.py", line 519, in __call__
    for kind, data, pos in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/output.py", line 679, in __call__
    for kind, data, pos in chain(stream, [(None, None, None)]):
  File "/opt/local/lib/python2.5/site-packages/genshi/output.py", line 459, in __call__
    for ev in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/core.py", line 212, in _ensure
    for event in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/template/markup.py", line 297, in _match
    for event in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/template/markup.py", line 242, in _exec
    for event in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 398, in _eval
    for kind, data, pos in stream:
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 447, in _flatten
    substream = _apply_directives(substream, ctxt, directives)
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 256, in _apply_directives
    stream = directives[0](iter(stream), ctxt, directives[1:])
  File "/opt/local/lib/python2.5/site-packages/genshi/template/directives.py", line 386, in __call__
    if self.expr.evaluate(ctxt):
  File "/opt/local/lib/python2.5/site-packages/genshi/template/eval.py", line 136, in evaluate
    return eval(self.code, _globals, {'data': data})
  File "<string>", line 2, in <Expression u'defined(my_dict)'>
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 137, in defined
    return name in self
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 153, in __contains__
    return self._find(key)[1] is not None
  File "/opt/local/lib/python2.5/site-packages/genshi/template/base.py", line 202, in _find
    if key in frame:
TypeError: dict objects are unhashable

But as the following also doesn't work I'm not sure if this maybe my mistake:

import genshi.template

template = """\
<p xmlns:py="http://genshi.edgewall.org/">
<py:if test="defined(no_dict)">
<strong>${no_dict}</strong>
</py:if>
</p>
"""

print genshi.template.MarkupTemplate(template) \
                     .generate(no_dict="string") \
                     .render()
<p>
</p>

Thanks for your help, Simon

Change History (2)

comment:1 Changed 16 years ago by cmlenz

  • Milestone 0.5 deleted
  • Resolution set to invalid
  • Status changed from new to closed

You need to pass a string to the defined() function, namely the name under which you expect the variable. Otherwise there's already a variable lookup even before you call defined. So use:

  defined('my_dict')

and not:

  defined(my_dict)

comment:2 Changed 16 years ago by anonymous

Thanks for the hint, this was really stupid of me. Now everything works fine.

Simon

Note: See TracTickets for help on using tickets.