[C++-sig] Insert instance..... again - solved
Beau Sapach
beau.sapach at ualberta.ca
Wed Nov 8 17:21:28 CET 2006
Hello everyone,
Boy I feel sheepish, but I think I've found my problem. In my PyTypeObject
I had set tp_basicsize to sizeof(C++_ClassName) instead of the
sizeof(Python_struct) where Python_struct is my
"typedef struct { PyObject_HEAD ..." that wraps the C++ object. The code I
posted below seems to work just fine now.
Beau
From: Beau Sapach [mailto:beau.sapach at ualberta.ca]
Sent: Tuesday, November 07, 2006 3:47 PM
To: 'Development of Python/C++ integration'
Subject: Insert instance..... again
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/20061108/cdffda88/attachment.htm>
More information about the Cplusplus-sig
mailing list