[pypy-dev] hash negotiation

Christian Tismer tismer at tismer.com
Mon Mar 3 18:11:32 CET 2003


Hi Armin,
...

>>It may negotiable whether we want to preserve an extra flag
>>in all the hashable objects whether the hash has been computed,
>>or repeat CPython's way to preserve a single value for this.
> 
> This abuse of -1 was for error passing, and we don't need it, so I thought it 
> should just go away. My point of view is definitely that any "C hack" should 
> go away.

Oh then I misunderstood the code. I thought that -1
was always for "not yet computed".
Now I understand:

long
PyObject_Hash(PyObject *v)
{
	PyTypeObject *tp = v->ob_type;
	if (tp->tp_hash != NULL)
		return (*tp->tp_hash)(v);
	if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
		return _Py_HashPointer(v); /* Use address as hash value */
	}
	/* If there's a cmp but no hash defined, the object can't be hashed */
	PyErr_SetString(PyExc_TypeError, "unhashable type");
	return -1;
}

This is an error flag!

> For objects like strings that internally use -1 for yet another usage ("not 
> computed yet"), our W_StringObjects are free to repeat that and never actually 
> return a value of -1. That's fine.

So we basically agreed.

Just as a not, about what I just learned by reading all
of the hash stuff: All number hashes are required to
return the same hash value for the same numerical
value. hash(1) == hash(1L) == hash(1.0) :-)

> Regarding CPython compatibility, that can be added later if needed by keeping 
> an explicit track of this kind of changes (hence the XXX in my comments -- but 
> we certainly need a better way to sort this kind of comments). It might be as 
> simple as calling our own hash functions, and then, if that returns -1, 
> translate it into -2 (as this is how all CPython objects work around this 
> special value).

Agreed.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  pager +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/



More information about the Pypy-dev mailing list