[Checkins] SVN: z3c.pt/trunk/ Code updates to make package compatible with lxml 2.x.

Malthe Borch mborch at gmail.com
Tue May 20 17:58:19 EDT 2008


Log message for revision 86861:
  Code updates to make package compatible with lxml 2.x.

Changed:
  U   z3c.pt/trunk/README.txt
  U   z3c.pt/trunk/z3c/pt/clauses.py
  U   z3c.pt/trunk/z3c/pt/translation.py
  U   z3c.pt/trunk/z3c/pt/translation.txt
  U   z3c.pt/trunk/z3c/pt/utils.py

-=-
Modified: z3c.pt/trunk/README.txt
===================================================================
--- z3c.pt/trunk/README.txt	2008-05-20 21:36:18 UTC (rev 86860)
+++ z3c.pt/trunk/README.txt	2008-05-20 21:58:19 UTC (rev 86861)
@@ -5,7 +5,7 @@
 template language including i18n. It also provides a simple text
 template class that allows expression interpolation.
 
-Casual benchmarks pegs it 11x more performant than ``zope.pagetemplate``.
+Casual benchmarks pegs it 10x more performant than ``zope.pagetemplate``.
 
 In a nutshell:
 

Modified: z3c.pt/trunk/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/z3c/pt/clauses.py	2008-05-20 21:36:18 UTC (rev 86860)
+++ z3c.pt/trunk/z3c/pt/clauses.py	2008-05-20 21:58:19 UTC (rev 86861)
@@ -544,6 +544,7 @@
       >>> _repeat.begin(stream)
       >>> stream.write("r = repeat['i']")
       >>> stream.write("print (i, r.index, r.start, r.end, r.number(), r.odd(), r.even())")
+      >>> _repeat.end(stream)
       >>> exec stream.getvalue()
       (0, 0, True, False, 1, False, True)
       (1, 1, False, False, 2, True, False)
@@ -555,7 +556,7 @@
     A repeat over an empty set.
     
       >>> stream = CodeIO()
-      >>> _repeat = Repeat("j", pyexp("range(0).__iter__()"))
+      >>> _repeat = Repeat("j", pyexp("range(0)"))
       >>> _repeat.begin(stream)
       >>> _repeat.end(stream)
       >>> exec stream.getvalue()
@@ -578,17 +579,25 @@
         self.define.begin(stream)
 
         # initialize iterator
-        stream.write("repeat['%s'] = %s = %s.__iter__()" % (variable, iterator, iterator))
-
+        stream.write("%s = repeat.insert('%s', %s)" % (iterator, variable, iterator))
+        
         # loop
-        stream.write("while %s:" % iterator)
+        stream.write("try:")
         stream.indent()
+        stream.write("while True:")
+        stream.indent()
         stream.write("%s = %s.next()" % (variable, iterator))
         
     def end(self, stream):
         # cook before leaving loop
-        stream.cook()        
+        stream.cook()
+        
         stream.outdent()
+        stream.outdent()
+        stream.write("except StopIteration:")
+        stream.indent()
+        stream.write("pass")
+        stream.outdent()
         
         self.define.end(stream)
         self.assign.end(stream)

Modified: z3c.pt/trunk/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.py	2008-05-20 21:36:18 UTC (rev 86860)
+++ z3c.pt/trunk/z3c/pt/translation.py	2008-05-20 21:58:19 UTC (rev 86861)
@@ -366,8 +366,12 @@
 parser = lxml.etree.XMLParser()
 parser.setElementClassLookup(lookup)
 
-lookup.get_namespace('http://www.w3.org/1999/xhtml')[None] = Element
-lookup.get_namespace('http://xml.zope.org/namespaces/tal')[None] = TALElement
+try:
+    lookup.get_namespace('http://www.w3.org/1999/xhtml')[None] = Element
+    lookup.get_namespace('http://xml.zope.org/namespaces/tal')[None] = TALElement
+except AttributeError:
+    lxml.etree.Namespace('http://www.w3.org/1999/xhtml')[None] = Element
+    lxml.etree.Namespace('http://xml.zope.org/namespaces/tal')[None] = TALElement
 
 def translate_xml(body, *args, **kwargs):
     tree = lxml.etree.parse(StringIO(body), parser)

Modified: z3c.pt/trunk/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.txt	2008-05-20 21:36:18 UTC (rev 86860)
+++ z3c.pt/trunk/z3c/pt/translation.txt	2008-05-20 21:58:19 UTC (rev 86861)
@@ -275,14 +275,18 @@
   >>> render(body, translate_xml)
   Traceback (most recent call last):
     ...
-  XMLSyntaxError: line 1: Couldn't find end of Start Tag div line 1
+  XMLSyntaxError: Couldn't find end of Start Tag div line 1, line 1, column 11
 
 Missing namespace definition:
-    We expect the engine to render the attributes as static.
+    We expect the engine to raise an exception*
 
+*) This test only passes on lxml 2.x.
+
   >>> body = """\
   ... <div xmlns="http://www.w3.org/1999/xhtml" tal:content="'Hello World'" />
   ... """
 
   >>> print render(body, translate_xml)
-  <div content="'Hello World'" />
+  Traceback (most recent call last):
+    ...
+  XMLSyntaxError: Namespace prefix tal for content on div is not defined, line 1, column 23

Modified: z3c.pt/trunk/z3c/pt/utils.py
===================================================================
--- z3c.pt/trunk/z3c/pt/utils.py	2008-05-20 21:36:18 UTC (rev 86860)
+++ z3c.pt/trunk/z3c/pt/utils.py	2008-05-20 21:58:19 UTC (rev 86861)
@@ -42,8 +42,18 @@
         
     @property
     def index(self):
-        return self.length - len(self.iterator) - 1
-        
+        try:
+            length = len(self.iterator)
+        except TypeError:
+            length = self.iterator.__length_hint__()
+        except:
+            raise TypeError("Unable to determine length.")
+
+        try:
+            return self.length - length - 1
+        except TypeError:
+            return None
+            
     @property
     def start(self):
         return self.index == 0
@@ -62,19 +72,17 @@
         return not self.odd()
 
 class repeatdict(dict):
-    def __setitem__(self, key, iterator):
-        try:
-            length = len(iterator)
-        except TypeError:
-            length = None
-            
-        dict.__setitem__(self, key, (iterator, length))
+    def insert(self, key, iterable):
+        length = len(iterable)
+        iterator = iterable.__iter__()
+        self[key] = (iterator, length)
+        return iterator
         
     def __getitem__(self, key):
         value, length = dict.__getitem__(self, key)
 
         if not isinstance(value, repeatitem):
             value = repeatitem(value, length)
-            self.__setitem__(key, value)
-
+            self[key] = (value, length)
+            
         return value



More information about the Checkins mailing list