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

Paul Reznicek maillists at ivsn.com
Thu Jan 27 15:49:29 MST 2005


Below suggested patches to omit broken output, if unicode
is returned instead of string.
Problem occurred during localization with gettext, see this
sample code:
-------------------------------------------------------------------
import gettext

from twisted.internet import reactor
from twisted.protocols import http
from nevow import inevow, rend, loaders, tags, appserver, static, guard

class Index(rend.Page):
     addSlash = True
     child_css = static.File('static/css')
     child_img = static.File('static/img')
     docFactory = loaders.xmlfile('static/index.html')

     def renderHTTP(self, ctx):
         request = inevow.IRequest(ctx)
         # Dynamic LOCALIZATION ===============================================
         # inspired by http://www.ospace.net/index.en.html
         language = request.getHeader('accept-language').split(',')[0][:2]
         try:
             tran = gettext.translation('PROJECT', 'static/locale', languages = [language])
         except IOError:
             print 'Cannot find catalog for "', language, '", installing null translations'
             tran = gettext.NullTranslations()
         tran.install(unicode = 1)
         # ====================================================================
         # got from: nevow/examples/http_auth.py
         username, password = request.getUser(), request.getPassword()
         if (username, password) == ('', ''):
             request.setHeader('WWW-Authenticate', 'Basic realm="Whatever"')
             request.setResponseCode(http.UNAUTHORIZED)
             return _("Authentication required.")    # THIS RETURN UNICODE
         ## They provided a username and password, so let's let them in! hurray
         self.data_username, self.data_password = username, password
         return rend.Page.renderHTTP(self, ctx)

site = appserver.NevowSite(Index())
reactor.listenTCP(8080, site)
reactor.run()
-------------------------------------------------------------------
Paul Reznicek

Necessary patch:
===================================================================
--- nevow/appserver.py (Revision 1126)
+++ nevow/appserver.py (local)
@@ -167,8 +167,8 @@
          self.deferred.callback("")

      def _cbFinishRender(self, html, ctx):
-        if isinstance(html, str):
-            self.write(html)
+        if isinstance(html, basestring):
+            self.write(html.encode('utf8'))
              server.Request.finish(self)
          elif html is errorMarker:
              ## Error webpage has already been rendered and finish called


I believe, the wsgi.py can have similar problem:
===================================================================
--- nevow/wsgi.py (Revision 1126)
+++ nevow/wsgi.py (local)
@@ -84,8 +84,8 @@
              p = rend.FourOhFour()

          result = p.renderHTTP(pctx)
-        if isinstance(result, str):
-            request.write(result)
+        if isinstance(result, basestring):
+            request.write(result.encode('utf8'))
          else:
              ## Exhaust the generator
              list(result)





More information about the Twisted-web mailing list