edgewall / genshi

Python toolkit for generation of output for the web
http://genshi.edgewall.org
Other
86 stars 35 forks source link

UnicodeEncodeError is raised while evaluating py:with expression in Genshi template on Python 2.7 #28

Open jun66j5 opened 4 years ago

jun66j5 commented 4 years ago

UnicodeEncodeError is raised while evaluating py:with expression in Genshi template when non-existent key in a dict and the key is an unicode instance on Python 2.7. In Python 3.x, the issue doesn't occur.

(Originally reported at https://trac.edgewall.org/ticket/13253)

Python 2.7.12 (default, Apr 15 2020, 17:07:12)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import genshi
>>> genshi.__version__
'0.7.3'
>>> from genshi.template import MarkupTemplate
>>> t = MarkupTemplate('<x xmlns:py="http://genshi.edgewall.org/" py:with="v=d[k]" />')
>>> t.generate(d={}, k=u'\u00e9').render()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/core.py", line 184, in render
    return encode(generator, method=method, encoding=encoding, out=out)
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/output.py", line 57, in encode
    return _encode(''.join(list(iterator)))
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/output.py", line 241, in __call__
    for kind, data, pos in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/output.py", line 671, in __call__
    for kind, data, pos in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/output.py", line 776, in __call__
    for kind, data, pos in chain(stream, [(None, None, None)]):
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/output.py", line 596, in __call__
    for ev in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/core.py", line 274, in _ensure
    event = stream.next()
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/base.py", line 639, in _include
    for event in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/markup.py", line 327, in _match
    for event in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/base.py", line 579, in _flatten
    for kind, data, pos in stream:
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/directives.py", line 726, in __call__
    value = _eval_expr(expr, ctxt, vars)
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/base.py", line 289, in _eval_expr
    retval = expr.evaluate(ctxt)
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/eval.py", line 178, in evaluate
    return eval(self.code, _globals, {'__data__': data})
  File "<string>", line 1, in <Expression '?'>
  File "/dev/shm/genshi-py2/local/lib/python2.7/site-packages/genshi/template/eval.py", line 338, in lookup_item
    val = getattr(obj, key, UNDEFINED)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
>>>