Mailing List Archive

more python to c/c++ questions.
Can you pass a user defined type in python to a c/c++ program. I didn't see
this in the docs and I would assume it isn't possible. I have the questions
commented inside my code

I snipped (=-)) this from my code...

pythond.py
class Cow:
i = 10
x = 0
str = "string"

testmeth.setCallback(setN) //fine
testmeth.testmeth(13,9) //fine
cow = Cow() //fine
testmeth.objpass(cow); //problem with this and the c code.

code.cpp
static PyObject* objPass(PyObject *self, PyObject* args){
PyObject *result = NULL;
PyObject *temp;

if (PyArg_ParseTuple(args, "O", &temp)) { /*this works ok*/
/*But how Am I supposed to
parseTuple the arg???*/
}
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}


Do I need to make a C++ class or struct that can map directly to the python
object ?

Thanx
more python to c/c++ questions. [ In reply to ]
Ok, I found some info on it but, it still doesn't work

I changed my c module to this...
<snip>
static PyObject* objPass(PyObject *self, PyObject* args){
PyObject *result = NULL;
PyObject *temp;

if (PyArg_ParseTuple(args, "O", &temp)) {
PyObject* xarg = Py_BuildValue("(s)","i");
xarg = PyObject_GetAttr(temp,xarg); // not getting anything but null
int val;
PyArg_ParseTuple(xarg,"i",&val);
}
Py_INCREF(Py_None);
result = Py_None;
return result;
}

Python module snippet

class Cow:
i = 10
x = 0
str = "string"
def genie(self):
print str

testmeth.setCallback(setN)
testmeth.testmeth(13,9)
cow = Cow()
testmeth.objpass(cow);

Thanks
jamarijr@liam_toh.com
^hotmail... Must spoil spammers
more python to c/c++ questions. [ In reply to ]
Jr. King is asking something or other:

> Ok, I found some info on it but, it still doesn't work
>
> I changed my c module to this...
> <snip>
> static PyObject* objPass(PyObject *self, PyObject* args){
> PyObject *result = NULL;
> PyObject *temp;
>
> if (PyArg_ParseTuple(args, "O", &temp)) {
> PyObject* xarg = Py_BuildValue("(s)","i");

You've just built a tuple. Probably not what you want.

> xarg = PyObject_GetAttr(temp,xarg); // not getting anything but
> null int val; PyArg_ParseTuple(xarg,"i",&val);
> }
> Py_INCREF(Py_None);
> result = Py_None;
> return result;
> }
>
> Python module snippet
>
> class Cow:
> i = 10
> x = 0
> str = "string"
> def genie(self):
> print str
>
> testmeth.setCallback(setN)
> testmeth.testmeth(13,9)
> cow = Cow()
> testmeth.objpass(cow);

- Gordon