[Numpy-discussion] Missing NULL return checks?

Michael Abbott michael@araneidae.co...
Sat Jul 12 09:42:54 CDT 2008


> PyArray_DescrFromType can return NULL
Yah, you noticed ;)

> Yet it is unchecked in several places:
Pity about that.  Easy enough to fix though -- just don't lose track of 
ref counts.  In fact, I've already submitted a patch to this function (but 
not addressing this issue).

> static int
> PyArray_CanCastSafely(int fromtype, int totype)
> {
>     PyArray_Descr *from, *to;
>     register int felsize, telsize;
> 
>     if (fromtype == totype) return 1;
>     if (fromtype == PyArray_BOOL) return 1;
>     if (totype == PyArray_BOOL) return 0;
>     if (totype == PyArray_OBJECT || totype == PyArray_VOID) return 1;
>     if (fromtype == PyArray_OBJECT || fromtype == PyArray_VOID) return 0;
> 
>     from = PyArray_DescrFromType(fromtype);
      if (from == NULL)  return 0;

>     /*
>      * cancastto is a PyArray_NOTYPE terminated C-int-array of types that
>      * the data-type can be cast to safely.
>      */
>     if (from->f->cancastto) {
>         int *curtype;
>         curtype = from->f->cancastto;
>         while (*curtype != PyArray_NOTYPE) {
>             if (*curtype++ == totype) return 1;
>         }
>     }
>     if (PyTypeNum_ISUSERDEF(totype)) return 0;
> 
>     to = PyArray_DescrFromType(totype);
      if (to == NULL)  { Py_DECREF(from); return 0; }

>     telsize = to->elsize;
>     felsize = from->elsize;
>     Py_DECREF(from);
>     Py_DECREF(to);
>	... 
> }
> 
> Furthermore, the last function can fail, but doesn't seem to have an error
> return. What is the best way to go about cleaning this up?

Given the question the function is asking, returning false seems good 
enough for "failure".


More information about the Numpy-discussion mailing list