Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize :meth:`set.issuperset` for non-set argument.
39 changes: 23 additions & 16 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,14 +1378,7 @@ set_isdisjoint(PySetObject *so, PyObject *other)
return NULL;

while ((key = PyIter_Next(it)) != NULL) {
Py_hash_t hash = PyObject_Hash(key);

if (hash == -1) {
Py_DECREF(key);
Py_DECREF(it);
return NULL;
}
rv = set_contains_entry(so, key, hash);
rv = set_contains_key(so, key);
Py_DECREF(key);
if (rv < 0) {
Py_DECREF(it);
Expand Down Expand Up @@ -1769,17 +1762,31 @@ 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)) {
return set_issubset((PySetObject *)other, (PyObject *)so);
}

if (!PyAnySet_Check(other)) {
tmp = make_new_set(&PySet_Type, other);
if (tmp == NULL)
PyObject *key, *it = PyObject_GetIter(other);
if (it == NULL) {
return NULL;
}
while ((key = PyIter_Next(it)) != NULL) {
int rv = set_contains_key(so, key);
Py_DECREF(key);
if (rv < 0) {
Py_DECREF(it);
return NULL;
result = set_issuperset(so, tmp);
Py_DECREF(tmp);
return result;
}
if (!rv) {
Py_DECREF(it);
Py_RETURN_FALSE;
}
}
return set_issubset((PySetObject *)other, (PyObject *)so);
Py_DECREF(it);
if (PyErr_Occurred()) {
return NULL;
}
Py_RETURN_TRUE;
}

PyDoc_STRVAR(issuperset_doc, "Report whether this set contains another set.");
Expand Down