summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZackery Spytz <zspytz@gmail.com>2019-10-13 05:49:05 -0600
committerSerhiy Storchaka <storchaka@gmail.com>2019-10-13 14:49:05 +0300
commitb16e382c446d76ede22780b15c75f43c5f132e25 (patch)
tree34c21fb3cdbd01798dd493e9afc52f2972b7bfbd
parent793cb85437299a3da3d74fe65480d720af330cbb (diff)
downloadcpython-git-b16e382c446d76ede22780b15c75f43c5f132e25.tar.gz
bpo-38202: Fix a crash in dict_view & non-itearble. (GH-16241)
-rw-r--r--Lib/test/test_dictviews.py19
-rw-r--r--Objects/dictobject.c4
2 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index 8410e8b5c4..be271bebaa 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -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()
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index b496350d47..64876e0519 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -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;