UnicodeEncodeError when encoded URL and traversal. Dispatch OK. #385

ghost opened this Issue Dec 28, 2011 · 3 comments


None yet

2 participants


When request URL is ascii both dispatch and traversal work but when URL is encoded dispatch works but traversal returns error UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0105' in position 1: ordinal not in range(256). for url: http://localhost:6543/traversal/a%C5%9B

Here's code I've used.
Pyramid version 1.3a2-py2.6

from pyramid.config import Configurator
from pyramid.response import Response
from paste.httpserver import serve

class Root(object):
    def __init__(self, request):

    def __getitem__(self, key):
        return Resource(key)

class Resource(dict):
    def __init__(self, v):
        self.val = v

def the_view(context, request):
    return Response(request.path_info)

if __name__ == '__main__':
    config = Configurator()
    config.add_route('the_route', 'dispatch/{foo}', request_method='GET')
    config.add_view(the_view, route_name='the_route')

    config.add_route('trav_route', 'traversal/{traverse:.*}', factory=Root, request_method='GET')
    config.add_view(the_view, route_name='trav_route')

    serve(config.make_wsgi_app(), port='6543')

Similar error when using Pyramid 1.2.5

UnicodeEncodeError: 'ascii' codec can't encode character u'\u015b' in position 1: ordinal not in range(128)

In both cases argument path is already unicode in traversal_path (v1.2.5) and traversal_path_info(v1.3.3)

Pylons Project member

FWIW, this is behavior that I'll try to anticipate, but I think you actually want this:

    config.add_route('trav_route', 'traversal/*traverse',
                     factory=Root, request_method='GET')

Instead of:

    config.add_route('trav_route', 'traversal/{traverse:.*}',
                     factory=Root, request_method='GET')

The former creates a tuple in matchdict with the traversal path, the latter a Unicode object. I'm not entirely sure what the right thing to do to fix the latter case is yet, but hopefully this can get you along in the meantime.

Pylons Project member

The original issue is fixed on the 1.3 branch by:


And on the 1.2 branch by:


These will eventually be released as 1.3a4 and 1.2.6 respectively.

@mcdonc mcdonc closed this Jan 5, 2012
@rbu rbu pushed a commit to rbu/pyramid that referenced this issue Mar 3, 2014
@mcdonc mcdonc - Using a dynamic segment named ``traverse`` in a route pattern like …

    config.add_route('trav_route', 'traversal/{traverse:.*}')

  Would cause a ``UnicodeDecodeError`` when the route was matched and the
  matched portion of the URL contained any high-order characters.  See also
  Pylons#385 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment