Finding a python object in c++

B

bmatt

I am trying to figure out how to get a reference to a python object in
c++ so that I can call methods from c++. For example if I have the
following python code

class foo:
def func():
x = 1

f1 = foo()
f1.func()

How do I find f1 at get a Py_Object* to it so I can call the func
method?

Thanks
 
J

jepler

Access the 'im_self' attribute of the bound function object. In C,
I suppose this is something like
PyObject_GetAttrString(bound_m, 'im_self')
... def m(self): pass
...
f = F()
bound_m = f.m
dir(bound_m) ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self']
bound_m.im_self is f
1

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBSjStJd01MZaTXX0RAthXAJ9KUU3K2THVK8UEa/NnQFxOINTm0ACgqMUa
sYDRj5571IPHVoaMO4r535Q=
=Oygg
-----END PGP SIGNATURE-----
 
B

Brian Matt

Initially I was confused by your response because of my lack of
knowledge of how the Python interpreter works. I did a bit more
research and things are much more clear. My biggest problem was not
understanding how all defined objects are stored in the dictionary for
the imported module. Once I figured this out I was able to use the
following code to get f1 and call func.

//Assume python initialized and module imported
dict = PyModule_GetDict(module);
PyObject* pInst = PyDict_GetItemString(dict, "f1");
if(pInst) {
PyObject *pValue;
pValue = PyObject_CallMethod(pInst, "func", "");
if(pValue) {
Py_DECREF(pValue);
}
}

Note: this code doesn't handle error situations like making sure the
pInst variable is an instance object

Thanks,
BSMatt


Access the 'im_self' attribute of the bound function object. In C,
I suppose this is something like
PyObject_GetAttrString(bound_m, 'im_self')
... def m(self): pass
...
f = F()
bound_m = f.m
dir(bound_m) ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 'im_self']
bound_m.im_self is f
1
 
A

Alex Martelli

Brian Matt said:
//Assume python initialized and module imported
dict = PyModule_GetDict(module);
PyObject* pInst = PyDict_GetItemString(dict, "f1");

OK, but needlessly indirect. Doing just:

PyObject* pInst = PyObject_GetAttrString(module, "f1");

should, I think, be preferable.
if(pInst) {
PyObject *pValue;
pValue = PyObject_CallMethod(pInst, "func", "");
if(pValue) {
Py_DECREF(pValue);
}
}

Note: this code doesn't handle error situations like making sure the
pInst variable is an instance object

So much the better! If module.f1 has a func method that is callable
without arguments, why ever would you want to prohibit it from being
whatever type it wants to be? Maybe one day you'll want to recode the
whole type in C, and if you'd gone to the misplaced trouble of "making
sure [it] is an instance object" you'd just be creating headaches for
yourself...


Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top