[Zope-Checkins] SVN: Zope/trunk/src/Products/ZCatalog/ Avoid bare except's and catch Exception instead - this won't catch system exceptions like KeyboardInterrupt, MemoryError and similar things

Hanno Schlichting hannosch at hannosch.eu
Sat Jul 31 17:54:35 EDT 2010


Log message for revision 115296:
  Avoid bare except's and catch Exception instead - this won't catch system exceptions like KeyboardInterrupt, MemoryError and similar things
  

Changed:
  U   Zope/trunk/src/Products/ZCatalog/CatalogAwareness.py
  U   Zope/trunk/src/Products/ZCatalog/CatalogBrains.py
  U   Zope/trunk/src/Products/ZCatalog/CatalogPathAwareness.py
  U   Zope/trunk/src/Products/ZCatalog/Lazy.py
  U   Zope/trunk/src/Products/ZCatalog/ZCatalog.py
  U   Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py

-=-
Modified: Zope/trunk/src/Products/ZCatalog/CatalogAwareness.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/CatalogAwareness.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/CatalogAwareness.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -45,24 +45,30 @@
     def manage_afterAdd(self, item, container):
         self.index_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_afterAdd(item, container)
             if s is None: object._p_deactivate()
 
     def manage_afterClone(self, item):
         self.index_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_afterClone(item)
             if s is None: object._p_deactivate()
 
     def manage_beforeDelete(self, item, container):
         self.unindex_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_beforeDelete(item, container)
             if s is None: object._p_deactivate()
 

Modified: Zope/trunk/src/Products/ZCatalog/CatalogBrains.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/CatalogBrains.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/CatalogBrains.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -43,13 +43,6 @@
 
     def getURL(self, relative=0):
         """Generate a URL for this record"""
-        # XXX The previous implementation attempted to eat errors coming from
-        #     REQUEST.physicalPathToURL. Unfortunately it also ate
-        #     ConflictErrors (from getPath), which is bad. Staring at the
-        #     relevent code in HTTPRequest.py it's unclear to me what could be
-        #     raised by it so I'm removing the exception handling here all
-        #     together. If undesired exceptions get raised somehow we should
-        #     avoid bare except band-aids and find a real solution.
         return self.REQUEST.physicalPathToURL(self.getPath(), relative)
 
     def _unrestrictedGetObject(self):
@@ -61,7 +54,7 @@
             return self.aq_parent.unrestrictedTraverse(self.getPath())
         except ConflictError:
             raise
-        except:
+        except Exception:
             if GETOBJECT_RAISES:
                 raise
             return None
@@ -86,7 +79,7 @@
                 parent = parent.unrestrictedTraverse(path[:-1])
             except ConflictError:
                 raise
-            except:
+            except Exception:
                 if GETOBJECT_RAISES:
                     raise
                 return None
@@ -95,7 +88,7 @@
             target = parent.restrictedTraverse(path[-1])
         except ConflictError:
             raise
-        except:
+        except Exception:
             if GETOBJECT_RAISES:
                 raise
             return None

Modified: Zope/trunk/src/Products/ZCatalog/CatalogPathAwareness.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/CatalogPathAwareness.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/CatalogPathAwareness.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -41,24 +41,30 @@
     def manage_afterAdd(self, item, container):
         self.index_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_afterAdd(item, container)
             if s is None: object._p_deactivate()
 
     def manage_afterClone(self, item):
         self.index_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_afterClone(item)
             if s is None: object._p_deactivate()
 
     def manage_beforeDelete(self, item, container):
         self.unindex_object()
         for object in self.objectValues():
-            try: s=object._p_changed
-            except: s=0
+            try:
+                s = object._p_changed
+            except Exception:
+                s = 0
             object.manage_beforeDelete(item, container)
             if s is None: object._p_deactivate()
 

Modified: Zope/trunk/src/Products/ZCatalog/Lazy.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/Lazy.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/Lazy.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -31,7 +31,7 @@
             try:
                 self[l]
                 l = l + 1
-            except:
+            except Exception:
                 self._len = l
                 return l
 
@@ -99,7 +99,7 @@
         sindex = self._sindex
         try:
             s = seq[sindex]
-        except:
+        except Exception:
             raise IndexError(index)
         eindex = self._eindex
         while i > ind:

Modified: Zope/trunk/src/Products/ZCatalog/ZCatalog.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/ZCatalog.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/ZCatalog.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -299,7 +299,7 @@
                     self.catalog_object(obj, p, pghandler=pghandler)
                 except ConflictError:
                     raise
-                except:
+                except Exception:
                     LOG.error('Recataloging object at %s failed' % p,
                               exc_info=sys.exc_info())
 
@@ -732,12 +732,15 @@
 
         if not hasattr(base, 'objectItems'):
             return result
-        try:    items=obj.objectItems()
-        except: return result
+        try:
+            items = obj.objectItems()
+        except Exception:
+            return result
 
-        try: add_result=result.append
-        except:
-            raise AttributeError, `result`
+        try:
+            add_result = result.append
+        except Exception:
+            raise AttributeError(repr(result))
 
         for id, ob in items:
             if pre: p="%s/%s" % (pre, id)
@@ -800,8 +803,10 @@
             script=REQUEST.script
             if path.find(script) != 0:
                 path='%s/%s' % (script, path)
-            try: return REQUEST.resolve_url(path)
-            except: pass
+            try:
+                return REQUEST.resolve_url(path)
+            except Exception:
+                pass
 
     def resolve_path(self, path):
         """
@@ -810,8 +815,10 @@
         style url. If no object is found, None is returned.
         No exceptions are raised.
         """
-        try: return self.unrestrictedTraverse(path)
-        except: pass
+        try:
+            return self.unrestrictedTraverse(path)
+        except Exception:
+            pass
 
     def manage_normalize_paths(self, REQUEST):
         """Ensure that all catalog paths are full physical paths

Modified: Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py	2010-07-31 21:50:48 UTC (rev 115295)
+++ Zope/trunk/src/Products/ZCatalog/tests/test_catalog.py	2010-07-31 21:54:33 UTC (rev 115296)
@@ -442,7 +442,7 @@
     def testBadUncatalog(self):
         try:
             self._catalog.uncatalogObject('asdasdasd')
-        except:
+        except Exception:
             self.fail('uncatalogObject raised exception on bad uid')
 
     def testUniqueValuesForLength(self):



More information about the Zope-Checkins mailing list