[Numpy-discussion] dtype comparison and hashing

Geoffrey Irving irving@naml...
Wed Oct 15 02:20:50 CDT 2008


Currently in numpy comparing dtypes for equality with == does an
internal PyArray_EquivTypes check, which means that the dtypes NPY_INT
and NPY_LONG compare as equal in python.  However, the hash function
for dtypes reduces id(), which is therefore inconsistent with ==.
Unfortunately I can't produce a python snippet showing this since I
don't know how to create a NPY_INT dtype in pure python.

Based on the source it looks like hash should raise a type error,
since tp_hash is null but tp_richcompare is not.  Does the following
snippet through an exception for others?

>>> import numpy
>>> hash(numpy.dtype('int'))

This might be the problem:

/* Macro to get the tp_richcompare field of a type if defined */
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
                         ? (t)->tp_richcompare : NULL)

I'm using the default Mac OS X 10.5 installation of python 2.5 and
numpy, so maybe those weren't compiled correctly.  Has anyone else
seen this issue?


More information about the Numpy-discussion mailing list