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
- 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
......
......@@ -309,14 +309,20 @@ class BaseRequest:
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 @@ class BaseRequest:
# 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
......
......@@ -43,6 +43,13 @@ class DummyObjectWithBPTH(DummyObjectBasic):
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,6 +60,13 @@ class DummyObjectWithBD(DummyObjectBasic):
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):
......@@ -64,6 +78,8 @@ class TestBaseRequest(TestCase):
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 @@ class TestBaseRequest(TestCase):
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]
......
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