diff options
author | Raymond Hettinger <python@rcn.com> | 2003-11-21 07:56:36 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-11-21 07:56:36 (GMT) |
commit | 3fbec701cae361de6beca55bc926e0540635f165 (patch) | |
tree | c192fc82927882e3c8ba16708bd264752a690e42 | |
parent | 82d73dd459d911dcb12a48db12b7f8b3eb0f6b1b (diff) | |
download | cpython-3fbec701cae361de6beca55bc926e0540635f165.zip cpython-3fbec701cae361de6beca55bc926e0540635f165.tar.gz cpython-3fbec701cae361de6beca55bc926e0540635f165.tar.bz2 |
issubset() and issuperset() to work with general iterables
-rw-r--r-- | Lib/test/test_set.py | 4 | ||||
-rw-r--r-- | Objects/setobject.c | 20 |
2 files changed, 19 insertions, 5 deletions
diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 8329fd1..c33461d 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -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) diff --git a/Objects/setobject.c b/Objects/setobject.c index d7190b5..88d5640 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -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); } |