]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
issubset() and issuperset() to work with general iterables
authorRaymond Hettinger <python@rcn.com>
Fri, 21 Nov 2003 07:56:36 +0000 (07:56 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 21 Nov 2003 07:56:36 +0000 (07:56 +0000)
Lib/test/test_set.py
Objects/setobject.c

index 8329fd16312ea1203b75b20f2a5e4f5c12a8791c..c33461d7dd93d1a0b065fc4da0b39c0c79e1bb66 100644 (file)
@@ -143,6 +143,10 @@ class TestJointOps(unittest.TestCase):
         self.failIf(q <= r)
         self.failIf(q > r)
         self.failIf(q >= r)
+        self.assert_(set('a').issubset('abc'))
+        self.assert_(set('abc').issuperset('a'))
+        self.failIf(set('a').issubset('cbs'))
+        self.failIf(set('cbs').issuperset('a'))
 
     def test_pickling(self):
         p = pickle.dumps(self.s)
index d7190b59b0cea9fa704437c3c6f3beb0c6d449d6..88d5640635f2880569345ce817bbb5144a8b79bc 100644 (file)
@@ -588,11 +588,15 @@ set_ixor(PySetObject *so, PyObject *other)
 static PyObject *
 set_issubset(PySetObject *so, PyObject *other)
 {
-       PyObject *otherdata, *it, *item;
+       PyObject *otherdata, *it, *item, *tmp, *result;
 
        if (!PyAnySet_Check(other)) {
-               PyErr_SetString(PyExc_TypeError, "can only compare to a set");
-               return NULL;
+               tmp = make_new_set(&PySet_Type, other);
+               if (tmp == NULL)
+                       return NULL;
+               result = set_issubset(so, tmp);
+               Py_DECREF(tmp);
+               return result;
        }
        if (set_len(so) > set_len((PySetObject *)other)) 
                Py_RETURN_FALSE;
@@ -621,9 +625,15 @@ PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
 static PyObject *
 set_issuperset(PySetObject *so, PyObject *other)
 {
+       PyObject *tmp, *result;
+
        if (!PyAnySet_Check(other)) {
-               PyErr_SetString(PyExc_TypeError, "can only compare to a set");
-               return NULL;
+               tmp = make_new_set(&PySet_Type, other);
+               if (tmp == NULL)
+                       return NULL;
+               result = set_issuperset(so, tmp);
+               Py_DECREF(tmp);
+               return result;
        }
        return set_issubset((PySetObject *)other, (PyObject *)so);
 }