Paster server encoding problems

If you have stumbled upon strange unicode encoding errors within Paste server like this one:

  File "/usr/lib/python2.6/site-packages/paste/httpserver.py", line 437, in handle_one_request
    self.wsgi_execute()
  File "/usr/lib/python2.6/site-packages/paste/httpserver.py", line 290, in wsgi_execute
    self.wsgi_write_chunk(chunk)
  File "/usr/lib/python2.6/site-packages/paste/httpserver.py", line 150, in wsgi_write_chunk
    self.wfile.write(chunk)
  File "/usr/lib64/python2.6/socket.py", line 292, in write
    data = str(data) # XXX Should really reject non-string non-buffers
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 1280: ordinal not in range(128)

 

You can easily fix those encoding bugs typing into config/environment.py (if you are using Pylons):

# Workaround stupid Paster encoding bugs
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

 

just before def load_environment() function. Module reload is needed, because sys.setdefaultencoding() is sometimes not accessible from command-line. This will make your encoding problems within Paster go away.

About Wolverine

If you are looking for IT consultant, let me know! karol at karoltomala dot REMOVE com Just remove the REMOVE word from the e-mail above!
This entry was posted in Coding, Programming, Pylons, Python and tagged , , , , , , . Bookmark the permalink.

2 Responses to Paster server encoding problems

  1. ciuchcia says:

    Did you tried str(data.encode(‘utf-8’))? For some reasons plain unicode can be usefull in other parts of the program ^^

  2. Wolverine says:

    It would require way too much effort from my side, because each Mako template which has any unicode variables passed from the controllers or models, should be escaped this way. I’ve tried using encode(‘utf-8’), but it didn’t work in some places.

    Somehow after updating Pylons and it’s dependency libraries, Mako templates stopped being properly encoded when fed into Paster server. That’s why I’ve decided to get rid of the problem with this hack.

Leave a Reply

Your email address will not be published. Required fields are marked *