[Numpy-discussion] Troubles with arrays

Paul F. Dubois paul at pfdubois.com
Fri Jul 27 14:03:12 CDT 2001


I don't know if someone helped you with this while I was on travel, but in
case they didn't:

I'm pretty sure your problem is that myTest is not in the same file as the
import_array; when it tries to access the Numeric C API, it will be
dereferencing zero. For extensions that are not in one file, a special
techniques is required. Recently an improvement was added to make this
easier. I believe you should add this to your header file, above the include
of arrayobject.h:

#define PY_ARRAY_UNIQUE_SYMBOL xxxx

where xxxx is any name you choose that won't conflict with your other stuff.

I hope that if I have this wrong somebody will correct me.

-----Original Message-----
From: numpy-discussion-admin at lists.sourceforge.net
[mailto:numpy-discussion-admin at lists.sourceforge.net]On Behalf Of Wim
Vanroose
Sent: Tuesday, July 24, 2001 4:05 AM
To: numpy-discussion at lists.sourceforge.net
Subject: [Numpy-discussion] Troubles with arrays


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);
}









_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/numpy-discussion





More information about the Numpy-discussion mailing list