[Numpy-discussion] Numarray header PEP

Gerard Vermeulen gerard.vermeulen at grenoble.cnrs.fr
Tue Jun 29 10:46:02 CDT 2004


> 
> The PEP is attached.  It is formatted using the docutils package which
> can be used to generate HTML or PDF.  Comments and corrections would be
> appreciated.
> 
PyQwt is a Python extension which can be conditionally compiled against
Numeric and/or numarray (both, one of them or none).

Your PEP excludes importing of Numeric and numarray in the same C-extension.

All you need to do is to hide the macros PyArray_Present(), PyArray_isArray()
and import_array() into a few functions with numarray specific names, so
that the following becomes possible:

#include <Numeric/meta.h>
/* defines the functions (no macros)
   int Numeric_Present();
   int Numeric_isArray();
   void import_numeric();
   to hide the Numeric C-API stuff in a small Numeric/meta.c file.
*/
#include <numarray/meta.h>
/* defines the functions (no macros)
   int numarray_Present();
   int numarray_isArray();
   void import_numarray();
   to hide the numarray C-API stuff in a small numarray/meta.c file.
*/

PyObject *
some_array_returning_function(PyObject *m, PyObject *args)
{
    int param;
    PyObject *result;
    
    if (!PyArg_ParseTuple(args, "i", &param))
	return NULL;
    
    if (Numeric_Present()) {
	result = numeric_returning_function(param);
    } else if (Numarray_Present()) {
	result = numarray_returning_function(param);
    } else {
	result = list_returning_function(param);
    }
    return result;
}

PyObject *
some_array_accepting_function(PyObject *m, PyObject *args)
{
    PyObject *sequence, *result;
    
    if (!PyArg_ParseTuple(args, "O", &sequence))
	return NULL;
    
    if (Numeric_isArray(sequence)) {
	result = numeric_input_function(sequence);
    } else if (Numarray_isArray(sequence)) {
	result = numarray_input_function(sequence);
    } else {
	result = sequence_input_function(sequence);
    }
    return result;
}

/*
  the code for the numeric_whatever_functions and for the
  numarray_whatever_functions should be source files.
 */

static void
initfoo(void)
{
    PyObject *m = Py_InitModule3(
	"foo",
	_foo_functions,
	_foo__doc__);
    if (m == NULL) return;
    import_numeric();
    import_numarray();
}       

Regards -- Gerard








More information about the Numpy-discussion mailing list