help with extending python

siddharthram at gmail.com siddharthram at gmail.com
Wed Jun 1 18:06:21 EDT 2005


Hello,

I'd appreciate some help figuring out what I might be doing wrong. I am
trying to extend Python to add some modules. However, when the code
appended below runs, the conversion always fails (returns -1), and I
get a TypeError

Thanks
Siddharth


So the usage is like this:
>>> import PyTest_itsFsm
>>> a = [(10,True)]
>>> PyTest_itsFsm.pytestprimitive(a)

the int is -1
bool is -1
in primitive test op with arg1 -1and arg 2183
1
>>>
TypeError: an integer is required
>>>



The PyMethodDef looks as below:
------------------------------
static PyMethodDef PyTest_itsFsmMethods[] = {
        {"pytestprimitive", PyTest_itsFsm_pytestprimitive,
METH_VARARGS, "pytestprimitive"},
        {NULL,NULL,0,NULL}
};

And the method I've added looks like this:
------------------------------------------
static PyObject * PyTest_itsFsm_pytestprimitive(PyObject * self,
PyObject *args) { int arg1;
        if
(!PyArg_ParseTuple(args,"O&",convert_pytestprimitive_arg1,&arg1))
        {
        return NULL;
        }
bool arg2;
        if
(!PyArg_ParseTuple(args,"O&",convert_pytestprimitive_arg2,&arg2))
        {
        return NULL;
        }


Finally, the convert functions:
---------------------------------

 PyTest_itsFsm::pytestpint convert_pytestprimitive_arg1 (PyObject * ob,
void * addr )
 {
  // this routine should convert the PyObject * to int for argument
arg1 int * p = (int *) addr;
 printf ("ready to parse in convert function\n");

*p = PyInt_AsLong (ob);
 printf ("the int is %d\n",*p);
if (PyErr_Occurred())
        return -1;

return 1;
}
int convert_pytestprimitive_arg2 (PyObject * ob, void * addr )
{
  // this routine should convert the PyObject * to bool for argument
arg2 bool * b = (bool *) addr;

int x;
x = PyInt_AsLong (ob);
printf ("bool is %d\n", (int) x);
if (PyErr_Occurred() )
        return -1;
*b =  (bool )PyBool_FromLong (x);
return 1;
}




More information about the Python-list mailing list