]> granicus.if.org Git - python/commitdiff
bpo-38202: Fix a crash in dict_view & non-itearble. (GH-16241)
authorZackery Spytz <zspytz@gmail.com>
Sun, 13 Oct 2019 11:49:05 +0000 (05:49 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 13 Oct 2019 11:49:05 +0000 (14:49 +0300)
Lib/test/test_dictviews.py
Objects/dictobject.c

index 8410e8b5c44d468db6119b0cbbdbd6e74b7ea64a..be271bebaaf1fffe1d955f4cfa817b2af650c795 100644 (file)
@@ -227,6 +227,25 @@ class DictSetTest(unittest.TestCase):
         self.assertEqual(items | iter([(1, 2)]), {(1, 2), (3, 4)})
         self.assertEqual(items - iter([(1, 2)]), {(3, 4)})
 
+    def test_set_operations_with_noniterable(self):
+        with self.assertRaises(TypeError):
+            {}.keys() & 1
+        with self.assertRaises(TypeError):
+            {}.keys() | 1
+        with self.assertRaises(TypeError):
+            {}.keys() ^ 1
+        with self.assertRaises(TypeError):
+            {}.keys() - 1
+
+        with self.assertRaises(TypeError):
+            {}.items() & 1
+        with self.assertRaises(TypeError):
+            {}.items() | 1
+        with self.assertRaises(TypeError):
+            {}.items() ^ 1
+        with self.assertRaises(TypeError):
+            {}.items() - 1
+
     def test_recursive_repr(self):
         d = {}
         d[42] = d.values()
index b496350d470936b726e931ba2ffbb72716d4f2fe..64876e05191e23753dc289892f2b0c40a68c0db1 100644 (file)
@@ -4227,6 +4227,10 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
         return NULL;
 
     it = PyObject_GetIter(other);
+    if (it == NULL) {
+        Py_DECREF(result);
+        return NULL;
+    }
 
     if (PyDictKeys_Check(self)) {
         dict_contains = dictkeys_contains;