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/", line 437, in handle_one_request
  File "/usr/lib/python2.6/site-packages/paste/", line 290, in wsgi_execute
  File "/usr/lib/python2.6/site-packages/paste/", line 150, in wsgi_write_chunk
  File "/usr/lib64/python2.6/", 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/ (if you are using Pylons):

# Workaround stupid Paster encoding bugs
import sys


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 *