[Twisted-web] Nevow error with unicode localization in appserver.py

James Y Knight foom at fuhm.net
Fri Jan 28 09:15:35 MST 2005


On Jan 27, 2005, at 5:49 PM, Paul Reznicek wrote:
> Below suggested patches to omit broken output, if unicode
> is returned instead of string.
> Problem occurred during localization with gettext, see this
> sample code:

> Necessary patch:
> ===================================================================
> --- appserver.py (Revision 1126)
> +++ appserver.py (local)
> @@ -170,6 +170,9 @@
>          if isinstance(html, str):
>              self.write(html)
>              server.Request.finish(self)
> +        elif isinstance(html, unicode):
> +            self.write(html.encode('utf8'))
> +            server.Request.finish(self)
>          elif html is errorMarker:
>              ## Error webpage has already been rendered and finish 
> called
>              pass

No. HTTP talks bytes, not characters. If you use the nevow rendering 
stuff, you get automatic encoding of characters into bytes via the 
UTF-8 encoding, but if you're going to override renderHTTP and return 
your own data, it should be a byte string.

appserver is really part of the HTTP server, not nevow (even though it 
is in the nevow package at the moment), and thus ought not know or care 
anything about what kind of data you are sending. It could be html in 
UTF-8, html in ISO8859-1, jpeg images, anything.

James




More information about the Twisted-web mailing list