From 3a62404264e02acbee83994c411e292bccd4e8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Wed, 8 Nov 2006 06:46:37 +0000 Subject: [PATCH] Correctly forward exception in instance_contains(). Fixes #1591996. Patch contributed by Neal Norwitz. Will backport. --- Lib/test/test_class.py | 8 ++++++++ Objects/classobject.c | 10 ++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index 6c91deb129b..26b8e7a134f 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -172,6 +172,14 @@ testme ^ 1 # List/dict operations +class Empty: pass + +try: + 1 in Empty() + print 'failed, should have raised TypeError' +except TypeError: + pass + 1 in testme testme[1] diff --git a/Objects/classobject.c b/Objects/classobject.c index 7680a3d6d0a..8560b6842ce 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -1318,15 +1318,17 @@ instance_contains(PyInstanceObject *inst, PyObject *member) /* Couldn't find __contains__. */ if (PyErr_ExceptionMatches(PyExc_AttributeError)) { + Py_ssize_t rc; /* Assume the failure was simply due to that there is no * __contains__ attribute, and try iterating instead. */ PyErr_Clear(); - return _PySequence_IterSearch((PyObject *)inst, member, - PY_ITERSEARCH_CONTAINS) > 0; + rc = _PySequence_IterSearch((PyObject *)inst, member, + PY_ITERSEARCH_CONTAINS); + if (rc >= 0) + return rc > 0; } - else - return -1; + return -1; } static PySequenceMethods