[pypy-dev] PyArray_Type cpyext bug

Matti Picus matti.picus at gmail.com
Sat Nov 23 20:55:53 CET 2013


Can we just say "don't do that?"
I guess the answer is no...
Going down the initialization route seems to be the way numpy does it, I see
  import_array();
used extensively in numpy c code.
Although making sure it is only called once seems to really complicate 
the header files, with API defines and strange macros.
Matti

On 11/22/2013 08:11 PM, Antonio Cuni wrote:
> Hi,
>
> I committed a cpyext+numpy failing test in a3c3b75a7f2b.
> In short, the following fails:
>
>     if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &obj))
>         return NULL;
>
> The problem is that PyArray_Type is currently defined in 
> ndarrayobject.c but never initialized:
>
>     PyTypeObject PyArray_Type;
>
> not a suprise that we get a segfault when we try to use it.
>
> However, I'm not sure about what is the best way to fix it, so I ask 
> the cpyext wizards :)
>
> I think that at the end what we want is an object for which 
> &PyArray_Type is equal to the PyObject* that we get when we pass 
> _numpypy.multiarray.ndarray to C.
>
> One possibility is to run the following code in some initialization 
> function:
>
>     static PyObject* _PyArray_Type;
>     #define PyArray_Type (*_PyArray_Type)
>
>     PyObject* np = PyImport_ImportModule("numpy");
>     if (!np)
>         return;
>
>     _PyArray_Type = PyObject_GetAttrString(np, "ndarray");
>     if (!My_PyArray_Type)
>         return;
>
>
> I'm sure there is a better way to do this. However, I tried to play 
> with the cpyext source for a while and didn't manage to find it. Any 
> suggestion?
>
> ciao,
> Anto
> _______________________________________________
> pypy-dev mailing list
> pypy-dev at python.org
> https://mail.python.org/mailman/listinfo/pypy-dev



More information about the pypy-dev mailing list