[Python-checkins] cpython (3.5): Issue #28189: dictitems_contains no longer swallows compare errors.

raymond.hettinger python-checkins at python.org
Mon Sep 19 00:46:40 EDT 2016


https://hg.python.org/cpython/rev/2a9e0e869ca7
changeset: 103946:2a9e0e869ca7
branch: 3.5
parent: 103921:c4cec8f7c727
user: Raymond Hettinger <python at rcn.com>
date: Sun Sep 18 21:45:11 2016 -0700
summary:
 Issue #28189: dictitems_contains no longer swallows compare errors.
 (Patch by Xiang Zhang)
files:
 Lib/test/test_dictviews.py | 26 ++++++++++++++++++++++++++
 Misc/NEWS | 3 +++
 Objects/dictobject.c | 2 +-
 3 files changed, 30 insertions(+), 1 deletions(-)
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -209,6 +209,32 @@
 self.assertRaises(TypeError, copy.copy, d.values())
 self.assertRaises(TypeError, copy.copy, d.items())
 
+ def test_compare_error(self):
+ class Exc(Exception):
+ pass
+
+ class BadEq:
+ def __hash__(self):
+ return 7
+ def __eq__(self, other):
+ raise Exc
+
+ k1, k2 = BadEq(), BadEq()
+ v1, v2 = BadEq(), BadEq()
+ d = {k1: v1}
+
+ self.assertIn(k1, d)
+ self.assertIn(k1, d.keys())
+ self.assertIn(v1, d.values())
+ self.assertIn((k1, v1), d.items())
+
+ self.assertRaises(Exc, d.__contains__, k2)
+ self.assertRaises(Exc, d.keys().__contains__, k2)
+ self.assertRaises(Exc, d.items().__contains__, (k2, v1))
+ self.assertRaises(Exc, d.items().__contains__, (k1, v2))
+ with self.assertRaises(Exc):
+ v2 in d.values()
+
 def test_pickle(self):
 d = {1: 10, "a": "ABC"}
 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,9 @@
 - Issue #25758: Prevents zipimport from unnecessarily encoding a filename
 (patch by Eryk Sun)
 
+- Issue #28189: dictitems_contains no longer swallows compare errors.
+ (Patch by Xiang Zhang)
+
 - Issue #27812: Properly clear out a generator's frame's backreference to the
 generator to prevent crashes in frame.clear().
 
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -3654,7 +3654,7 @@
 return 0;
 key = PyTuple_GET_ITEM(obj, 0);
 value = PyTuple_GET_ITEM(obj, 1);
- found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
+ found = PyDict_GetItemWithError((PyObject *)dv->dv_dict, key);
 if (found == NULL) {
 if (PyErr_Occurred())
 return -1;
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /