[Checkins] SVN: zope.container/trunk/ Fix a bug that made it impossible to store None values in containers

Marius Gedminas cvs-admin at zope.org
Wed Oct 24 08:36:37 UTC 2012


Log message for revision 128136:
  Fix a bug that made it impossible to store None values in containers
  (LP#1070719).
  
  

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

-=-
Modified: zope.container/trunk/CHANGES.txt
===================================================================
--- zope.container/trunk/CHANGES.txt	2012-10-23 00:39:05 UTC (rev 128135)
+++ zope.container/trunk/CHANGES.txt	2012-10-24 08:36:36 UTC (rev 128136)
@@ -21,7 +21,10 @@
 
 - Handle Broken Objects more gracefully
 
+- Fix a bug that made it impossible to store None values in containers
+  (LP#1070719).
 
+
 3.12.0 (2010-12-14)
 -------------------
 

Modified: zope.container/trunk/src/zope/container/contained.py
===================================================================
--- zope.container/trunk/src/zope/container/contained.py	2012-10-23 00:39:05 UTC (rev 128135)
+++ zope.container/trunk/src/zope/container/contained.py	2012-10-24 08:36:36 UTC (rev 128136)
@@ -331,6 +331,8 @@
     """Notify that the container was modified."""
     notify(ContainerModifiedEvent(object, *descriptions))
 
+_SENTINEL = object()
+
 def setitem(container, setitemf, name, object):
     """Helper function to set an item and generate needed events
 
@@ -547,10 +549,10 @@
     if not name:
         raise ValueError("empty names are not allowed")
 
-    old = container.get(name)
+    old = container.get(name, _SENTINEL)
     if old is object:
         return
-    if old is not None:
+    if old is not _SENTINEL:
         raise KeyError(name)
 
     object, event = containedEvent(object, container, name)

Modified: zope.container/trunk/src/zope/container/tests/test_ordered.py
===================================================================
--- zope.container/trunk/src/zope/container/tests/test_ordered.py	2012-10-23 00:39:05 UTC (rev 128135)
+++ zope.container/trunk/src/zope/container/tests/test_ordered.py	2012-10-24 08:36:36 UTC (rev 128136)
@@ -122,6 +122,27 @@
 
     """
 
+def test_adding_none():
+    """Test for OrderedContainer
+
+    This is a regression test: adding None to an OrderedContainer
+    used to corrupt its internal data structure (_order and _data
+    wouldl get out of sync, causing KeyErrors when you tried to iterate).
+
+        >>> from zope.container.ordered import OrderedContainer
+        >>> oc = OrderedContainer()
+        >>> oc['foo'] = None
+        >>> oc.keys()
+        ['foo']
+        >>> oc.values()
+        [None]
+        >>> oc.items()
+        [('foo', None)]
+        >>> print oc['foo']
+        None
+
+    """
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest(DocTestSuite("zope.container.ordered",



More information about the checkins mailing list