UnicodeDecodeError: 'ascii' codec inside controller

61 views
Skip to first unread message

Carlos Alberto Costa Beppler

unread,
Nov 1, 2012, 8:32:57 AM11/1/12
to we...@googlegroups.com
I´m having an very strange UnicodeDecodeError on the following program. 

The most strange is that before start the application the same formatting instruction works, it raises the error only inside the GET (or any other controller method).

The test program is:
==============================
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import web

class Index(object):
    def GET(self, id = None):
        if id:
         # this raises UnicodeDecodeError
         msg = 'Não encontrado: %s' % web.ctx.path
         raise web.notfound(msg)
        return 'Encontrei'

urls = (
    '/(.*)', 'Index'
)

application = web.application(urls, globals())

# this works
msg = 'Não encontrado: %s' % '/test'
web.debug(msg)

if __name__=='__main__':
    application.run()
==============================


The log message generated on server is:
==============================
beppler@squeeze:~/site$ python test.py
'N\xc3\xa3o encontrado: /test'
'N\xc3\xa3o encontrado: /test'
Traceback (most recent call last):
  File "/home/beppler/Projects/elco/src/web/application.py", line 239, in process
    return self.handle()
  File "/home/beppler/Projects/elco/src/web/application.py", line 230, in handle
    return self._delegate(fn, self.fvars, args)
  File "/home/beppler/Projects/elco/src/web/application.py", line 462, in _delegate
    return handle_class(cls)
  File "/home/beppler/Projects/elco/src/web/application.py", line 438, in handle_class
    return tocall(*args)
  File "/home/beppler/Projects/elco/src/site/test.py", line 17, in GET
    msg = 'Não encontrado: %s' % web.ctx.path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
==============================

Andrey Kuzmin

unread,
Nov 1, 2012, 9:57:27 AM11/1/12
to we...@googlegroups.com
Have you tried marking your string as unicode?

msg = u'Não encontrado: %s' % web.ctx.path

Carlos Alberto Costa Beppler

unread,
Nov 1, 2012, 11:58:40 AM11/1/12
to we...@googlegroups.com
Thanks.

It worked.

I understand that this exception happening because of the web.ctx.path is an unicode string and the % operator is converting the "format" string to unicode using the defatult encode. Is this right?
Reply all
Reply to author
Forward
0 new messages