From 7eb1becc2502723ebcdcc56972e41c03ef80d6af Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 18 Sep 2016 21:45:11 -0700 Subject: Issue #28189: dictitems_contains no longer swallows compare errors. (Patch by Xiang Zhang) --- Lib/test/test_dictviews.py | 26 ++++++++++++++++++++++++++ Misc/NEWS | 3 +++ Objects/dictobject.c | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index ab23ca1..0c99652 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -209,6 +209,32 @@ class DictSetTest(unittest.TestCase): 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 index c23ec97..a0dccdb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,9 @@ Core and Builtins - 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 index e04ab2b..e79ab36 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3654,7 +3654,7 @@ dictitems_contains(_PyDictViewObject *dv, PyObject *obj) 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; -- cgit v0.12 From 26d998cfdd1f843a154e5398d09c0270fa17aa30 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sun, 18 Sep 2016 23:41:11 -0700 Subject: properly handle the single null-byte file (closes #24022) --- Parser/tokenizer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index 1540d26..1a8cf40 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -1014,7 +1014,7 @@ tok_nextc(struct tok_state *tok) else { tok->done = E_OK; tok->inp = strchr(tok->buf, '\0'); - done = tok->inp[-1] == '\n'; + done = tok->inp == tok->buf || tok->inp[-1] == '\n'; } } else { -- cgit v0.12