calldll and floats/doubles (newbie q)

Thomas S. Strinnhed thstr at serop.abb.se
Mon Jun 7 08:26:10 EDT 1999


Hi

If you've got the module up'n working this should be easy.
(At least I think so, I leave it to the wizzes to correct me if
I'm wrong :-)

The calling from python is straight forward:
>>> import spam
>>> x = spam.func(3.14159, 0.0548)

The C-code for the spammodule containing function func() could be like
this (in say spammodule.c):

/* File spammodule.c
 * Almost the same as example in 
 * "Extending and Embedding the Python Interpreter"
 * which has a lot more detail.
 */
#include "Python.h"

/* Declare functions */
static PyObject * spam_func(PyObject *self, PyObject *args);
// ... more functions

/* A method lookup table */
static PyMethodDef spam_methods[] = 
{
   {"func", spam_func, 1},
   // ... more functions
   {NULL, NULL}
};

/* Init function called on module load-time */
void _declspec(dllexport) initspam(void)
{
   (void) Py_InitModule("spam", spam_methods);
}


/* Define functions */
static PyObject * spam_func(PyObject *self, PyObject *args)
{
   double d_val, d_val2;
   PyObject *ret_val;

	/* Extract argument(s) from args) 
         * d is for double, f for float
         */
   if(!PyArg_ParseTuple(args, "dd", &d_val, &d_val2)
      return NULL;  /* Python raises exception upon NULL-value */

	/* Do something useful */
   d_val = d_val / d_val2;

	/* Convert values into PyObject, reverse of Parse_Tuple */
   ret_val = Py_BuildValue("d", d_val);
   return ret_val;
}


Good Luck 
-- Thomas

Wesley Phoa wrote:
> 
> I'd like to write a small library of option pricing functions in C, and test
> it from Python using calldll or windll. Does anyone know how to invoke
> functions that take float or double arguments and return float or double
> results?
> 
> Wesley Phoa.




More information about the Python-list mailing list