FAQ
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

Search Discussions

  • Jr. King at Jun 7, 1999 at 5:05 pm
    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 at liam_toh.com
    ^hotmail... Must spoil spammers
  • Gordon McMillan at Jun 8, 1999 at 4:18 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 7, '99 at 4:01p
activeJun 8, '99 at 4:18p
posts3
users2
websitepython.org

2 users in discussion

Jr. King: 2 posts Gordon McMillan: 1 post

People

Translate

site design / logo © 2022 Grokbase