[Zope3-checkins] CVS: Zope3/src/zope/app/container - ordered.py:1.4

Garrett Smith garrett@mojave-corp.com
Fri, 1 Aug 2003 15:46:50 -0400


Update of /cvs-repository/Zope3/src/zope/app/container
In directory cvs.zope.org:/tmp/cvs-serv32092/src/zope/app/container

Modified Files:
	ordered.py 
Log Message:
Added additional validation for input to updateOrder. Also ensured that _order is a PersistentList after this 
call. Updated tests and interface docs accordingly.


=== Zope3/src/zope/app/container/ordered.py 1.3 => 1.4 ===
--- Zope3/src/zope/app/container/ordered.py:1.3	Tue Jun 24 06:33:59 2003
+++ Zope3/src/zope/app/container/ordered.py	Fri Aug  1 15:46:46 2003
@@ -22,7 +22,7 @@
 from persistence import Persistent
 from persistence.dict import PersistentDict
 from persistence.list import PersistentList
-from types import StringTypes
+from types import StringTypes, TupleType, ListType
 
 class OrderedContainer(Persistent):
     """ OrderedContainer maintains entries' order as added and moved.
@@ -253,24 +253,41 @@
         >>> oc.updateOrder(1)
         Traceback (most recent call last):
         ...
-        TypeError: len() of unsized object
+        TypeError: order must be a tuple or a list.
+        >>> oc.updateOrder('bar')
+        Traceback (most recent call last):
+        ...
+        TypeError: order must be a tuple or a list.
         >>> oc.updateOrder(['baz', 'zork', 'quux'])
         Traceback (most recent call last):
         ...
         ValueError: Incompatible key set.
+        >>> del oc['baz']
+        >>> del oc['zork']
+        >>> del oc['foo']
+        >>> len(oc)
+        0
         """
 
+        if not isinstance(order, ListType) and \
+            not isinstance(order, TupleType):
+            raise TypeError('order must be a tuple or a list.')
+
+        order = list(order)
         if len(order) != len(self._order):
             raise ValueError("Incompatible key set.")
 
         was_dict = {}
         will_be_dict = {}
+        new_order = PersistentList()
 
         for i in range(len(order)):
             was_dict[self._order[i]] = 1
             will_be_dict[order[i]] = 1
+            new_order.append(order[i])
 
         if will_be_dict != was_dict:
             raise ValueError("Incompatible key set.")
 
-        self._order = order
+        self._order = new_order
+