[PEAK] Error having non-ASCII characters in HTTP content

Yaroslav Samchuk yarcat at ank-sia.com
Fri Apr 29 11:13:43 EDT 2005


Hello,

An exception was raised while converting text to string
======
Apr 28 16:51:46 yarcat peak.web.app[1436]:   File 
"E:\Python24\Lib\site-packages\peak\web\templates.py", line 604, in 
handle_http
Apr 28 16:51:46 yarcat peak.web.app[1436]:     return '200 OK', h, 
[str(unicodeJoin(data))]  # XXX encoding
Apr 28 16:51:46 yarcat peak.web.app[1436]: UnicodeEncodeError: 'ascii' 
codec can't encode character u'\u0421' in position 2775: ordinal not in 
range(128)
======

We had locally patched `peak.web/templates.py` to avoid this error. 
Patch is attached to this mail. May be it could be useful.

-- 
Best regards,
Yaroslav
-------------- next part --------------
--- templates.py.bak	2005-04-14 11:40:15.187500000 +0300
+++ templates.py	2005-04-29 18:10:37.265625000 +0300
@@ -601,7 +601,7 @@
         h = []
         if self.content_type:
             h.append(('Content-type',self.content_type))
-        return '200 OK', h, [str(unicodeJoin(data))]    # XXX encoding
+        return '200 OK', h, [unicodeJoin(data).encode(self.content_charset)]
 
 
     def getObjectInstance(klass, context, refInfo, name, attrs=None):
@@ -617,6 +617,15 @@
         str(self.params.get('content-type'))
     )
 
+    def content_charset(self):
+        for param in (self.content_type or "").split(";")[1:]:
+            param = param.strip()
+            if param.startswith("charset="):
+                return param.split("=", 1)[1]
+        return "utf-8"
+
+    content_charset = binding.Make(content_charset)
+
     def layoutDOMlet(self,d,attrName):
 
         if attrName+'-layout' in self.params:


More information about the PEAK mailing list