[Python-Dev] PySet API (original) (raw)

Raymond Hettinger raymond.hettinger at verizon.net
Sun Mar 26 17:38:30 CEST 2006


[Alex]

And I'm on the fence regarding the specific issue of PySetNext.

So, having carefully staked out a position smack in the middle, I cheerfully now expect to be fired upon from both sides!-)

Okay, here's the first cheap shot ;-) Which of the following pieces of code is preferable? The first loops with the iterator protocol and the second loops with the _next protocol.

static long frozenset_hash(PyObject *self) { PySetObject *so = (PySetObject *)self; long h, hash = 0; PyObject *it, *key;

    if (so->hash != -1)
            return so->hash;

    it = PyObject_GetIter(self);
    if (it == NULL)
            return -1;

    while ((key = PyIter_Next(it)) != NULL) {
            h = PyObject_Hash(key);
            Py_DECREF(key);
            if (h == -1) {
                    Py_DECREF(it);
                    return -1;
            }
            hash ^= h * 3644798167;
    }
    Py_DECREF(it);
    if (PyErr_Occurred())
            return -1;

    if (hash == -1)
            hash = 590923713L;
    so->hash = hash;
    return hash;

}

static long frozenset_hash(PyObject *self) { PySetObject *so = (PySetObject *)self; long h, hash = 0; PyObject *key; Py_ssize_t pos = 0;

    if (so->hash != -1)
            return so->hash;

    while (set_next(so, &pos, &key)) {
            h = PyObject_Hash(key);
            if (h == -1) {
                return -1;
            }
            hash ^= h * 3644798167;
    }

    if (hash == -1)
            hash = 590923713L;
    so->hash = hash;
    return hash;

}



More information about the Python-Dev mailing list