diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-04-06 16:57:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-06 16:57:13 (GMT) |
commit | a69a4a917c436579c2c4112081ea86a70f1f05d3 (patch) | |
tree | 9534b13c84aa6e3fcb67c22329e3f7167f1b0a24 /Objects | |
parent | 31cd25f4e17cd68487dc76c1b2ec162a646818c2 (diff) | |
download | cpython-a69a4a917c436579c2c4112081ea86a70f1f05d3.zip cpython-a69a4a917c436579c2c4112081ea86a70f1f05d3.tar.gz cpython-a69a4a917c436579c2c4112081ea86a70f1f05d3.tar.bz2 |
bpo-46721: Optimize set.issuperset() for non-set arguments (GH-31280)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/setobject.c | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/Objects/setobject.c b/Objects/setobject.c index 18dc49b..ef2190d 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1382,14 +1382,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); @@ -1773,17 +1766,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."); |