[Zope3-dev] DefaultTraversable and __getitem__

Garrett Smith garrett at mojave-corp.com
Fri Mar 4 14:31:59 EST 2005


Currently z/a/traversing/adapters/DefaultTraversable uses an object's
__getitem__ if it has one. This is great, but it lets KeyError
exceptions propogate, which seems strange. I think it should catch these
and raise TraversalError.

I'd normally go ahead and commit the change, but this code has been here
a while and no one has complained. I want to vet this before committing
it:

Index: adapters.py
===================================================================
--- adapters.py	(revision 29319)
+++ adapters.py	(working copy)
@@ -48,13 +48,14 @@
         attr = getattr(subject, name, _marker)
         if attr is not _marker:
             return attr
-
         if hasattr(subject, '__getitem__'):
-            # Let exceptions propagate.
-            return subject[name]
-        else:
-            raise TraversalError(subject, name)
+            try:
+                return subject[name]
+            except KeyError:
+                pass
+        raise TraversalError(subject, name)

If no one objects, I'll commit it later today.

 -- Garrett


More information about the Zope3-dev mailing list