Commit 714cda2d authored by Sidnei da Silva's avatar Sidnei da Silva

- 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
parent 08478a29
...@@ -64,6 +64,11 @@ Zope Changes ...@@ -64,6 +64,11 @@ Zope Changes
- If a PROPPATCH element value contains only a CDATA section, - If a PROPPATCH element value contains only a CDATA section,
store the CDATA contents only. 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 Zope 2.8a1
......
...@@ -309,14 +309,20 @@ class BaseRequest: ...@@ -309,14 +309,20 @@ class BaseRequest:
else: return response.forbiddenError(entry_name) else: return response.forbiddenError(entry_name)
if hasattr(object,'__bobo_traverse__'): if hasattr(object,'__bobo_traverse__'):
try:
subobject=object.__bobo_traverse__(request,entry_name) subobject=object.__bobo_traverse__(request,entry_name)
if type(subobject) is type(()) and len(subobject) > 1: if type(subobject) is type(()) and len(subobject) > 1:
# Add additional parents into the path # Add additional parents into the path
parents[-1:] = list(subobject[:-1]) parents[-1:] = list(subobject[:-1])
object, subobject = subobject[-2:] 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: else:
try: try:
# Note - no_acquire_flag is necessary to support # Note - no_acquire_flag is necessary to support
# things like DAV. We have to make sure # things like DAV. We have to make sure
# that the target object is not acquired # that the target object is not acquired
......
...@@ -43,6 +43,13 @@ class DummyObjectWithBPTH(DummyObjectBasic): ...@@ -43,6 +43,13 @@ class DummyObjectWithBPTH(DummyObjectBasic):
REQUEST._hacked_path=1 REQUEST._hacked_path=1
class DummyObjectWithBBT(DummyObjectBasic):
""" Dummy class with docstring.
"""
def __bobo_traverse__(self, REQUEST, name):
raise AttributeError, name
class DummyObjectWithBD(DummyObjectBasic): class DummyObjectWithBD(DummyObjectBasic):
"""Dummy class with docstring.""" """Dummy class with docstring."""
...@@ -53,6 +60,13 @@ class DummyObjectWithBD(DummyObjectBasic): ...@@ -53,6 +60,13 @@ class DummyObjectWithBD(DummyObjectBasic):
raise RuntimeError('Infinite loop detected.') raise RuntimeError('Infinite loop detected.')
return self, self._default_path 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): class TestBaseRequest(TestCase):
...@@ -64,6 +78,8 @@ class TestBaseRequest(TestCase): ...@@ -64,6 +78,8 @@ class TestBaseRequest(TestCase):
self.f1._setObject('objWithDefaultNone', DummyObjectWithDefaultNone() ) self.f1._setObject('objWithDefaultNone', DummyObjectWithDefaultNone() )
self.f1._setObject('objWithBPTH', DummyObjectWithBPTH() ) self.f1._setObject('objWithBPTH', DummyObjectWithBPTH() )
self.f1._setObject('objWithBD', DummyObjectWithBD() ) self.f1._setObject('objWithBD', DummyObjectWithBD() )
self.f1._setObject('objWithBBT', DummyObjectWithBBT() )
self.f1._setObject('objWithBDBBT', DummyObjectWithBDBBT() )
def makeBaseRequest(self): def makeBaseRequest(self):
response = HTTPResponse() response = HTTPResponse()
...@@ -131,6 +147,35 @@ class TestBaseRequest(TestCase): ...@@ -131,6 +147,35 @@ class TestBaseRequest(TestCase):
self.f1.objWithBD._default_path = [''] self.f1.objWithBD._default_path = ['']
self.failUnlessRaises(NotFound, r.traverse, 'folder/objWithBD') 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): def test_traverse_slash(self):
r = self.makeBaseRequest() r = self.makeBaseRequest()
r['PARENTS'] = [self.f1.objWithDefault] r['PARENTS'] = [self.f1.objWithDefault]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment