#13253 closed defect (fixed)
UnicodeEncodeError raised while rendering timeline rss when author of event is unknown and unicode
Reported by: | Jun Omae | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | 1.0.20 |
Component: | timeline | Version: | |
Severity: | normal | Keywords: | genshi |
Cc: | Branch: | ||
Release Notes: |
Fix |
||
API Changes: | |||
Internal Changes: |
Description
I noticed UnicodeEncodeError raised while rendering timeline rss on production environment.
2020-02-12 18:42:48,520 ERROR: Genshi UnicodeEncodeError error while rendering template (unknown template location) Traceback (most recent call last): File "/venv/lib/python2.7/site-packages/trac/web/chrome.py", line 1180, in iterable_content for chunk in stream.serialize(method, **kwargs): File "/venv/lib/python2.7/site-packages/genshi/output.py", line 241, in __call__ for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/output.py", line 671, in __call__ for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/output.py", line 776, in __call__ for kind, data, pos in chain(stream, [(None, None, None)]): File "/venv/lib/python2.7/site-packages/genshi/output.py", line 596, in __call__ for ev in stream: File "/venv/lib/python2.7/site-packages/genshi/core.py", line 292, in _ensure for event in stream: File "/venv/lib/python2.7/site-packages/genshi/core.py", line 292, in _ensure for event in stream: File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 639, in _include for event in stream: File "/venv/lib/python2.7/site-packages/genshi/template/markup.py", line 327, in _match for event in stream: File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 579, in _flatten for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/template/directives.py", line 726, in __call__ value = _eval_expr(expr, ctxt, vars) File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 289, in _eval_expr retval = expr.evaluate(ctxt) File "/venv/lib/python2.7/site-packages/genshi/template/eval.py", line 178, in evaluate return eval(self.code, _globals, {'__data__': data}) File "/venv/lib/python2.7/site-packages/trac/templates/author_or_creator.rss", line 16, in <Expression '?'> <py:with vars="author = email_map[author] if show_email_addresses and email_map and '@' not in author else author"> File "/venv/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 characters in position 0-15: ordinal not in range(128)
The issue occurs when email_map[author]
expression is evaluated in genshi template and author
is not existent in email_map
dict.
How to reproduce same exception
>>> from genshi.template import MarkupTemplate >>> t = MarkupTemplate('<x xmlns:py="http://genshi.edgewall.org/" py:with="v=d[k]" />') >>> unicode(t.generate(d={'key': 42}, k=u'\u200b'*16)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/venv/lib/python2.7/site-packages/genshi/core.py", line 251, in __unicode__ return self.render(encoding=None) File "/venv/lib/python2.7/site-packages/genshi/core.py", line 184, in render return encode(generator, method=method, encoding=encoding, out=out) File "/venv/lib/python2.7/site-packages/genshi/output.py", line 57, in encode return _encode(''.join(list(iterator))) File "/venv/lib/python2.7/site-packages/genshi/output.py", line 241, in __call__ for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/output.py", line 671, in __call__ for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/output.py", line 776, in __call__ for kind, data, pos in chain(stream, [(None, None, None)]): File "/venv/lib/python2.7/site-packages/genshi/output.py", line 596, in __call__ for ev in stream: File "/venv/lib/python2.7/site-packages/genshi/core.py", line 274, in _ensure event = stream.next() File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 639, in _include for event in stream: File "/venv/lib/python2.7/site-packages/genshi/template/markup.py", line 327, in _match for event in stream: File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 579, in _flatten for kind, data, pos in stream: File "/venv/lib/python2.7/site-packages/genshi/template/directives.py", line 726, in __call__ value = _eval_expr(expr, ctxt, vars) File "/venv/lib/python2.7/site-packages/genshi/template/base.py", line 289, in _eval_expr retval = expr.evaluate(ctxt) File "/venv/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 "/venv/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 characters in position 0-15: ordinal not in range(128)
Proposed changes
-
trac/templates/author_or_creator.rss
diff --git a/trac/templates/author_or_creator.rss b/trac/templates/author_or_creator.rss index e0a21bf90..25af76675 100644
a b Arguments: 13 13 py:with="email_map = value_of('email_map', None)" py:strip=""> 14 14 <py:if test="author"> 15 15 <!--! Try our best to retrieve an email address if wanted and possible --> 16 <py:with vars="author = email_map[author] if show_email_addresses and email_map and '@' not in author else author"> 16 <py:with vars="author = (email_map.get(author, author) 17 if show_email_addresses and email_map and '@' not in author 18 else author)"> 17 19 <py:choose> 18 20 <author py:when="show_email_addresses and '@' in author">${format_author(author)}</author> 19 21 <dc:creator py:otherwise="">${format_author(author)}</dc:creator> -
trac/timeline/tests/web_ui.py
diff --git a/trac/timeline/tests/web_ui.py b/trac/timeline/tests/web_ui.py index 8325198f3..4d24dbdf2 100644
a b class TimelineModuleTestCase(unittest.TestCase): 120 120 121 121 self.assertIn('prev', req.chrome['links']) 122 122 123 def test_render_rss_with_unicode_unknown_author(self): 124 self.env.config.set('trac', 'show_email_addresses', 'enabled') 125 self.env.known_users.append(('blah', 'Blah user', 'blah@example.org')) 126 req = MockRequest(self.env, path_info='/timeline', 127 args={'format': 'rss'}) 128 rv = TimelineModule(self.env).process_request(req) 129 self.assertEqual('timeline.rss', rv[0]) 130 event = ('mock', datetime_now(utc), u'jöé', None) 131 rv[1]['events'] = [ 132 {'kind': event[0], 'date': event[1], 'author': event[2], 133 'data': event[3], 'event': event, 'provider': None, 134 'dateuid': '42', 'render': lambda field, context: 'mock'}, 135 ] 136 output = Chrome(self.env).render_template(req, rv[0], rv[1], rv[2]) 137 self.assertIsNotNone(output) 138 123 139 124 140 def suite(): 125 141 suite = unittest.TestSuite()
Attachments (0)
Change History (5)
comment:1 by , 5 years ago
Milestone: | 1.0.20 → next-stable-1.4.x |
---|
comment:2 by , 4 years ago
Milestone: | next-stable-1.4.x → 1.4.3 |
---|
The change looks good to me. Feel free to move to 1.4.2 if you want to include the change in the upcoming release.
comment:3 by , 4 years ago
Milestone: | 1.4.3 → 1.0.20 |
---|
Sorry for my delay.
Proposed fix is depended on Genshi. I'll push to 1.0-stable, also I'm going to confirm whether the same issue is existent on 1.4-stable.
comment:4 by , 4 years ago
Release Notes: | modified (diff) |
---|---|
Resolution: | → fixed |
Status: | new → closed |
comment:5 by , 4 years ago
The root cause is a Genshi issue. I've filed at https://github.com/edgewall/genshi/issues/28.
Please move back to 1.0.20 if you wish to fix for release 1.0.20 (#13294).