[Numpy-discussion] Trouble with extension using NumPy on the Mac

Chris Barker cbarker at jps.net
Wed Nov 22 17:02:30 CST 2000


Hi all,

I'm cross posting this to the NumPy List and the MacPython list, because
it involves NumPy on the Mac, so I'm not sure which group can be most
helpfull.


It took me a while to get this far, and I finally got everything to
compile, but now I have a crashing problem.

It seems to be a problem with PyArray_Type not being seen as a PyObject.
I am pretty new to C, but I have consulted with a number of folks I work
with that know a lot more than I do, and this seems to be a pretty
esoteric C issue. It also seems to be compiler dependent, because I have
all this working fine on Linux with gcc.

I have chopped my problem down into a very small function that
demonstrates the problem.

The function takes a contiguous NumPy array of Floats (doubles) and
multiplies every element by two (in place), and returns None. Here is
the code as it works on Linux:



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


/* A function that doubles an array of Floats in place*/
static PyObject * minimal_doublearray(PyObject *self, PyObject *args)
{
  PyArrayObject *array;
  
  int i, num_elements;

  double *data_array ;


  if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &array))
	return NULL;
  
  if (array->descr->type_num != PyArray_DOUBLE) 
	{
	  PyErr_SetString(PyExc_ValueError,
					  "array must be of type Float");
	  return NULL;
	}

  data_array = (double *) array->data;

  /*num_elements = PyArray_Size((PyObject *) array);*/
  num_elements = PyArray_Size(array);

  printf("The size of the array is: %i elements\n",num_elements);


  for (i= 0; i < num_elements; i++)
	data_array[i] = 2 * data_array[i];

  return Py_None;

}

	
static PyMethodDef minimalMethods[] = {
  {"doublearray",    minimal_doublearray,  METH_VARARGS},
  {NULL,      NULL}        /* Sentinel */
};

void initminimal()
{
  (void) Py_InitModule("minimal", minimalMethods);
}


Note that the call to: "PyArray_Size(array)" gives me a 
"./minimal.c:28: warning: passing arg 1 from incompatible pointer type "
on gcc on linux. In CodeWarrior on the Mac, it is an fatal error. With
the typcast (see previous commented out line) it gives no warnings, and
compiles on both systems. Here is a small script to test it:

#!/usr/bin/env python
from Numeric import *

import minimal


print "\nTesting doublearray"
a = arange(10.0)
print a
minimal.doublearray(a)
print a
print "the second version should be doubled"

This works fine on Linux, and gives the appropriate errors if the wrong
type object is passed in.

On the Mac, it crashes.

Trying various things, I found that it crashes on the 
 if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &array))
	return NULL;
line. If I use:
 if (!PyArg_ParseTuple(args, "O", &array))
	return NULL;

It works.

Then it crashes on the:

num_elements = PyArray_Size((PyObject *) array);

line. If I use another way to determine the number of elements, like:

num_elements = 1;
for (i=0,i<array->nd,i++)
	num_elements = num_elements * array->dimensions[1];

Then I can get it to work.

What is going on? anyone have any suggestions?

MacPython 1.5.2c
The NumPy that came with MacPython 1.5.2c
CodeWarrior pro 5.

Thanks for any suggestions,

-Chris




-- 
Christopher Barker,
Ph.D.                                                           
cbarker at jps.net                      ---           ---           ---
http://www.jps.net/cbarker          -----@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Water Resources Engineering       ------   @    ------   @   ------   @
Coastal and Fluvial Hydrodynamics -------      ---------     --------    
------------------------------------------------------------------------
------------------------------------------------------------------------



More information about the Numpy-discussion mailing list