UnicodeDecodeError when string representation of context contains accented characters. #70

Open
benwah opened this Issue Jul 19, 2012 · 2 comments

Comments

Projects
None yet
2 participants
@benwah

benwah commented Jul 19, 2012

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.

@benwah

This comment has been minimized.

Show comment Hide comment
@benwah

benwah Jul 19, 2012

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')))

benwah commented Jul 19, 2012

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')))
@fizyk

This comment has been minimized.

Show comment Hide comment
@fizyk

fizyk Aug 9, 2012

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 for Rendering Value in Rendering section

fizyk commented Aug 9, 2012

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 for Rendering Value in Rendering section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment