[C++-sig] Insert instance..... again

Beau Sapach beau.sapach at ualberta.ca
Tue Nov 7 23:46:46 CET 2006


Hello everyone,

 

I know it seems like I'm beating a dead horse here, but I'm not having much
luck in understanding the "proper" way to use some of the C API functions.
I want to insert an instance of one of my own objects into the python
environment, here's what I've got so far.

 

//get the main module

PyObject * pMainModule = PyImport_ImportModule("__main__");

 

//insert MyType into the main module      

Py_INCREF(MyType);

PyModule_AddObject(pMainModule,"MyObject",(PyObject*)&MyType);

 

//create an instance of mytype

PyObject * MyInstance = MakeMyObject();

 

//get the main module's dictionary

PyObject * pMainDict = PyModule_GetDict(pMainModule);

 

//insert my instance into the dictionary under the name 'testobject'

PyDict_SetItemString(pMainDict,"testobject",MyInstance);

 

//run a script that calls some function of my 'testobject' instance

PyRun_SimpleString("testobject.function()\n");

 

 

My object implements only a constructor, destructor and one function of its
own. The above code crashes somewhere in the python DLL but appears to be
trying to access something that's un-initialized.  Is there something I'm
doing wrong?  The above code works fine when I insert a PyObject integer
created using PyInt_FromLong(). Minus of course the unnecessary
PyModule_AddObject().  What's different about my object?  Is there some
functionality missing?  Should I be implementing some of the other functions
in the PyTypeObject?  Any help would be appreciated!


Beau

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20061107/6b507e92/attachment.htm>


More information about the Cplusplus-sig mailing list