[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/src/zope/ Merged from trunk 25974 and 25984:

Jim Fulton jim at zope.com
Fri Jul 2 17:00:18 EDT 2004


Log message for revision 26075:
Merged from trunk 25974 and 25984:

Moved _error_start to module rather than class scope, so as not to
mistakingly thing that it's part of a public or subclassing interface.

Removed the unused _engine_name.

Redefined pt_getEngineContext as a regular method.

Resolved an XXX

Inlined the barely use html method.


Fixed bug publishing bound page templates

When Fred and I added an interface declaration to
zope.pagetemplate.pagetemplate.PageTemplate, we unwittingly broke
BoundPageTemplates. This was due to the fact that BoundPageTemplates
were overagressive about exposing underlying template attributes, 
including interface declarations.  

Changed BoundPageTemplates to only expose needed attributes, macros
amd filename.

Added a test for the view that exposed the error.



-=-
Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py	2004-07-02 20:54:55 UTC (rev 26074)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/engine.py	2004-07-02 21:00:18 UTC (rev 26075)
@@ -182,7 +182,6 @@
             context.request = namespace['request']
 
         # Put context into context so path traversal can find it
-        # XXX: Change to container once the renaming has been done!
         if 'context' in namespace:
             context.context = namespace['context']
 
@@ -205,8 +204,5 @@
 
 class AppPT(object):
 
-    # Use our special engine
-    pt_getEngineContext = Engine.getContext
-
     def pt_getEngine(self):
         return Engine

Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/tests/test_boundpagetemplate.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/tests/test_boundpagetemplate.py	2004-07-02 20:54:55 UTC (rev 26074)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/tests/test_boundpagetemplate.py	2004-07-02 21:00:18 UTC (rev 26075)
@@ -23,11 +23,9 @@
 
         from zope.app.pagetemplate.tests.sample import C
 
-        self.assertRaises(AttributeError, setattr, C.index, 'foo', 1)
-        self.assertRaises(AttributeError, setattr, C().index, 'foo', 1)
-
         C.index.im_func.foo = 1
-        self.assertEqual(C.index.foo, 1)
+        self.assertEqual(C.index.macros, C.index.im_func.macros)
+        self.assertEqual(C.index.filename, C.index.im_func.filename)
 
 
 

Modified: Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/viewpagetemplatefile.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/viewpagetemplatefile.py	2004-07-02 20:54:55 UTC (rev 26074)
+++ Zope3/branches/ZopeX3-3.0/src/zope/app/pagetemplate/viewpagetemplatefile.py	2004-07-02 21:00:18 UTC (rev 26075)
@@ -47,7 +47,7 @@
             instance=instance, args=args, options=keywords)
         return self.pt_render(namespace)
 
-    def __get__(self, instance, type=None):
+    def __get__(self, instance, type):
         return BoundPageTemplate(self, instance)
 
 class ViewMapper:
@@ -64,14 +64,19 @@
         object.__setattr__(self, 'im_func', pt)
         object.__setattr__(self, 'im_self', ob)
 
-    def __call__(self, **kw):
-        return self.im_func(self.im_self, **kw)
+    macros = property(lambda self: self.im_func.macros)
+    filename = property(lambda self: self.im_func.filename)
 
-    def __getattr__(self, name):
-        return getattr(self.im_func, name)
+    def __call__(self, *args, **kw):
+        if self.im_self is None:
+            im_self, args = args[0], args[1:]
+        else:
+            im_self = self.im_self
+        return self.im_func(im_self, *args, **kw)
 
     def __setattr__(self, name, v):
         raise AttributeError("Can't set attribute", name)
 
     def __repr__(self):
         return "<BoundPageTemplateFile of %r>" % self.im_self
+

Modified: Zope3/branches/ZopeX3-3.0/src/zope/pagetemplate/pagetemplate.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/pagetemplate/pagetemplate.py	2004-07-02 20:54:55 UTC (rev 26074)
+++ Zope3/branches/ZopeX3-3.0/src/zope/pagetemplate/pagetemplate.py	2004-07-02 21:00:18 UTC (rev 26075)
@@ -28,7 +28,10 @@
 # Don't use cStringIO here!  It's not unicode aware.
 from StringIO import StringIO
 
+import zope.pagetemplate.interfaces
+import zope.interface
 
+
 class MacroCollection:
     def __get__(self, parent, type=None):
         parent._cook_check()
@@ -36,6 +39,7 @@
 
 
 _default_options = {}
+_error_start = '<!-- Page Template Diagnostics'
 
 class PageTemplate:
     """Page Templates using TAL, TALES, and METAL.
@@ -61,6 +65,10 @@
         passed to the TALES expression engine, then calls pt_render()
         to perform the rendering.
     """
+
+    zope.interface.implements(
+        zope.pagetemplate.interfaces.IPageTemplateSubclassing)
+
     content_type = 'text/html'
     expand = 1
     _v_errors = ()
@@ -69,8 +77,6 @@
     _v_macros = None
     _v_cooked = 0
     _text = ''
-    _engine_name = 'default'
-    _error_start = '<!-- Page Template Diagnostics'
 
     macros = MacroCollection()
 
@@ -94,7 +100,8 @@
     def __call__(self, *args, **kwargs):
         return self.pt_render(self.pt_getContext(args, kwargs))
 
-    pt_getEngineContext = Engine.getContext
+    def pt_getEngineContext(self, namespace):
+        return self.pt_getEngine().getContext(namespace)
 
     def pt_getEngine(self):
         return Engine
@@ -133,13 +140,15 @@
         # which case we have already unicode. 
         assert isinstance(text, (str, unicode))
 
-        if text.startswith(self._error_start):
+        if text.startswith(_error_start):
             errend = text.find('-->')
             if errend >= 0:
                 text = text[errend + 4:]
         if self._text != text:
             self._text = text
-        # XXX can this be done only if we changed self._text?
+
+        # we always want to cook on an update, even if the source
+        # is the same.  Possibly because the content-type might have changed.
         self._cook()
 
     def read(self):
@@ -154,10 +163,10 @@
                 return self.pt_render({}, source=1)
             except:
                 return ('%s\n Macro expansion failed\n %s\n-->\n%s' %
-                        (self._error_start, "%s: %s" % sys.exc_info()[:2],
+                        (_error_start, "%s: %s" % sys.exc_info()[:2],
                          self._text) )
 
-        return ('%s\n %s\n-->\n%s' % (self._error_start,
+        return ('%s\n %s\n-->\n%s' % (_error_start,
                                       '\n'.join(self._v_errors),
                                       self._text))
 
@@ -176,7 +185,7 @@
         """
         engine = self.pt_getEngine()
         source_file = self.pt_source_file()
-        if self.html():
+        if self.content_type == 'text/html':
             gen = TALGenerator(engine, xml=0, source_file=source_file)
             parser = HTMLTALParser(gen)
         else:
@@ -193,12 +202,7 @@
         self._v_warnings = parser.getWarnings()
         self._v_cooked = 1
 
-    def html(self):
-        if not hasattr(self, 'is_html'):
-            return self.content_type == 'text/html'
-        return self.is_html
 
-
 class TemplateUsage:
     def __init__(self, value):
         if not isinstance(value, unicode):



More information about the Zope3-Checkins mailing list