]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Allow temporary hashability for the __contains__ test.
authorRaymond Hettinger <python@rcn.com>
Fri, 21 Nov 2003 18:36:54 +0000 (18:36 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 21 Nov 2003 18:36:54 +0000 (18:36 +0000)
(Requested by Alex Martelli.)

Lib/test/test_set.py
Objects/setobject.c

index c33461d7dd93d1a0b065fc4da0b39c0c79e1bb66..3203d516938c1f5c55bd135aee567084c43d58cc 100644 (file)
@@ -35,6 +35,8 @@ class TestJointOps(unittest.TestCase):
         for c in self.letters:
             self.assertEqual(c in self.s, c in self.d)
         self.assertRaises(TypeError, self.s.__contains__, [[]])
+        s = self.thetype([frozenset(self.letters)])
+        self.assert_(self.thetype(self.letters) in s)
 
     def test_copy(self):
         dup = self.s.copy()
index 88d5640635f2880569345ce817bbb5144a8b79bc..2d77c7485a3b72f418c35b46bd81858299c25b4f 100644 (file)
@@ -104,7 +104,23 @@ set_len(PySetObject *so)
 static int
 set_contains(PySetObject *so, PyObject *key)
 {
-       return PySequence_Contains(so->data, key);
+       PyObject *olddict;
+       PySetObject *tmp;
+       int result;
+
+       result = PySequence_Contains(so->data, key);
+       if (result == -1 && PyType_IsSubtype(key->ob_type, &PySet_Type)) {
+               PyErr_Clear();
+               tmp = (PySetObject *)make_new_set(&PyFrozenSet_Type, NULL);
+               if (tmp == NULL)
+                       return -1;
+               olddict = tmp->data;
+               tmp->data = ((PySetObject *)(key))->data;
+               result = PySequence_Contains(so->data, (PyObject *)tmp);
+               tmp->data = olddict;
+               Py_DECREF(tmp);
+       }
+       return result;
 }
 
 static PyObject *