On Wed, May 20, 2009 at 6:47 PM, Robert Bradshaw
<[email protected]> wrote:
> According to the Python/C API, returning -1 from __hash__ is the way
> to signal an error. Of course, if one returns -1 manually one gets a
> runtime error that an error was raised without setting the exception.
> We could document this, but it's still kind of obscure.
>
> In Python one can do
>
> sage: class A:
> ....:     def __hash__(self):
> ....:         return int(-1)
> ....:
> sage: hash(A())
> -2
>
> Should we just check for -1 and return -2 for cdef class __hash__
> functions as well?
>

IMHO, yes. Cython should do something similar to what core CPyhton
does in typeobject.c... see this

static long
slot_tp_hash(PyObject *self)
{
        PyObject *func;
        static PyObject *hash_str, *eq_str, *cmp_str;
        long h;

        func = lookup_method(self, "__hash__", &hash_str);
<.....>
        if (h == -1 && !PyErr_Occurred())
                h = -2;
        return h;
}


-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to