Num Array problem with Embedding python in C

Fernando Perez fperez528 at yahoo.com
Tue Jun 15 15:41:52 EDT 2004


youngdubliner at hotmail.com wrote:

> I'm having a problem ........
> 
> I've stripped all my code to help isolate the problem.
> 
> Its seems to be with importing numarray when python is embedded in C.
> 
> I have a simple C program it Opens Python imports a script and
> then Closes Python
> 
> like so .......
> 
> int main(int argc,char *argv[])
> {
> 
>  int count = 0;
>  PyObject *pmod;
> 
>  while ( count < 10)
>  {
>   
>   /*
>   ** Initialize Python
>   */
>     
>   Py_Initialize();
>   printf("\nOpen Python ---- >");
>   
>   pmod   = PyImport_ImportModule("test");
>   
>   Py_DECREF(pmod);
> 
>   /*
>   ** Close Python
>   */
>   
>   Py_Finalize();
>   printf("\n<---- Closed Python");
> 
> 
>    /*
>    ** Do this 10 times !
>    */
>    count++;
>     
>  }
>    getchar();
>    return(0);
>    
> }
> 
> and thats it !
> 
> The script it calls  is test.py its very simple , it imports numarray
> and then returns. like so .......
> 
> -----------------------
> test.py
> -----------------------
> #Imports
> from numarray import *
> 
> def test_func():
>     return (99)
> -----------------------
> end of file
> -----------------------
> 
> 
> The first time it is called from my C code
> it all works fine ! ( but only the first time ! )
> 
> In the second iteration above test .c crashes

From: http://www.pfdubois.com/numpy/html2/numpy-13.html

*************************************************************************
The Numeric installation process installed arrayobject.h in a subdirectory
Numeric in your Python include path, so you should include it this way:

#include "Numeric/arrayobject.h"
 

Is your C extension using Numeric blowing up? Maybe you didn't call
import_array(). If the extension is not in a single file, also define
PY_ARRAY_UNIQUE_SYMBOL.
 

In addition to including arrayobject.h , the extension must call import_array()
in its initialization function, after the call to Py_InitModule() . This call
makes sure that the module which implements the array type has been imported,
and initializes a pointer array through which the NumPy functions are called.
If you forget this call, your extension module will crash on the first call to
a NumPy function.

*************************************************************************

This referst to Numeric, but I'm sure the same issue exists with Numarray (I
still use Numeric, so I don't know the details with numarray).

cheers,

f



More information about the Python-list mailing list