1. Ben Bangert
  2. dozer
  3. Pull requests

Pull requests

#3 Merged

Fix for unicode problems.

  1. Mitchell Peabody

This fix forces the template function to consistently return unicode strings instead of unicode and str strings. Views that use the template function can thus consistently set the Response.text property without type error.

Background pip installs WebOb 1.2.3 as a supporting package for Dozer. So, I was using WebOb 1.2.3 when I sent you the pull request. It also appears that there are other places in the source where this switch from Response.body to Response.text is needed (the tree function for example). Looking a bit closer at some of the WebOb code, what's going on is the the default behavior for Response.init is to set the content type to utf-8. When the template (in leak.py) function returns, the type is sometimes unicode and sometimes str. You can verify this by comparing the type returned from applying the template function to graphs.html (Dozer.index) and the type return from applying the template function to trace.html (Dozer.trace).

The set method for Response.body detects if the content-type is binary or not and if it isn't, checks what the type the new value is and raises the TypeError if it's of a "text_type" which is defined in compat.py to be unicode for python versions < 3.

template was returning both str and unicode responses. When it's an str return, then setting Response.body works, but not Response.text. When it's a unicode return, then setting Response.text works, but not Response.body. So either there needs to be consistency in the return of the template function, a check needs to be made to set the appropriate response field, or a conversion to unicode when Response.text is set.

Comments (1)