[Numpy-discussion] import_array weird behavior

Robert Kern robert.kern at gmail.com
Tue Dec 20 12:02:09 EST 2011


On Tue, Dec 20, 2011 at 10:31, Marc POINOT <Marc.Poinot at onera.fr> wrote:
>
> Hi all,
>
> I've just changed to cython and old numpy module with a raw C API.
> The C module init is removed, and I've put the import_array in the 'pure-cython'
> part of the module init. Usual tutorial examples have these lines:
>
> import  numpy as NPY
> cimport numpy as NPY
>
> NPY.import_array()
>
> But this fails (first numpy API call core dump) in my module, if I put back the
> import_array() in the C part of the module everything turns ok again.
> Now if I remove again this C API import_array and I write:
>
> import  numpy as NPY
> cimport numpy as CNPY
>
> CNPY.import_array()
>
> all is ok.
> Do I miss something? It sounds good to me to have to separate scopes but
> I wonder if it's a common practice with potential side-effects or if it is
> the right way to use numpy with cython.

Yes, it is the right way to do it. Cython can *mostly* tell from
context whether to resolve NPY.<whatever> from either the C side or
the Python side, but sometimes it's ambiguous. It's more often
ambiguous to the human reader, too, so I try to be explicit about it.
I don't really know why the tutorials do it the confusing way.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list