Zope 2.11b1: "BaseRequest.traverse" may fail without "__getitem__"

Bug #213311 reported by Dieter Maurer
2
Affects Status Importance Assigned to Milestone
Zope 2
Fix Released
Undecided
Tres Seaver

Bug Description

When "ZPublisher.BaseRequest.BaseRequest" traverses over an object without "__getitem__"
and the other lookup methods have failed, then a "TypeError" exception ("unsubscriptable oject")
is raised rather than a "NotFound" exception. This causes a wrong response code to be generated
("500" (internal server error) rather than "404" (not found)).

Revision history for this message
Tres Seaver (tseaver) wrote : Re: [Bug 213311] [NEW] Zope 2.11b1: "BaseRequest.traverse" may fail without "__getitem__"

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dmaurer wrote:
> Public bug reported:
>
> When "ZPublisher.BaseRequest.BaseRequest" traverses over an object without "__getitem__"
> and the other lookup methods have failed, then a "TypeError" exception ("unsubscriptable oject")
> is raised rather than a "NotFound" exception. This causes a wrong response code to be generated
> ("500" (internal server error) rather than "404" (not found)).

I just tried adding a testcase for this, and couldn't provoke the error:
    the AttributeError on the object with no '__getitem__' got turned
into a NotFound. Can you provide a testcase?

Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 <email address hidden>
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFH+jSr+gerLs4ltQ4RAq58AJ0SG78p47cVhpVGHYrJAk+jzroQaACfRGU0
X2DxbSxGFwMp2zf2pkAbH0M=
=mn4d
-----END PGP SIGNATURE-----

Revision history for this message
Dieter Maurer (d.maurer) wrote :

This is the test case, I added to "ZPublisher.tests.testBaseRequest.TestBaseRequest":

    def test_traverse_no_subscription(self):
        class _Object(object): pass
        self.root = _Object()
        r = self.makeBaseRequest()
        from ZPublisher import NotFound
        self.assertRaises(NotFound, r.traverse, 'not_found')

And this is the resulting traceback:

Error in test test_traverse_no_subscription (ZPublisher.tests.testBaseRequest.TestBaseRequest)
Traceback (most recent call last):
  File "/usr/local/lib/python2.4/unittest.py", line 260, in run
    testMethod()
  File "/home/dieter/G/z211/Base/lib/python/ZPublisher/tests/testBaseRequest.py", line 123, in test_traverse_no_subscription
    self.assertRaises(NotFound, r.traverse, 'not_found')
  File "/usr/local/lib/python2.4/unittest.py", line 320, in failUnlessRaises
    callableObj(*args, **kwargs)
  File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 485, in traverse
  File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 329, in traverseName
  File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 124, in publishTraverse
TypeError: unsubscriptable object

Revision history for this message
Tres Seaver (tseaver) wrote : Re: [Bug 213311] Re: Zope 2.11b1: "BaseRequest.traverse" may fail without "__getitem__"

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

dmaurer wrote:
> This is the test case, I added to
> "ZPublisher.tests.testBaseRequest.TestBaseRequest":
>
> def test_traverse_no_subscription(self):
> class _Object(object): pass
> self.root = _Object()
> r = self.makeBaseRequest()
> from ZPublisher import NotFound
> self.assertRaises(NotFound, r.traverse, 'not_found')
>
> And this is the resulting traceback:
>
> Error in test test_traverse_no_subscription (ZPublisher.tests.testBaseRequest.TestBaseRequest)
> Traceback (most recent call last):
> File "/usr/local/lib/python2.4/unittest.py", line 260, in run
> testMethod()
> File "/home/dieter/G/z211/Base/lib/python/ZPublisher/tests/testBaseRequest.py", line 123, in test_traverse_no_subscription
> self.assertRaises(NotFound, r.traverse, 'not_found')
> File "/usr/local/lib/python2.4/unittest.py", line 320, in failUnlessRaises
> callableObj(*args, **kwargs)
> File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 485, in traverse
> File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 329, in traverseName
> File "/home/dieter/G/Zope-2.11.0-b1/lib/python/ZPublisher/BaseRequest.py", line 124, in publishTraverse
> TypeError: unsubscriptable object

The attached patch adds that test and a fix: all tests pass after
applying it to the 2.10 branch.

Tres.
- --
===================================================================
Tres Seaver +1 540-429-0999 <email address hidden>
Palladion Software "Excellence by Design" http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFH+nA9+gerLs4ltQ4RAs0mAJ9sHVBmPqAmdisgI1RhT0mArMlrZQCeLB3H
ubuLWo/HpPilQc3O5zoehKg=
=Aw79
-----END PGP SIGNATURE-----

Revision history for this message
Tres Seaver (tseaver) wrote :
Changed in zope2:
assignee: nobody → tseaver
status: New → Fix Committed
Revision history for this message
Tres Seaver (tseaver) wrote :

Included in 2.10.6 and 2.11.

Changed in zope2:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.