[Checkins] SVN: z3c.pt/trunk/ Made the _out.write method directly available as _write in all scopes, so we avoid the method lookup call each time.

Hanno Schlichting plone at hannosch.info
Sat Jun 14 14:52:51 EDT 2008


Log message for revision 87402:
  Made the _out.write method directly available as _write in all scopes, so we avoid the method lookup call each time.
  

Changed:
  U   z3c.pt/trunk/docs/HISTORY.txt
  U   z3c.pt/trunk/src/z3c/pt/clauses.py
  U   z3c.pt/trunk/src/z3c/pt/generation.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py

-=-
Modified: z3c.pt/trunk/docs/HISTORY.txt
===================================================================
--- z3c.pt/trunk/docs/HISTORY.txt	2008-06-14 17:58:27 UTC (rev 87401)
+++ z3c.pt/trunk/docs/HISTORY.txt	2008-06-14 18:52:50 UTC (rev 87402)
@@ -4,6 +4,9 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Made the _out.write method directly available as _write in all scopes, so
+  we avoid the method lookup call each time.
+
 - Optimized 'is None' handling in Write clause.
 
 - Slightly refactored benchmark tests and added tests for the file variants.

Modified: z3c.pt/trunk/src/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/clauses.py	2008-06-14 17:58:27 UTC (rev 87401)
+++ z3c.pt/trunk/src/z3c/pt/clauses.py	2008-06-14 18:52:50 UTC (rev 87402)
@@ -298,6 +298,7 @@
     """
       >>> from z3c.pt.generation import CodeIO
       >>> from z3c.pt.testing import pyexp
+      >>> from StringIO import StringIO
       >>> from cgi import escape as _escape
       
     Unlimited scope:
@@ -318,9 +319,7 @@
 
     Finalized limited scope:
 
-      >>> stream = CodeIO()
-      >>> from StringIO import StringIO
-      >>> _out = StringIO()
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> true = Condition(pyexp("True"), [Write(pyexp("'Hello'"))])
       >>> false = Condition(pyexp("False"), [Write(pyexp("'Hallo'"))])
       >>> true.begin(stream)
@@ -333,9 +332,7 @@
 
     Open limited scope:
 
-      >>> stream = CodeIO()
-      >>> from StringIO import StringIO
-      >>> _out = StringIO()
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> true = Condition(pyexp("True"), [Tag('div')], finalize=False)
       >>> false = Condition(pyexp("False"), [Tag('span')], finalize=False)
       >>> true.begin(stream)
@@ -421,7 +418,7 @@
 
       Dynamic attribute:
       
-      >>> _out = StringIO(); stream = CodeIO()
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> tag = Tag('div', dict(alt=pyexp(repr('Hello World!'))))
       >>> tag.begin(stream)
       >>> stream.out('Hello Universe!')
@@ -432,7 +429,7 @@
 
       Self-closing tag:
       
-      >>> _out = StringIO(); stream = CodeIO()
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> tag = Tag('br', {}, True)
       >>> tag.begin(stream)
       >>> tag.end(stream)
@@ -442,7 +439,7 @@
 
       Unicode:
       
-      >>> _out = StringIO(); stream = CodeIO()
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> tag = Tag('div', dict(alt=pyexp(repr('La Peña'))))
       >>> tag.begin(stream)
       >>> stream.out('Hello Universe!')
@@ -509,7 +506,7 @@
             else:
                 stream.write("%s = str(%s)" % (temp, temp))
                 
-            stream.write("_out.write(' %s=\"' + _escape(%s, \"\\\"\"))" %
+            stream.write("_write(' %s=\"' + _escape(%s, \"\\\"\"))" %
                          (attribute, temp))
             stream.out('"')
             
@@ -605,14 +602,14 @@
 
 class Write(object):
     """
-    >>> from z3c.pt.generation import CodeIO; stream = CodeIO()
+    >>> from z3c.pt.generation import CodeIO
     >>> from z3c.pt.testing import pyexp
     >>> from StringIO import StringIO
     >>> from cgi import escape as _escape
 
     Basic write:
     
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(pyexp("'New York'"))
     >>> write.begin(stream)
     >>> write.end(stream)
@@ -622,8 +619,7 @@
 
     Try-except parts:
 
-    >>> stream = CodeIO()
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(pyexp("undefined | 'New Delhi'"))
     >>> write.begin(stream)
     >>> write.end(stream)
@@ -633,15 +629,13 @@
 
     Unicode:
 
-    >>> stream = CodeIO()
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(types.value("unicode('La Pe\xc3\xb1a', 'utf-8')"))
     >>> write.begin(stream)
     >>> write.end(stream)
     >>> exec stream.getvalue()
     >>> _out.getvalue() == 'La Pe\xc3\xb1a'
     True
-    
     """
 
     value = assign = None
@@ -668,22 +662,22 @@
         if unicode_required_flag:
             stream.write("if isinstance(_urf, unicode):")
             stream.indent()
-            stream.write("_out.write(_urf.encode('utf-8'))")
+            stream.write("_write(_urf.encode('utf-8'))")
             stream.outdent()
             stream.write("elif _urf is not None:")
             stream.indent()
             if self.structure:
-                stream.write("_out.write(str(_urf))")
+                stream.write("_write(str(_urf))")
             else:
-                stream.write("_out.write(_escape(str(_urf)))")
+                stream.write("_write(_escape(str(_urf)))")
             stream.outdent()
         else:
             stream.write("if _urf is not None:")
             stream.indent()
             if self.structure:
-                stream.write("_out.write(str(_urf))")
+                stream.write("_write(str(_urf))")
             else:
-                stream.write("_out.write(_escape(str(_urf)))")
+                stream.write("_write(_escape(str(_urf)))")
             stream.outdent()
 
     def end(self, stream):
@@ -698,8 +692,7 @@
 
     Basic write:
 
-    >>> stream = CodeIO()
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(types.value("'New York'"))
     >>> write.begin(stream)
     >>> write.end(stream)
@@ -709,8 +702,7 @@
 
     Unicode:
 
-    >>> stream = CodeIO()
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(types.value("unicode('La Pe\xc3\xb1a', 'utf-8')"))
     >>> write.begin(stream)
     >>> write.end(stream)
@@ -720,8 +712,7 @@
 
     Invalid:
 
-    >>> stream = CodeIO()
-    >>> _out = StringIO()
+    >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
     >>> write = Write(types.value("None"))
     >>> write.begin(stream)
     >>> write.end(stream)
@@ -739,15 +730,15 @@
             self.assign.begin(stream, temp)
             expr = temp
 
-        stream.write("_out.write(%s.encode('utf-8'))" % expr)
+        stream.write("_write(%s.encode('utf-8'))" % expr)
 
 class Out(object):
     """
-      >>> from z3c.pt.generation import CodeIO; stream = CodeIO()
+      >>> from z3c.pt.generation import CodeIO
       >>> from z3c.pt.testing import pyexp
       >>> from StringIO import StringIO
-      >>> _out = StringIO()
       
+      >>> _out = StringIO(); _write = _out.write; stream = CodeIO()
       >>> out = Out('Hello World!')
       >>> out.begin(stream)
       >>> out.end(stream)

Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-06-14 17:58:27 UTC (rev 87401)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-06-14 18:52:50 UTC (rev 87402)
@@ -13,8 +13,7 @@
 def render(%starget_language=None):
 \tglobal generation
 
-\t_out = generation.initialize_stream()
-    
+\t(_out, _write) = generation.initialize_stream()
 \t(_attributes, repeat) = generation.initialize_tal()
 \t(_domain, _translate) = generation.initialize_i18n()
 \t(_escape, _marker) = generation.initialize_helpers()
@@ -35,7 +34,8 @@
     return (cgi.escape, object())
 
 def initialize_stream():
-    return cStringIO.StringIO()
+    out = cStringIO.StringIO()
+    return (out, out.write)
 
 def initialize_traversal():
     return expressions.PathTranslation.traverse
@@ -46,7 +46,7 @@
         self.stream = CodeIO(indentation=1, indentation_string="\t")
 
         # initialize variable scope
-        self.stream.scope.append(set(params + ['_out']))
+        self.stream.scope.append(set(params + ['_out', '_write']))
 
     def __call__(self):
         # prepare template arguments
@@ -114,7 +114,7 @@
         if self.queue:
             queue = self.queue
             self.queue = ''
-            self.write("_out.write('%s')" %
+            self.write("_write('%s')" %
                        queue.replace('\n', '\\n').replace("'", "\\'"))
             
     def write(self, string):

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-06-14 17:58:27 UTC (rev 87401)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-06-14 18:52:50 UTC (rev 87402)
@@ -181,7 +181,7 @@
                     
                     subclauses = []
                     subclauses.append(clauses.Define(
-                        '_out', types.value('generation.initialize_stream()')))
+                        ('_out', '_write'), types.value('generation.initialize_stream()')))
                     subclauses.append(clauses.Group(element._clauses()))
                     subclauses.append(clauses.Assign(
                         types.value('_out.getvalue()'), "%s['%s']" % (mapping, name)))



More information about the Checkins mailing list