Fred Drake wrote:
On 7/13/05, Dmitry Vasiliev <[EMAIL PROTECTED]> wrote:

Ok. But default 'ascii' encoding seems like a bug for me and I want to change
it to 'utf-8' before the release.


Not using the right encoding for the input would indeed be a bug.  Do
you have a reason to think it's handled incorrectly in XML mode? Expat is allowed to deal with the encoding on it's own; nothing is
done to override the use of the encoding from the XML declaration.

Please file a bug in the collector with a sample input if that's the case.

No. In XML mode input encoding is always handled correctly and we get an unicode PT content. But in HTML mode no encoding/decoding operations is performed on input so we get an str PT content and implicitly default PT input encoding is 'ascii'. I want to apply the attached patch so default PT input encoding explicitly will be 'utf-8'.

--
Dmitry Vasiliev (dima at hlabs.spb.ru)
    http://hlabs.spb.ru
Index: src/zope/pagetemplate/pagetemplatefile.py
===================================================================
--- src/zope/pagetemplate/pagetemplatefile.py	(revision 33293)
+++ src/zope/pagetemplate/pagetemplatefile.py	(working copy)
@@ -22,6 +22,9 @@
 
 from zope.pagetemplate.pagetemplate import PageTemplate
 
+
+DEFAULT_ENCODING = "utf-8"
+
 def package_home(gdict):
     filename = gdict["__file__"]
     return os.path.dirname(filename)
@@ -31,11 +34,12 @@
 
     _v_last_read = 0
 
-    def __init__(self, filename, _prefix=None):
+    def __init__(self, filename, _prefix=None, encoding=DEFAULT_ENCODING):
         path = self.get_path_from_prefix(_prefix)
         self.filename = os.path.join(path, filename)
         if not os.path.isfile(self.filename):
             raise ValueError("No such file", self.filename)
+        self.encoding = encoding
 
     def get_path_from_prefix(self, _prefix):
         if isinstance(_prefix, str):
@@ -63,12 +67,14 @@
             f.close()
             raise
         t = sniff_type(text)
-        if t != "text/xml":
+        if t == "text/xml":
+            text += f.read()
+        else:
             # For HTML, we really want the file read in text mode:
             f.close()
             f = open(self.filename)
-            text = ''
-        text += f.read()
+            text = f.read()
+            text = unicode(text, self.encoding)
         f.close()
         self.pt_edit(text, t)
         self._cook()
_______________________________________________
Zope3-dev mailing list
Zope3-dev@zope.org
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to