tornado icon indicating copy to clipboard operation
tornado copied to clipboard

Broken Unicode handling in reverse_url function

Open callum opened this issue 14 years ago • 0 comments

  File "/tornado/web.py", line 1371, in reverse_url
    return self.named_handlers[name].reverse(*args)
  File "/tornado/web.py", line 1931, in reverse
    return self._path % tuple([str(a) for a in args])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

I'm using a temporary fix at the moment that looks like this:

class URLSpec(tornado.web.URLSpec):
    def reverse(self, *args):
        assert self._path is not None, \
            "Cannot reverse url regex " + self.regex.pattern
        assert len(args) == self._group_count, "required number of arguments "\
            "not found"
        if not len(args):
            return self._path
        return self._path % tuple([a.encode('utf-8') for a in args])

Replacing str(a) for a.encode('utf-8').

callum avatar Apr 10 '12 01:04 callum