[Zope3-checkins] CVS: Zope3/src/zope/security - checker.py:1.16

Guido van Rossum guido@python.org
Mon, 21 Apr 2003 17:04:37 -0400


Update of /cvs-repository/Zope3/src/zope/security
In directory cvs.zope.org:/tmp/cvs-serv18953

Modified Files:
	checker.py 
Log Message:
Add a __traceback_supplement__ when a permission check fails with the
class module+name, to help tracking down permission errors.


=== Zope3/src/zope/security/checker.py 1.15 => 1.16 ===
--- Zope3/src/zope/security/checker.py:1.15	Wed Apr 16 15:21:39 2003
+++ Zope3/src/zope/security/checker.py	Mon Apr 21 17:04:37 2003
@@ -25,6 +25,7 @@
 from zope.exceptions \
      import Unauthorized, ForbiddenAttribute, Forbidden, DuplicationError
 from zope.interface.interfaces import IInterface
+from zope.interface.implements import flattenInterfaces
 from zope.interface import Interface
 from zope.security._proxy import _Proxy as Proxy
 from zope.security.interfaces import ISecurityProxyFactory
@@ -131,11 +132,13 @@
             else:
                 if WATCH_CHECKERS:
                     print >> sys.stderr, 'Unauthorized.'
+                __traceback_supplement__ = (TracebackSupplement, object)
                 raise Unauthorized(name=name)
 
         if WATCH_CHECKERS:
             print >> sys.stderr, 'Forbidden.'
 
+        __traceback_supplement__ = (TracebackSupplement, object)
         raise ForbiddenAttribute(name)
 
     def check(self, object, name):
@@ -159,6 +162,7 @@
             else:
                 if WATCH_CHECKERS:
                     print >> sys.stderr, 'Unauthorized.'
+                __traceback_supplement__ = (TracebackSupplement, object)
                 raise Unauthorized(name=name)
         elif name in _always_available:
             if WATCH_CHECKERS:
@@ -168,6 +172,7 @@
         if WATCH_CHECKERS:
             print >> sys.stderr, 'Forbidden.'
 
+        __traceback_supplement__ = (TracebackSupplement, object)
         raise ForbiddenAttribute(name)
 
     def proxy(self, value):
@@ -181,6 +186,29 @@
                 return value
 
         return Proxy(value, checker)
+
+# Helper class for __traceback_supplement__
+class TracebackSupplement:
+
+    def __init__(self, obj):
+        self.obj = obj
+
+    def getInfo(self):
+        result = []
+        try:
+            cls = self.obj.__class__
+        except:
+            cls = type(self.obj)
+        try:
+            if hasattr(cls, "__module__"):
+                s = "%s.%s" % (cls.__module__, cls.__name__)
+            else:
+                s = str(cls.__name__)
+            result.append("   - class: " + s)
+        except:
+            pass
+        return "\n".join(result)
+
 
 # Marker for public attributes