New submission from Caleb Levy:

The current implementation ItemsView.__contains__ reads

class ItemsView(MappingView, Set):
    ...
    def __contains__(self, item):
        key, value = item
        try:
            v = self._mapping[key]
        except KeyError:
            return False
        else:
            return v == value
    ...

This poses several problems. First, any non-iterable or iterable not having 
exactly two elements will raise an error instead of returning false. 

Second, an ItemsView object is roughly the same as a set of tuple-pairs hashed 
by the first element. Thus, for example,

    ["a", 1] in d.items()

will return False for any dict d, yet in the current ItemsView implementation, 
this is True.

The patch changes behavior to immediately return false for non-tuple items and 
tuples not of length 2, avoiding unnecessary exceptions. It also adds tests to 
collections which fail under the old behavior and pass with the update.

----------
keywords: +patch
Added file: http://bugs.python.org/file39682/ItemsView_contains.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24434>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to