469,328 Members | 1,266 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

help with extending python

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_pytestprimiti ve_arg1,&arg1))
{
return NULL;
}
bool arg2;
if
(!PyArg_ParseTuple(args,"O&",convert_pytestprimiti ve_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;
}

Jul 19 '05 #1
0 1069

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eli Daniel | last post: by
1 post views Thread by Tommy Nordgren | last post: by
1 post views Thread by Richard Townsend | last post: by
reply views Thread by Terry Tang | last post: by
3 posts views Thread by Redefined Horizons | last post: by
7 posts views Thread by Maximus Decimus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.