[Zope-Checkins] SVN: Zope/trunk/ - Catch AttributeErrors and KeyErrors raised from

Sidnei da Silva sidnei at awkly.org
Wed Nov 3 12:54:35 EST 2004


Log message for revision 28329:
  
        - Catch AttributeErrors and KeyErrors raised from
          __bobo_traverse__ and convert them to NotFound. In debug mode
          a more verbose error message is issued, the same way it's done
          on attribute/item traversal.
  
        - Merged /Zope/branches/dc-bobo_traverse-branch 27744:28328
  

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/ZPublisher/BaseRequest.py
  U   Zope/trunk/lib/python/ZPublisher/tests/testBaseRequest.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2004-11-03 16:54:12 UTC (rev 28328)
+++ Zope/trunk/doc/CHANGES.txt	2004-11-03 17:54:35 UTC (rev 28329)
@@ -64,6 +64,11 @@
       - If a PROPPATCH element value contains only a CDATA section,
         store the CDATA contents only.
 
+      - Catch AttributeErrors and KeyErrors raised from
+        __bobo_traverse__ and convert them to NotFound. In debug mode
+        a more verbose error message is issued, the same way it's done
+        on attribute/item traversal.
+
   Zope 2.8a1
 
 

Modified: Zope/trunk/lib/python/ZPublisher/BaseRequest.py
===================================================================
--- Zope/trunk/lib/python/ZPublisher/BaseRequest.py	2004-11-03 16:54:12 UTC (rev 28328)
+++ Zope/trunk/lib/python/ZPublisher/BaseRequest.py	2004-11-03 17:54:35 UTC (rev 28329)
@@ -309,14 +309,20 @@
                     else: return response.forbiddenError(entry_name)
 
                 if hasattr(object,'__bobo_traverse__'):
-                    subobject=object.__bobo_traverse__(request,entry_name)
-                    if type(subobject) is type(()) and len(subobject) > 1:
-                        # Add additional parents into the path
-                        parents[-1:] = list(subobject[:-1])
-                        object, subobject = subobject[-2:]
+                    try:
+                        subobject=object.__bobo_traverse__(request,entry_name)
+                        if type(subobject) is type(()) and len(subobject) > 1:
+                            # Add additional parents into the path
+                            parents[-1:] = list(subobject[:-1])
+                            object, subobject = subobject[-2:]
+                    except (AttributeError, KeyError):
+                        if debug_mode:
+                            return response.debugError(
+                                "Cannot locate object at: %s" % URL)
+                        else:
+                            return response.notFoundError(URL)
                 else:
                     try:
-
                         # Note - no_acquire_flag is necessary to support
                         # things like DAV.  We have to make sure
                         # that the target object is not acquired
@@ -362,7 +368,7 @@
                 # certain mutable types (dicts, lists) to become publishable
                 # when they shouldn't be. The following check makes sure that
                 # the right thing happens in both 2.2.2+ and earlier versions.
-                
+
                 if not typeCheck(subobject):
                     return response.debugError(
                         "The object at %s is not publishable." % URL

Modified: Zope/trunk/lib/python/ZPublisher/tests/testBaseRequest.py
===================================================================
--- Zope/trunk/lib/python/ZPublisher/tests/testBaseRequest.py	2004-11-03 16:54:12 UTC (rev 28328)
+++ Zope/trunk/lib/python/ZPublisher/tests/testBaseRequest.py	2004-11-03 17:54:35 UTC (rev 28329)
@@ -43,6 +43,13 @@
         REQUEST._hacked_path=1
 
 
+class DummyObjectWithBBT(DummyObjectBasic):
+    """ Dummy class with docstring.
+    """
+
+    def __bobo_traverse__(self, REQUEST, name):
+        raise AttributeError, name
+
 class DummyObjectWithBD(DummyObjectBasic):
     """Dummy class with docstring."""
 
@@ -53,7 +60,14 @@
             raise RuntimeError('Infinite loop detected.')
         return self, self._default_path
 
+class DummyObjectWithBDBBT(DummyObjectWithBD):
+    """Dummy class with docstring."""
 
+    def __bobo_traverse__(self, REQUEST, name):
+        if name == self._default_path[0]:
+            return getattr(self, name)
+        raise AttributeError, name
+
 class TestBaseRequest(TestCase):
 
     def setUp(self):
@@ -64,6 +78,8 @@
         self.f1._setObject('objWithDefaultNone', DummyObjectWithDefaultNone() )
         self.f1._setObject('objWithBPTH', DummyObjectWithBPTH() )
         self.f1._setObject('objWithBD', DummyObjectWithBD() )
+        self.f1._setObject('objWithBBT', DummyObjectWithBBT() )
+        self.f1._setObject('objWithBDBBT', DummyObjectWithBDBBT() )
 
     def makeBaseRequest(self):
         response = HTTPResponse()
@@ -131,6 +147,35 @@
         self.f1.objWithBD._default_path = ['']
         self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBD')
 
+    def test_traverse_withBBT_handles_AttributeError(self):
+        # Test that if __bobo_traverse__ raises AttributeError
+        # that we get a NotFound
+        from ZPublisher import NotFound
+        r = self.makeBaseRequest()
+        self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBBT/bbt_foo')
+
+    def test_traverse_withBDBBT(self):
+        # Test for an object which has a __browser_default__
+        # and __bobo_traverse__
+        # __bobo_traverse__ should return the object
+        # pointed by __browser_default__
+        r = self.makeBaseRequest()
+        self.f1.objWithBDBBT._default_path = ['view']
+        r.traverse('folder/objWithBDBBT')
+        self.assertEqual(r.URL, '/folder/objWithBDBBT/view')
+        self.assertEqual(r.response.base, '/folder/objWithBDBBT/')
+
+    def test_traverse_withBDBBT_NotFound(self):
+        # Test for an object which has a __browser_default__
+        # and __bobo_traverse__
+        # __bobo_traverse__ should raise an AttributeError, which will
+        # raise a NotFound
+        from ZPublisher import NotFound
+        r = self.makeBaseRequest()
+        self.f1.objWithBDBBT._default_path = ['xxx']
+        r = self.makeBaseRequest()
+        self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBDBBT')
+
     def test_traverse_slash(self):
         r = self.makeBaseRequest()
         r['PARENTS'] = [self.f1.objWithDefault]



More information about the Zope-Checkins mailing list