[SciPy-dev] PyArray_FromDims and friends

Alan McIntyre alan.mcintyre at gmail.com
Mon Oct 6 04:54:03 EDT 2008


On Fri, Aug 15, 2008 at 10:26 AM, Zachary Pincus
<zachary.pincus at yale.edu> wrote:
>> No, no, you can't just cast the pointer to a different type, it
>> needs to point to npy_intp instead of int to start with, i.e., n has
>> to be an array of npy_intp.
>
> Thanks for looking that over. This is of course what happens when I
> try to deal with C too late at night.
>
> For cases where the dimension was 1, I had originally had:
>
> [whatever] = (PyArrayObject
> *)PyArray_SimpleNew(1,&(npy_intp)n,PyArray_[whatever]);
>
> But I got compiler warnings to the tune of "The argument to & is not
> strictly an lvalue. This will be a hard error in the future." Which
> makes sense, (npy_intp)n isn't a proper lvalue... So I changed it to:
>
> [whatever] = (PyArrayObject *)PyArray_SimpleNew(1,
> (npy_intp*)&n,PyArray_[whatever]);
>
> which, as you point out, is pretty daft.
>
> Is there a correct, portable, and warning-free way to do this in one
> line? I'm guessing not. Anyhow, I'll fix the patch with a multi-line
> solution:
> npy_int intp_n;
> ...
> intp_n = (npy_intp) n;
> [whatever] = (PyArrayObject
> *)PyArray_SimpleNew(1,&intp_n,PyArray_[whatever]);
>
> Sorry for the noise, and thanks Chuck for the catch.

I added a patch to #723 (http://scipy.org/scipy/scipy/ticket/723) that
(I think) switches to use PyArray_SimpleNew instead of
PyArray_FromDims.  If nobody sees any problems with it I'll look it
over again tomorrow or Tuesday and apply it.  I'll try to get to
switching out PyArray_FromDimsAndDataAndDescr later this week if
nobody else gets around to it.



More information about the SciPy-Dev mailing list