=== Bill Spotz, wrapping code with SWIG === - Hand-wrapped extension: /* Must include Python.h before any standard headers! */ #include "Python.h" #include "fact.h" /* Define the wrapper functions exposed to Python (must be static) */ static PyObject* wrap_fact(PyObject*self, PyObject*args) { intn, result; /* Python->C Conversion */ if (!PyArg_ParseTuple(args, "i", &n)) return NULL; /* Call our function */ result = fact(n); /* C->Python Conversion */ return Py_BuildValue("i", result); } /* Method table declaring the names of functions exposed to Python */ static PyMethodDefExampleMethods[] = { {"fact", wrap_fact, METH_VARARGS, "Calculate the factorial of n"}, {NULL, NULL, 0, NULL} /* Sentinel */ }; /* Module initialization function called at "import example" */ PyMODINIT_FUNCinitexample(void) { { (void) Py_InitModule("example", ExampleMethods); } - See also the wrap.pdf handout - With SWIG: - Define docstring for module: %define %graph_doc " Provide a Graph class for describing a network or sparse matrix structure. " %enddef %module (docstring = %graph_doc) Graph - Or presumably do it inline... - Unwrappable methods: %ignore operator<<(std::ostream&, const Graph &); - Add C++ functionality %extend { } - Return STL string as python string %include "std_string.i" - Using %typemap(out) and %apply to replace integer return code with exception see Graph4.i - !!!numpy-1.0/numpy/doc/swig/numpy.i has full set of numpy typemaps!!! with correct memory management! See Graph5.i