Issue #8404: Fix set operations on dictionary views.

This commit is contained in:
Alexandre Vassalotti 2010-05-04 03:21:51 +00:00
parent 04b99cc68d
commit 268e4872d3
3 changed files with 69 additions and 3 deletions

View File

@ -85,6 +85,67 @@ class DictSetTest(unittest.TestCase):
self.assertTrue(r == "dict_values(['ABC', 10])" or
r == "dict_values([10, 'ABC'])")
def test_keys_set_operations(self):
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 2}
d3 = {'d': 4, 'e': 5}
self.assertEqual(d1.viewkeys() & d1.viewkeys(), {'a', 'b'})
self.assertEqual(d1.viewkeys() & d2.viewkeys(), {'b'})
self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())
self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), {'a', 'b'})
self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), {'b'})
self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())
self.assertEqual(d1.viewkeys() | d1.viewkeys(), {'a', 'b'})
self.assertEqual(d1.viewkeys() | d2.viewkeys(), {'a', 'b', 'c'})
self.assertEqual(d1.viewkeys() | d3.viewkeys(), {'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), {'a', 'b'})
self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), {'a', 'b', 'c'})
self.assertEqual(d1.viewkeys() | set(d3.viewkeys()),
{'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())
self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), {'a', 'c'})
self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), {'a', 'b', 'd', 'e'})
self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())
self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), {'a', 'c'})
self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),
{'a', 'b', 'd', 'e'})
def test_items_set_operations(self):
d1 = {'a': 1, 'b': 2}
d2 = {'a': 2, 'b': 2}
d3 = {'d': 4, 'e': 5}
self.assertEqual(
d1.viewitems() & d1.viewitems(), {('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() & d2.viewitems(), {('b', 2)})
self.assertEqual(d1.viewitems() & d3.viewitems(), set())
self.assertEqual(d1.viewitems() & set(d1.viewitems()),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() & set(d2.viewitems()), {('b', 2)})
self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())
self.assertEqual(d1.viewitems() | d1.viewitems(),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() | d2.viewitems(),
{('a', 1), ('a', 2), ('b', 2)})
self.assertEqual(d1.viewitems() | d3.viewitems(),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
self.assertEqual(d1.viewitems() | set(d1.viewitems()),
{('a', 1), ('b', 2)})
self.assertEqual(d1.viewitems() | set(d2.viewitems()),
{('a', 1), ('a', 2), ('b', 2)})
self.assertEqual(d1.viewitems() | set(d3.viewitems()),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())
self.assertEqual(d1.viewitems() ^ d2.viewitems(),
{('a', 1), ('a', 2)})
self.assertEqual(d1.viewitems() ^ d3.viewitems(),
{('a', 1), ('b', 2), ('d', 4), ('e', 5)})
def test_main():
test_support.run_unittest(DictSetTest)

View File

@ -28,6 +28,8 @@ Core and Builtins
- Issue #7072: isspace(0xa0) is true on Mac OS X
- Issue #8404: Fixed set operations on dictionary views.
Library
-------

View File

@ -3000,13 +3000,14 @@ static PyNumberMethods dictviews_as_number = {
0, /*nb_add*/
(binaryfunc)dictviews_sub, /*nb_subtract*/
0, /*nb_multiply*/
0, /*nb_divide*/
0, /*nb_remainder*/
0, /*nb_divmod*/
0, /*nb_power*/
0, /*nb_negative*/
0, /*nb_positive*/
0, /*nb_absolute*/
0, /*nb_bool*/
0, /*nb_nonzero*/
0, /*nb_invert*/
0, /*nb_lshift*/
0, /*nb_rshift*/
@ -3040,7 +3041,8 @@ PyTypeObject PyDictKeys_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_CHECKTYPES, /* tp_flags */
0, /* tp_doc */
(traverseproc)dictview_traverse, /* tp_traverse */
0, /* tp_clear */
@ -3124,7 +3126,8 @@ PyTypeObject PyDictItems_Type = {
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_CHECKTYPES, /* tp_flags */
0, /* tp_doc */
(traverseproc)dictview_traverse, /* tp_traverse */
0, /* tp_clear */