summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDong-hee Na <donghee.na92@gmail.com>2019-10-06 20:28:33 +0900
committerSerhiy Storchaka <storchaka@gmail.com>2019-10-06 14:28:33 +0300
commitc38e725d17537b20ff090b1b5ec7db1820ff9b63 (patch)
tree43538ff0c73adcfe30a7db8d28af1a2e24c93436
parent65dcc8a8dc41d3453fd6b987073a5f1b30c5c0fd (diff)
downloadcpython-git-c38e725d17537b20ff090b1b5ec7db1820ff9b63.tar.gz
bpo-38210: Fix intersection operation with dict view and iterator. (GH-16602)
-rw-r--r--Lib/test/test_dictviews.py11
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2019-10-06-15-01-57.bpo-38210.Xgc6F_.rst2
-rw-r--r--Objects/dictobject.c8
3 files changed, 13 insertions, 8 deletions
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index b15cfebc98..7cf0192502 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -214,6 +214,17 @@ class DictSetTest(unittest.TestCase):
self.assertTrue(de.items().isdisjoint(de.items()))
self.assertTrue(de.items().isdisjoint([1]))
+ def test_set_operations_with_iterator(self):
+ origin = {1: 2, 3: 4}
+ self.assertEqual(origin.keys() & iter([1, 2]), {1})
+ self.assertEqual(origin.keys() | iter([1, 2]), {1, 2, 3})
+ self.assertEqual(origin.keys() ^ iter([1, 2]), {2, 3})
+
+ items = origin.items()
+ self.assertEqual(items & iter([(1, 2)]), {(1, 2)})
+ self.assertEqual(items ^ iter([(1, 2)]), {(3, 4)})
+ self.assertEqual(items | iter([(1, 2)]), {(1, 2), (3, 4)})
+
def test_recursive_repr(self):
d = {}
d[42] = d.values()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-10-06-15-01-57.bpo-38210.Xgc6F_.rst b/Misc/NEWS.d/next/Core and Builtins/2019-10-06-15-01-57.bpo-38210.Xgc6F_.rst
new file mode 100644
index 0000000000..768c6d4e64
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-10-06-15-01-57.bpo-38210.Xgc6F_.rst
@@ -0,0 +1,2 @@
+Remove unecessary intersection and update set operation in dictview with
+empty set. (Contributed by Dong-hee Na in :issue:`38210`.)
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 2f86946b98..5c3f1fb3c1 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -4225,14 +4225,6 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
it = PyObject_GetIter(other);
- _Py_IDENTIFIER(intersection_update);
- tmp = _PyObject_CallMethodIdOneArg(result, &PyId_intersection_update, other);
- if (tmp == NULL) {
- Py_DECREF(result);
- return NULL;
- }
- Py_DECREF(tmp);
-
if (PyDictKeys_Check(self)) {
dict_contains = dictkeys_contains;
}