Skip to content

Commit e4817f3

Browse files
ZackerySpytzJake Taylor
authored andcommitted
bpo-38202: Fix a crash in dict_view & non-itearble. (pythonGH-16241)
1 parent e62813f commit e4817f3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Lib/test/test_dictviews.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ def test_set_operations_with_iterator(self):
227227
self.assertEqual(items | iter([(1, 2)]), {(1, 2), (3, 4)})
228228
self.assertEqual(items - iter([(1, 2)]), {(3, 4)})
229229

230+
def test_set_operations_with_noniterable(self):
231+
with self.assertRaises(TypeError):
232+
{}.keys() & 1
233+
with self.assertRaises(TypeError):
234+
{}.keys() | 1
235+
with self.assertRaises(TypeError):
236+
{}.keys() ^ 1
237+
with self.assertRaises(TypeError):
238+
{}.keys() - 1
239+
240+
with self.assertRaises(TypeError):
241+
{}.items() & 1
242+
with self.assertRaises(TypeError):
243+
{}.items() | 1
244+
with self.assertRaises(TypeError):
245+
{}.items() ^ 1
246+
with self.assertRaises(TypeError):
247+
{}.items() - 1
248+
230249
def test_recursive_repr(self):
231250
d = {}
232251
d[42] = d.values()

Objects/dictobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4227,6 +4227,10 @@ _PyDictView_Intersect(PyObject* self, PyObject *other)
42274227
return NULL;
42284228

42294229
it = PyObject_GetIter(other);
4230+
if (it == NULL) {
4231+
Py_DECREF(result);
4232+
return NULL;
4233+
}
42304234

42314235
if (PyDictKeys_Check(self)) {
42324236
dict_contains = dictkeys_contains;

0 commit comments

Comments
 (0)