[Numpy-discussion] Converting None to NULL using ndpointer

Lutz Maibaum lutz.maibaum at gmail.com
Tue May 18 13:43:15 EDT 2010


Hello,

I am trying to use a C library function from Python using numpy and
ctypes. Assume the function has the signature

void foo(int* bar)

so it usually takes an integer array as a parameter. In python, I define

foo.argtypes=[ndpointer(dtype="intc",flags="C_CONTIGUOUS")]

and can then call the function foo(x), where x is an appropriate
ndarray. This works very well.

The problem is that I need to be able to call the function foo with a
NULL pointer as an argument. Ideally, I would like to be able to use
foo(None), but that doesn't work because the argument conversion
throws a TypeError. Therefore my questions:

1. Is there a ndarray that is converted to NULL by ndpointer? I was
hoping that maybe ndarray(None) or an array of size 0 would work, but
they don't.

2. Is there a way to tell ndpointer that None is an acceptable
parameter, which should be converted to NULL?

3. I could also change the argument specification in python to
foo.argtypes=[POINTER(c_int)], but then I would lose the type checking
features of ndpointer. Can I invoke the ndpointer conversion manually?
For example, does a ndpointer object have a method that takes a
ndarray as an argument and returns a POINTER(c_int) if appropriate?

Any suggestions would be much appreciated.

Thanks,

  Lutz



More information about the NumPy-Discussion mailing list