Core and builtins
-----------------
+- Bug #1456209: In some obscure cases it was possible for a class with a
+ custom ``__eq__()`` method to confuse set internals when class instances
+ were used as a set's elements and the ``__eq__()`` method mutated the set.
+
+- Eliminated unnecessary repeated calls to hash() by set.intersection() and
+ set.symmetric_difference_update().
+
- Bug #1591996: Correctly forward exception in instance_contains().
- Bug #1588287: fix invalid assertion for `1,2` in debug builds.
- Bug #1576657: when setting a KeyError for a tuple key, make sure that
- the tuple isn't used as the "exception arguments tuple".
+ the tuple isn't used as the "exception arguments tuple". Applied to
+ both sets and dictionaries.
- Bug #1565514, SystemError not raised on too many nested blocks.
#include "Python.h"
#include "structmember.h"
+/* Set a key error with the specified argument, wrapping it in a
+ * tuple automatically so that tuple keys are not unpacked as the
+ * exception arguments. */
+static void
+set_key_error(PyObject *arg)
+{
+ PyObject *tup;
+ tup = PyTuple_Pack(1, arg);
+ if (!tup)
+ return; /* caller will expect error to be set anyway */
+ PyErr_SetObject(PyExc_KeyError, tup);
+ Py_DECREF(tup);
+}
+
/* This must be >= 1. */
#define PERTURB_SHIFT 5
/*
Internal routine to insert a new key into the table.
-Used both by the internal resize routine and by the public insert routine.
+Used by the public insert routine.
Eats a reference to key.
*/
static int
return 0;
}
+/*
+Internal routine used by set_table_resize() to insert an item which is
+known to be absent from the set. This routine also assumes that
+the set contains no deleted entries. Besides the performance benefit,
+using set_insert_clean() in set_table_resize() is dangerous (SF bug #1456209).
+Note that no refcounts are changed by this routine; if needed, the caller
+is responsible for incref'ing `key`.
+*/
+static void
+set_insert_clean(register PySetObject *so, PyObject *key, long hash)
+{
+ register size_t i;
+ register size_t perturb;
+ register size_t mask = (size_t)so->mask;
+ setentry *table = so->table;
+ register setentry *entry;
+
+ i = hash & mask;
+ entry = &table[i];
+ for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
+ i = (i << 2) + i + perturb + 1;
+ entry = &table[i & mask];
+ }
+ so->fill++;
+ entry->key = key;
+ entry->hash = hash;
+ so->used++;
+}
+
/*
Restructure the table by allocating a new table and reinserting all
keys again. When entries have been deleted, the new table may
} else {
/* ACTIVE */
--i;
- if(set_insert_key(so, entry->key, entry->hash) == -1) {
- if (is_oldtable_malloced)
- PyMem_DEL(oldtable);
- return -1;
- }
+ set_insert_clean(so, entry->key, entry->hash);
}
}
}
while ((key = PyIter_Next(it)) != NULL) {
- int rv = set_contains_key(so, key);
+ int rv;
+ setentry entry;
+ long hash = PyObject_Hash(key);
+
+ if (hash == -1) {
+ Py_DECREF(it);
+ Py_DECREF(result);
+ Py_DECREF(key);
+ return NULL;
+ }
+ entry.hash = hash;
+ entry.key = key;
+ rv = set_contains_entry(so, &entry);
if (rv == -1) {
Py_DECREF(it);
Py_DECREF(result);
return NULL;
}
if (rv) {
- if (set_add_key(result, key) == -1) {
+ if (set_add_entry(result, &entry) == -1) {
Py_DECREF(it);
Py_DECREF(result);
Py_DECREF(key);
PyObject *value;
int rv;
while (PyDict_Next(other, &pos, &key, &value)) {
- rv = set_discard_key(so, key);
+ setentry an_entry;
+ long hash = PyObject_Hash(key);
+
+ if (hash == -1)
+ return NULL;
+ an_entry.hash = hash;
+ an_entry.key = key;
+ rv = set_discard_entry(so, &an_entry);
if (rv == -1)
return NULL;
if (rv == DISCARD_NOTFOUND) {
- if (set_add_key(so, key) == -1)
+ if (set_add_entry(so, &an_entry) == -1)
return NULL;
}
}
Py_DECREF(tmpkey);
return result;
} else if (rv == DISCARD_NOTFOUND) {
- PyErr_SetObject(PyExc_KeyError, key);
+ set_key_error(key);
return NULL;
}
Py_RETURN_NONE;