]> granicus.if.org Git - python/commitdiff
Correctly forward exception in instance_contains().
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 8 Nov 2006 06:46:37 +0000 (06:46 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 8 Nov 2006 06:46:37 +0000 (06:46 +0000)
Fixes #1591996. Patch contributed by Neal Norwitz.
Will backport.

Lib/test/test_class.py
Objects/classobject.c

index 6c91deb129b35867867002215e538b4dde125d30..26b8e7a134f53affbebd5e1c1cafd4deb9f7ad18 100644 (file)
@@ -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]
index 7680a3d6d0a20dda1d3e51f01298ea6039417d5b..8560b6842cec2213488ee0c1796ce9a1c1785069 100644 (file)
@@ -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