[SciPy-dev] Thoughts on weave improvements

Patrick Miller patmiller at llnl.gov
Tue Feb 12 17:27:17 CST 2002

Pearu Peterson wrote:
> 2) There is one issue. Fortran always expects that arguments are
> pointers. Is it possible that weave/pycod could generate the following
>   int pure_c_foo(int *a, int *b)
>   {
>       return *a + *b;
>   }
If our target is really to allow extensions to call underlying
C/FORTRAN (and it should!), then I would favor having weave.accelerate
build both.

long foo_as_C(long a, long b) { ... }
  return foo_as_C(*A,*B);
PyObject* foo_as_Python(PyObject* self, PyObject* args) { ...  }

There is an issue of mutability of arguments, but Python
(like C) doesn't allow changes to arguments, so it is
OK to make the call by reference there (and avoids aliasing
issues).  It make the function written in Python do the
expected Python thing.
> 3) Assume that issue 2) is solved. Then f2py should generate the following
> preferable if f2py extension modules would need _not_ to include any
> weave/pycod specific header files. For example, the Python equivalent for
> CheckAccelerated could be:

Here, one could use the Python interface directly.  I think you
would want to do something like:

PyObject* t = PyObject_CallMethod(py_fun,"as_FORTRAN","OO",PyInt_Type,PyInt_Type);
if ( !PyErr_Occurred() ) {
  PyObject* addr  = PyTuple_GetItem(t,0);
  int (*fun)(int*,int*) = PyInt_AsLong(addr); /* needs a cast */

No references at all to any weave header files.  Plus, CallMethod
fails quickly if there is no attribute.

This means that so long as we agree on an method API, ANYBODY
who want's to write accelerated functions can do so (even
other extension modules).  It even means that weave.accelerate
can directly query objects for their C source linkage if
that is part of the api

i.e. If the Python core did this, it might look like:

print math.sin.as_C_Source(FloatType)
("#include <math.h>", "sin(%s)", FloatType) # header code, call, return type

print math.sin.as_FORTRAN(FloatType)
( 2312823400, FloatType ) # Address, return type

print math.sin.as_C(FloatType)
( 2321823200, FloatType )

Patrick Miller | (925) 423-0309 | patmiller at llnl.gov

Big jobs usually go to the men who prove their ability to outgrow small 
ones. -- Ralph Waldo Emerson American writer and philosopher (1803-1882)

More information about the Scipy-dev mailing list