[Numpy-discussion] Troubles with arrays

Wim Vanroose vanroose at ruca.ua.ac.be
Tue Jul 24 06:04:33 CDT 2001


Dear Numerical Python Users,

	I  have a small program that produces array's that  I want to import in Python.  The code is organised in three files. 1) arraytest.h, 2) arraytest.c 3) testmodule.c.  and the code is shown below.

	 The function myTest() produces the array and is called from the test module.

	 However, the program does not work! it crashes.  In my opinion, I do not have yet enough insight in Numerical Python. Is there an essential part that I do not understand.   

	Remarkable is that the pure Python equivalent of the program does not crash.

	Any ideas?

Wim Vanroose


/////////////////////////
//file: arraytest.h
//////////////////////

#include "Python.h"
#include "arrayobject.h"

PyObject *myTest(void);

//////////////////////
// file: arraytest.c
////////////////////

#include "arraytest.h"

PyObject * myTest(void ){
  PyArrayObject *result;
  double *datainput;
  int dimensions[1];
  int M=10;
  int MM;
  
  dimensions[0]=M;
  result = (PyArrayObject *)PyArray_FromDims(1,dimensions,PyArray_DOUBLE);
  datainput =(double *)result->data;
  for(MM=0;MM < M ; MM++){
    datainput[MM] = MM*0.1;
  }
  return PyArray_Return(result);
}
  
////////////////////
//file: test.c    CRASHES!!!!
////////////////
#include "arraytest.h"

static PyObject *function(PyObject *self, PyObject *args){
  return myTest();
}

static PyMethodDef testMethods[] = {
  {"test",function,1},
  {NULL,NULL}
};

extern "C" {
void  inittest(){
  PyObject *m,*d;
  m = Py_InitModule("test", testMethods);
  import_array();
  d = PyModule_GetDict(m);
} 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The Python Equivalent: DOES NOT CRASH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/////////////
// file: simpletest.h
/////////////////
#include "simpletest.h"


PyObject * myTest(void);
  
////////////////////
// file: test.c 
//////////////////////
#include "simpletest.h"

PyObject * myTest(void ){
  return Py_BuildValue("i",123);
}

////////////////
//file: test.c
//////////////////  
#include "simpletest.h"

static PyObject *function(PyObject *self, PyObject *args){
  return myTest();
}


static PyMethodDef testMethods[] = {
  {"test",function,1},
  {NULL,NULL}
};

extern "C" {
void  inittest(){
  PyObject *m,*d;
  m = Py_InitModule("test", testMethods);
  d = PyModule_GetDict(m);
} 












More information about the Numpy-discussion mailing list