From 6b0388dfec36917883a128700f16c39593483ddc Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Tue, 17 Sep 2019 13:28:40 -0600 Subject: [PATCH 1/4] bpo-38202: PyObject_GetIter() should be checked for failure An exception may occur during a PyObject_GetIter() call. --- Objects/dictobject.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 2f24105835120c..b0ba4fc12d3cbb 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -4219,6 +4219,10 @@ _PyDictView_Intersect(PyObject* self, PyObject *other) return NULL; it = PyObject_GetIter(other); + if (it == NULL) { + Py_DECREF(result); + return NULL; + } _Py_IDENTIFIER(intersection_update); tmp = _PyObject_CallMethodIdOneArg(result, &PyId_intersection_update, other); From 2f816835e2f6d075da409941f9259d6cb7261747 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 10 Oct 2019 22:31:08 +0100 Subject: [PATCH 2/4] Add unit test --- Lib/test/test_dictviews.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index b15cfebc98912d..abbf77bdf1a407 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -296,6 +296,10 @@ def test_abc_registry(self): self.assertIsInstance(d.items(), collections.abc.Iterable) self.assertIsInstance(d.items(), collections.abc.Container) + def test_bpo38202(self): + with self.assertRaises(TypeError): + {}.keys() & 1 + if __name__ == "__main__": unittest.main() From b5a9eabe6f389f02bdf03067c5adaa6396feb272 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 11 Oct 2019 00:37:04 +0300 Subject: [PATCH 3/4] Update test_dictviews.py --- Lib/test/test_dictviews.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index abbf77bdf1a407..b7946112a33d66 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -214,6 +214,25 @@ def test_items_set_operations(self): self.assertTrue(de.items().isdisjoint(de.items())) self.assertTrue(de.items().isdisjoint([1])) + 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() @@ -296,10 +315,6 @@ def test_abc_registry(self): self.assertIsInstance(d.items(), collections.abc.Iterable) self.assertIsInstance(d.items(), collections.abc.Container) - def test_bpo38202(self): - with self.assertRaises(TypeError): - {}.keys() & 1 - if __name__ == "__main__": unittest.main() From d8d67b0ab49696c1e67684152cb3d44355d8ebd0 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 11 Oct 2019 11:46:06 +0300 Subject: [PATCH 4/4] Fix indentation. --- Lib/test/test_dictviews.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 9ae9f8ea60f6d8..be271bebaaf1ff 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -227,7 +227,7 @@ def test_set_operations_with_iterator(self): 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): + def test_set_operations_with_noniterable(self): with self.assertRaises(TypeError): {}.keys() & 1 with self.assertRaises(TypeError):