mirror of
https://github.com/python/cpython.git
synced 2025-10-22 22:53:06 +00:00
issubset() and issuperset() to work with general iterables
This commit is contained in:
parent
82d73dd459
commit
3fbec701ca
2 changed files with 19 additions and 5 deletions
|
@ -143,6 +143,10 @@ class TestJointOps(unittest.TestCase):
|
||||||
self.failIf(q <= r)
|
self.failIf(q <= r)
|
||||||
self.failIf(q > r)
|
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):
|
def test_pickling(self):
|
||||||
p = pickle.dumps(self.s)
|
p = pickle.dumps(self.s)
|
||||||
|
|
|
@ -588,11 +588,15 @@ set_ixor(PySetObject *so, PyObject *other)
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_issubset(PySetObject *so, PyObject *other)
|
set_issubset(PySetObject *so, PyObject *other)
|
||||||
{
|
{
|
||||||
PyObject *otherdata, *it, *item;
|
PyObject *otherdata, *it, *item, *tmp, *result;
|
||||||
|
|
||||||
if (!PyAnySet_Check(other)) {
|
if (!PyAnySet_Check(other)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
|
tmp = make_new_set(&PySet_Type, other);
|
||||||
|
if (tmp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
result = set_issubset(so, tmp);
|
||||||
|
Py_DECREF(tmp);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
if (set_len(so) > set_len((PySetObject *)other))
|
if (set_len(so) > set_len((PySetObject *)other))
|
||||||
Py_RETURN_FALSE;
|
Py_RETURN_FALSE;
|
||||||
|
@ -621,9 +625,15 @@ PyDoc_STRVAR(issubset_doc, "Report whether another set contains this set.");
|
||||||
static PyObject *
|
static PyObject *
|
||||||
set_issuperset(PySetObject *so, PyObject *other)
|
set_issuperset(PySetObject *so, PyObject *other)
|
||||||
{
|
{
|
||||||
|
PyObject *tmp, *result;
|
||||||
|
|
||||||
if (!PyAnySet_Check(other)) {
|
if (!PyAnySet_Check(other)) {
|
||||||
PyErr_SetString(PyExc_TypeError, "can only compare to a set");
|
tmp = make_new_set(&PySet_Type, other);
|
||||||
|
if (tmp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
result = set_issuperset(so, tmp);
|
||||||
|
Py_DECREF(tmp);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
return set_issubset((PySetObject *)other, (PyObject *)so);
|
return set_issubset((PySetObject *)other, (PyObject *)so);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue