I want to check whether a memory view is initialized with None. I have a function like this:
cdef void test(int[:] a):
if a == NULL:
print("invalid data")
cdef int[:] memview = None
test(memview)
I get this error when I compile this:
Invalid types for '==' (int[:], void *)
Is the problem assigning None to the memmory view? If that's not the case, what can I do to detect the NULL values?
Thanks!
Turning my comments into a very answer:
You are assigning
Noneto your memoryview:Therefore it makes sense to check it against
None, not againstNULL:Ultimately they're similar concepts but different things:
NULLis a pointer that doesn't point to anything, whileNoneis a singleton Python object that indicates that something is unset. The distinction is really between C level and Python level and here memoryviews act "Python level".There's an additional detail that lets you do
if a is Noneinside anogilblock: Python ensures that there is exactly oneNoneobject in existence (and also that it remains the sameNoneobject for the entire program). Therefore it's possible to translatea is Noneto the C codei.e. a simple pointer comparison, that doesn't need the GIL since there's no changes to reference counts.