Search code examples
pythonnumpypython-c-api

Check if return value is instance of Py_None


I have to use a function (myAPI.readArr) that returns a scalar,numpy.ndarray or Py_None on failure.

This works for a failure:

data = myAPI.readArr(arg1, arg2)
if not data:
   raise Exception("Problem!")

but for valid arguments I get:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How can I check that the call succeeded or not?


Solution

  • data = myAPI.readArr(arg1, arg2)
    if data is None:
       raise Exception("Problem!")
    

    This answer contains more details about comparison to None