[SciPy-user] Weave callbacks with return values - an example

Fernando Perez fperez at colorado.edu
Tue May 6 11:46:24 EDT 2003


This is just a followup on my previous message, not a question.  After Eric's 
reply, I needed my callback functions to return a value, and it turned out to 
be very simple to modify the code for that.

I don't know how many people use weave, but for the benefit of those who do 
(and for google's archive in case future users need this), here's the example 
code.

Cheers,

f.


#!/usr/bin/env python
from weave import inline

def foo(x,y):
     print "In Python's foo:"
     print 'x',x
     print 'y',y
     return x

def cfoo(x,y):
      code = """
      printf("Attemtping to call back foo() from C...\\n");
      py::tuple foo_args(2);
      py::object z;  // This will hold the return value of foo()
      foo_args[0] = x;
      foo_args[1] = y;
      z = foo.call(foo_args);
      printf("Exiting C code.\\n");
      return_val = z;
      """
      return inline(code,"foo x y".split() )

x=99
y="Hello"

print "Pure python..."
z=foo(x,y)
print "foo returned:",z
print "\nVia weave..."
z=cfoo(x,y)
print "cfoo returned:",z




More information about the SciPy-User mailing list