[ZPT] CVS: Zope/lib/python/Products/PageTemplates - TALES.py:1.28.2.3

Shane Hathaway shane@cvs.zope.org
Mon, 4 Mar 2002 18:21:38 -0500


Update of /cvs-repository/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv21891

Modified Files:
      Tag: shane-better-tracebacks-branch
	TALES.py 
Log Message:
Accept source file events and reveal them via a traceback supplement.


=== Zope/lib/python/Products/PageTemplates/TALES.py 1.28.2.2 => 1.28.2.3 ===
     _context_class = SafeMapping
     position = (None, None)
+    source_file = None
 
     def __init__(self, engine, contexts):
         self._engine = engine
@@ -214,7 +215,7 @@
         if isinstance(expression, StringType):
             expression = self._engine.compile(expression)
         __traceback_supplement__ = (
-            TALESTracebackSupplement, self.position, expression)
+            TALESTracebackSupplement, self, expression)
         v = expression(self)
         return v
 
@@ -244,24 +245,41 @@
     def getDefault(self):
         return Default
 
+    def setSourceFile(self, source_file):
+        self.source_file = source_file
+
     def setPosition(self, position):
         self.position = position
 
 
 class TALESTracebackSupplement:
     """Implementation of Products.ErrorReporter.ITracebackSupplement"""
-    def __init__(self, position, expression):
-        self.position = position
+    def __init__(self, context, expression):
+        self.context = context
         self.expression = expression
 
     def getManageableObject(self):
+        source_file = self.context.source_file
+        if (isinstance(source_file, StringType) and
+            source_file.startswith('traversal:')):
+            p = source_file[10:]
+            # XXX There should be a better way to find the Zope app root.
+            root = self.context.contexts.get('root', None)
+            if root is not None:
+                try:
+                    object = root.unrestrictedTraverse(p)
+                except:
+                    # Hmm, it didn't work out.
+                    return None
+                else:
+                    return object
         return None
 
     def getLine(self):
-        return self.position[0]
+        return self.context.position[0]
 
     def getColumn(self):
-        return self.position[1]
+        return self.context.position[1]
 
     def getExpression(self):
         return repr(self.expression)