trouble embedding python

TheDustbustr thedustbustr at aol.com
Thu Jun 5 21:15:32 EDT 2003


*** BEGIN script.cpp ***
class Script
{
public:
	PyObject* pName;
	PyObject* pModule;
	PyObject* pDict;
};

int Script_LoadScript(Script& s, char* module)
{
	s.pName =   PyString_FromString(module);
	s.pModule = PyImport_Import(s.pName);
	if (!s.pModule) {
		printf("Script_LoadScript> pModule failed\n");
		return false; }

	s.pDict = PyModule_GetDict(s.pModule);
	if (!s.pDict) {
		printf("Script_LoadScript> pDict failed\n");
		return false; }

	return true;
}
*** END script.cpp ***


*** BEGIN main() ***
...
Script_LoadScript(script, "pobj");
result=PyDict_GetItemString(script.pDict, "Human.str");
if (!result) {printf("fatal: str is null\n");}
attr=PyInt_AsLong(result);
attributes[0]=attr;
...
*** BEGIN main() ***


*** BEGIN pobj.py ***
class Human:
    str=10
    dex=10
    con=10
    int=10
    wis=10
    chr=10
*** END pobj.py ***

I want to be able to access Human and all attributes from C++.  What's wrong
with what I have (when run, result evaluates to false and attributes[0] is -1).
 If it matters, a call to PyErr_Print() crashes python, but I may be using it
wrong.

Thanks, Dustin




More information about the Python-list mailing list