Skip to content

Commit

Permalink
100% coverage for ordered.py
Browse files Browse the repository at this point in the history
But note this exposes a bug: #17
  • Loading branch information
jamadden committed Jul 27, 2017
1 parent 86e9147 commit d8a81bc
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions src/zope/container/ordered.py
Expand Up @@ -185,27 +185,13 @@ def __setitem__(self, key, object):

existed = key in self._data

bad = False
if isinstance(key, six.string_types):
try:
key.decode()
except AttributeError:
# Py3 str cannot decode.
pass
except UnicodeError:
bad = True
else:
bad = True
if bad:
raise TypeError("'%s' is invalid, the key must be an "
"ascii or unicode string" % key)
if len(key) == 0:
raise ValueError("The key cannot be an empty string")

# We have to first update the order, so that the item is available,
# otherwise most API functions will lie about their available values
# when an event subscriber tries to do something with the container.
if not existed:
# XXX: This is not using the decoded key that setitem will use.
# In the case of a bytes key. This is bad on Python 3. But also not tested.
# See https://github.com/zopefoundation/zope.container/issues/17
self._order.append(key)

# This function creates a lot of events that other code listens to.
Expand Down Expand Up @@ -290,8 +276,7 @@ def updateOrder(self, order):
0
"""

if not isinstance(order, list) and \
not isinstance(order, tuple):
if not isinstance(order, (list, tuple)):
raise TypeError('order must be a tuple or a list.')

if len(order) != len(self._order):
Expand All @@ -301,10 +286,10 @@ def updateOrder(self, order):
will_be_dict = {}
new_order = PersistentList()

for i in range(len(order)):
for i, obj in enumerate(order):
was_dict[self._order[i]] = 1
will_be_dict[order[i]] = 1
new_order.append(order[i])
will_be_dict[obj] = 1
new_order.append(obj)

if will_be_dict != was_dict:
raise ValueError("Incompatible key set.")
Expand Down

0 comments on commit d8a81bc

Please sign in to comment.