[Numpy-discussion] failure to register ufunc loops for user defined types

Geoffrey Irving irving at naml.us
Sat Dec 3 22:14:11 EST 2011


I'm trying to add a fixed precision rational number dtype to numpy,
and am running into an issue trying to register ufunc loops.  The code
in question looks like

    int npy_rational = PyArray_RegisterDataType(&rational_descr);
    PyObject* equal = ... // extract equal object from the imported numpy module
    int types[3] = {npy_rational,npy_rational,NPY_BOOL};
    if (PyUFunc_RegisterLoopForType((PyUFuncObject*)ufunc,npy_rational,rational_ufunc_##name,_types,0)<0)
        return 0;

In Python 2.6.7 with the latest numpy from git, I get

    >>> from rational import *
    >>> i = array([rational(5,3)])
    >>> i
    array([5/3], dtype=rational)
    >>> equal(i,i)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: ufunc 'equal' not supported for the input types, and
the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''

The same thing happens with (rational,rational)->rational ufuncs like multiply.

The full extension module code is here:


I realize this isn't much information to go on, but let me know if
anything comes to mind in terms of possible reasons or further tests
to run.  Unfortunately it looks like the ufunc ntypes and types
properties aren't updated based on user-defined loops, so I'm not yet
sure if the problem is in registry or resolution.

It's also possible someone else hit this before:


More information about the NumPy-Discussion mailing list