pyramid_debugtoolbar
pyramid_debugtoolbar copied to clipboard
UnicodeDecodeError when string representation of context contains accented characters.
So I have a simple view that returns a MongoDB dataset:
def region_selector(request):
return {
'regions': Region.objects.all()
}
But since I added a unicode method to my Region model, pyramid_debugtoolbar breaks. Here's the stack:
Traceback (most recent call last):
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/pyramid/mako_templating.py", line 181, in __call__
result = template.render_unicode(**system)
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/Mako-0.7.0-py2.7.egg/mako/template.py", line 406, in render_unicode
as_unicode=True)
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/Mako-0.7.0-py2.7.egg/mako/runtime.py", line 764, in _render
**_kwargs_for_callable(callable_, data))
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/Mako-0.7.0-py2.7.egg/mako/runtime.py", line 796, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/Mako-0.7.0-py2.7.egg/mako/runtime.py", line 822, in _exec_template
callable_(context, *args, **kwargs)
File "/sites/envs/tuktu/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.2-py2.7.egg/pyramid_debugtoolbar/panels/templates/renderings.dbtmako", line 21, in render_body
<td colspan="2">${rendering['val']|h}</td>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 30: ordinal not in range(128)
I enabled pdb in renderings.py, in function content, maybe it will help:
> /sites/envs/tuktu/local/lib/python2.7/site-packages/pyramid_debugtoolbar-1.0.2-py2.7.egg/pyramid_debugtoolbar/panels/renderings.py(44)content()
-> return self.render(
(Pdb) print self.renderings[0]['val']
{'regions': [<Region: Saguenay–Lac-Saint-Jean>, <Region: Gaspésie–Îles-de-la-Madeleine>, <Region: Lanaudière>, <Region: Chaudière-Appalaches>, <Region: Montréal>, <Region: Nord-du-Québec>, <Region: Mauricie>, <Region: Côte-Nord>, <Region: Montérégie>, <Region: Bas-Saint-Laurent>, <Region: Abitibi-Témiscamingue>, <Region: Centre-du-Québec>, <Region: Laurentides>, <Region: Laval>, <Region: Outaouais>, <Region: Estrie>, <Region: Saint-Pierre et Miquelon>, <Region: t2>, <Region: t1>]}
(Pdb)
Note that the data type above is str.
Ok I found a fix.. I don't think it's a perfect fix though, am afraid it only works for utf8 data :P
in pyramid_debugtoolbar/panels/renderings.py, here's the diff:
27c27
< self.renderings.append(dict(name=name, system=dictrepr(event), val=val))
---
> self.renderings.append(dict(name=name, system=dictrepr(event), val=val.decode('utf8')))
same happens to me, making whole pyramid development server returning Internal Server Error.
If I return objects, which repr returns a string with utf8 encoded non-ascii characters (eg. ł), but almost works, when repr is unicode.
Almost, becouse I get