[Checkins] SVN: zope.app.container/trunk/ fix a bug where exceptions thrown during __setitem__ for an ordered container leaves a bad key in the ordering.

Paul Carduner paulcarduner at gmail.com
Tue Jul 22 18:30:45 EDT 2008


Log message for revision 88742:
  fix a bug where exceptions thrown during __setitem__ for an ordered container leaves a bad key in the ordering.

Changed:
  U   zope.app.container/trunk/CHANGES.txt
  U   zope.app.container/trunk/src/zope/app/container/ordered.py
  U   zope.app.container/trunk/src/zope/app/container/tests/test_ordered.py

-=-
Modified: zope.app.container/trunk/CHANGES.txt
===================================================================
--- zope.app.container/trunk/CHANGES.txt	2008-07-22 22:25:24 UTC (rev 88741)
+++ zope.app.container/trunk/CHANGES.txt	2008-07-22 22:30:45 UTC (rev 88742)
@@ -5,7 +5,8 @@
 
 3.6.1dev (unreleased)
 ---------------------
-
+- Bug: Error thrown during __setitem__ for an ordered container leaves
+  bad key in order
 - fixed #238579 / #163149: error with unicode traversing
 - fixed #221025 : adding menu is sorted with translated item
                   by using a collator (better localized sorting)

Modified: zope.app.container/trunk/src/zope/app/container/ordered.py
===================================================================
--- zope.app.container/trunk/src/zope/app/container/ordered.py	2008-07-22 22:25:24 UTC (rev 88741)
+++ zope.app.container/trunk/src/zope/app/container/ordered.py	2008-07-22 22:30:45 UTC (rev 88742)
@@ -204,7 +204,11 @@
             self._order.append(key)
 
         # This function creates a lot of events that other code listens to.
-        setitem(self, self._data.__setitem__, key, object)
+        try:
+            setitem(self, self._data.__setitem__, key, object)
+        except Exception, e:
+            self._order.remove(key)
+            raise e
 
         return key
 

Modified: zope.app.container/trunk/src/zope/app/container/tests/test_ordered.py
===================================================================
--- zope.app.container/trunk/src/zope/app/container/tests/test_ordered.py	2008-07-22 22:25:24 UTC (rev 88741)
+++ zope.app.container/trunk/src/zope/app/container/tests/test_ordered.py	2008-07-22 22:30:45 UTC (rev 88742)
@@ -89,6 +89,43 @@
         >>> setup.placefulTearDown()
     """
 
+def test_exception_causes_order_fix():
+    """
+    Prepare the setup::
+
+        >>> root = setup.placefulSetUp(site=True)
+
+    Now register an event subscriber to object added events that
+    throws an error.
+
+        >>> import zope.component
+        >>> from zope.app.container import interfaces
+
+        >>> @zope.component.adapter(interfaces.IObjectAddedEvent)
+        ... def raiseException(event):
+        ...     raise Exception()
+
+        >>> zope.component.provideHandler(raiseException)
+
+    Now we are adding an object to the container.
+
+        >>> from zope.app.container.ordered import OrderedContainer
+        >>> oc = OrderedContainer()
+        >>> oc['foo'] = 'FOO'
+        Traceback (most recent call last):
+        ...
+        Exception
+
+    The key 'foo' should not be around:
+
+        >>> 'foo' in oc.keys()
+        False
+
+    Finally, tear down::
+
+        >>> setup.placefulTearDown()
+    """
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(DocTestSuite("zope.app.container.ordered",



More information about the Checkins mailing list