[Checkins] SVN: zope.pagetemplate/trunk/ Fixed issue where a template would not have a ``_v_macros`` attribute.

Malthe Borch mborch at gmail.com
Tue Sep 13 06:15:40 EST 2011


Log message for revision 122788:
  Fixed issue where a template would not have a ``_v_macros`` attribute.
  
  This volatile attribute is relied upon by some legacy code such as the
  Zope 2 product ``PageTemplates``.
  
  In this changeset, the interface is changed to match that of the TAL
  parser's ``getCode`` method which returns a tuple ``(program,
  macros)``. These are in turn assigned to the volatile attributes
  ``_v_program`` and ``_v_macros``.
  

Changed:
  U   zope.pagetemplate/trunk/CHANGES.txt
  U   zope.pagetemplate/trunk/src/zope/pagetemplate/interfaces.py
  U   zope.pagetemplate/trunk/src/zope/pagetemplate/pagetemplate.py
  U   zope.pagetemplate/trunk/src/zope/pagetemplate/tests/test_basictemplate.py

-=-
Modified: zope.pagetemplate/trunk/CHANGES.txt
===================================================================
--- zope.pagetemplate/trunk/CHANGES.txt	2011-09-13 10:14:26 UTC (rev 122787)
+++ zope.pagetemplate/trunk/CHANGES.txt	2011-09-13 11:15:39 UTC (rev 122788)
@@ -5,7 +5,13 @@
 3.6.2 (unreleased)
 ------------------
 
+- Change interface for engine and program such that the return type of
+  the ``cook`` method is a tuple ``(program, macros)``. This follows
+  the interface for the TAL parser's ``getCode`` method.
 
+  This fixes a legacy compatibility issue where code would expect an
+  ``_v_macros`` volatile attribute which was missing.
+
 3.6.1 (2011-08-23)
 ------------------
 

Modified: zope.pagetemplate/trunk/src/zope/pagetemplate/interfaces.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/interfaces.py	2011-09-13 10:14:26 UTC (rev 122787)
+++ zope.pagetemplate/trunk/src/zope/pagetemplate/interfaces.py	2011-09-13 11:15:39 UTC (rev 122788)
@@ -113,23 +113,22 @@
     """
 
     def cook(source_file, text, engine, content_type):
-        """Parse text and return prepared template program.
+        """Parse text and return prepared template program and macros.
 
         Note that while ``source_file`` is provided to name the source
         of the input ``text``, it should not be relied on to be an
         actual filename (it may be an application-specific, virtual
         path).
+
+        The return type is a tuple ``(program, macros)``.
         """
 
 
 class IPageTemplateProgram(Interface):
     """Cooked template program."""
 
-    macros = Attribute(
-        "Template macros.")
-
     def __call__(
-        context, debug=0, wrap=60, metal=1, tal=1, showtal=-1,
+        context, macros, debug=0, wrap=60, metal=1, tal=1, showtal=-1,
         strictinsert=1, stackLimit=100, i18nInterpolate=1,
         sourceAnnotations=0):
         """Render template in the provided template ``context``.

Modified: zope.pagetemplate/trunk/src/zope/pagetemplate/pagetemplate.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/pagetemplate.py	2011-09-13 10:14:26 UTC (rev 122787)
+++ zope.pagetemplate/trunk/src/zope/pagetemplate/pagetemplate.py	2011-09-13 11:15:39 UTC (rev 122788)
@@ -78,12 +78,13 @@
     expand = 1
     _v_errors = ()
     _v_cooked = 0
+    _v_macros = None
     _v_program = None
     _text = ''
 
     def macros(self):
         self._cook_check()
-        return self._v_program.macros
+        return self._v_macros
 
     macros = property(macros)
 
@@ -127,7 +128,7 @@
         context = self.pt_getEngineContext(namespace)
 
         return self._v_program(
-            context, tal=not source, showtal=showtal,
+            context, self._v_macros, tal=not source, showtal=showtal,
             strictinsert=0, sourceAnnotations=sourceAnnotations
             )
 
@@ -204,7 +205,7 @@
             engine = queryUtility(
                 IPageTemplateEngine, default=PageTemplateEngine
                 )
-            self._v_program = engine.cook(
+            self._v_program, self._v_macros = engine.cook(
                 source_file, self._text, pt_engine, self.content_type)
         except:
             etype, e = sys.exc_info()[:2]
@@ -227,14 +228,13 @@
     implements(IPageTemplateProgram)
     classProvides(IPageTemplateEngine)
 
-    def __init__(self, program, macros):
-        self.macros = macros
-        self._program = program
+    def __init__(self, program):
+        self.program = program
 
-    def __call__(self, context, **options):
+    def __call__(self, context, macros, **options):
         output = StringIO(u'')
         interpreter = TALInterpreter(
-            self._program, self.macros, context,
+            self.program, macros, context,
             stream=output, **options
             )
         interpreter()
@@ -252,7 +252,7 @@
         parser.parseString(text)
         program, macros = parser.getCode()
 
-        return cls(program, macros)
+        return cls(program), macros
 
 
 class PageTemplateTracebackSupplement(object):

Modified: zope.pagetemplate/trunk/src/zope/pagetemplate/tests/test_basictemplate.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/tests/test_basictemplate.py	2011-09-13 10:14:26 UTC (rev 122787)
+++ zope.pagetemplate/trunk/src/zope/pagetemplate/tests/test_basictemplate.py	2011-09-13 11:15:39 UTC (rev 122788)
@@ -93,14 +93,19 @@
                 return self.args, args, kwargs
 
         class DummyEngine(object):
-            cook = DummyProgram
+            @staticmethod
+            def cook(*args):
+                return DummyProgram(*args), "macros"
 
         provideUtility(DummyEngine, IPageTemplateEngine)
         self.t._cook()
 
+        self.assertTrue(isinstance(self.t._v_program, DummyProgram))
+        self.assertEqual(self.t._v_macros, "macros")
+
         # "Render" and unpack arguments passed for verification
         ((cls, source_file, text, engine, content_type),
-         (program, context),
+         (program, context, macros),
          options) = \
          self.t.pt_render({})
 



More information about the checkins mailing list