[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/ Make the following test pass on machines which are configured with

Chris McDonough chrism at plope.com
Mon Aug 4 06:38:14 EDT 2008


Log message for revision 89340:
  Make the following test pass on machines which are configured with
  ascii default encoding:
  
  import z3c.pt
  from zope.configuration import xmlconfig
  xmlconfig.file('configure.zcml', z3c.pt)
  pt = """<html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:tal="http://xml.zope.org/namespaces/tal">
  <span>
  normal text
  </span>
  <div>
  &#169;
  </div>
  &#169;
  </html>"""
  from z3c.pt.pagetemplate import PageTemplate
  t = PageTemplate(pt)
  print t.render()
  
  Previously, on systems configured with an ascii default encoding, this would
  fail with a UnicodeDecodeError.
  
  I have made sure the tests still pass on a system configured with utf-8 default
  encoding.
  
  A previous attempt at a fix for this was put into the chrism-gen-unicode-fix
  branch, but that was not the right fix (it was too coarse).
  
  Here we cause CodeIO to keep a string queue rather than a unicode queue, and
  we encode "text" and "tail" literals to utf-8 before making clauses
  out of them.  CodeIO should always hold a string in its queue from
  here on in.
  
  This is required because the libxml2 (lxml?) parser returns us
  unicode when it encounters entities in the document, while everything else
  it encounters seems to come to us as strings.
  
  This change has no meaningful effect on benchmark numbers.
  
  This is a ridiculously long checkin message.
  
  

Changed:
  U   z3c.pt/trunk/src/z3c/pt/generation.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py

-=-
Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-08-04 10:34:40 UTC (rev 89339)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-08-04 10:38:13 UTC (rev 89340)
@@ -110,7 +110,7 @@
         BufferIO.__init__(self)
         self.indentation = indentation
         self.indentation_string = indentation_string
-        self.queue = u''
+        self.queue = ''
         self.scope = [set()]
         self.annotations = {}
         
@@ -151,8 +151,8 @@
                        queue.replace('\n', '\\n').replace("'", "\\'"))
 
     def write(self, string):
-        if isinstance(string, str):
-            string = string.decode('utf-8')
+        if isinstance(string, unicode):
+            string = string.encode('utf-8')
 
         self.l_counter += len(string.split('\n'))-1
 

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-04 10:34:40 UTC (rev 89339)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-04 10:38:13 UTC (rev 89340)
@@ -25,7 +25,9 @@
 
     return property(get, set)
 
+
 class Element(lxml.etree.ElementBase):
+
     def begin(self, stream):
         stream.scope.append(set())
         stream.begin(self._clauses())
@@ -135,7 +137,7 @@
 
         # tag tail (deferred)
         if self.tail:
-            _.append(clauses.Out(self.tail, defer=True))
+            _.append(clauses.Out(self.tail.encode('utf-8'), defer=True))
 
         # compute dynamic flag
         dynamic = (self.replace or
@@ -158,7 +160,7 @@
 
         # tag text (if we're not replacing tag body)
         if self.text and not dynamic:
-            _.append(clauses.Out(self.text))
+            _.append(clauses.Out(self.text.encode('utf-8')))
 
         # dynamic content and content translation
         replace = self.replace
@@ -221,7 +223,7 @@
 
                 subclauses = []
                 if self.text:
-                    subclauses.append(clauses.Out(self.text))
+                    subclauses.append(clauses.Out(self.text.encode('utf-8')))
                 for element in self:
                     name = element.i18n_name
                     if name:



More information about the Checkins mailing list