diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 0cc6da3ba91..f90367660c5 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -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) diff --git a/Misc/NEWS b/Misc/NEWS index 8d180ff1202..5c16eac84c1 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -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 ------- diff --git a/Objects/dictobject.c b/Objects/dictobject.c index be2517609ee..2ab15207f9f 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -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 */