Advertisement

Weird problem embedding python 3.2

Started by March 30, 2011 01:57 PM
0 comments, last by Kylotan 13 years, 8 months ago
I have currently embedded Python 3.2 into a C project. It works just fine in Linux, but not in Windows (visual studio). It will execute the script, and does not blow up, but also acts like nothing happened (no function calls are made in the script). I even tried loading the file into a char buffer and execution that (I verified the buffer was correctly filled), but still nothing.

#ifndef WIN32
FILE *pyFile = fopen(fileName, "r");
PyRun_SimpleFile(pyFile, fileName);
#else
FILE *fp = fopen(fileName,"r");
fseek(fp,0,SEEK_END);
long len = ftell(fp);;
fseek(fp,0,SEEK_SET);
char *buf = new char[len];
fread(buf,len,1,fp);
fclose(fp);
PyRun_SimpleString(buf);
delete buf;
#endif




For informational purposes, heres my module that works fine in Linux:
static PyObject* openwars_SetTitle(PyObject *self, PyObject *title)
{
char *newTitle;
if (!PyArg_ParseTuple(title, "s:SetTitle", &newTitle))
return NULL;

BOOST_FOREACH(IScriptCallback it, GlobalPythonScript->onSetTitle)
(*it)(boost::any(newTitle));

Py_RETURN_NONE;
};

static PyMethodDef openwars_Methods[] = {
{"SetTitle", openwars_SetTitle, METH_VARARGS,
"Sets the title of the main window."},
{NULL, NULL, 0, NULL}
};

static PyModuleDef openwars_Module = {
PyModuleDef_HEAD_INIT, "openwars", NULL, -1, openwars_Methods,
NULL, NULL, NULL, NULL
};



I have placed a breakpoint in the method, and it is never hit.

Yes python has been initialized, and the logic to add my embedded module has been executed properly. Also, I downloaded python 3.2 and installed it from the website, and used the supplied library and header files straight from the installed directories.

Any ideas on what would cause this?
You're not checking the return value of [font="Courier New"]fopen[/font], nor the return value of PyRun_SimpleFile[font="Arial"]. There's a [/font]90% chance the problem is with the first one. Hint: the working directory when you run via Visual Studio is rarely the directory where the actual executable is, unless you explicitly set it to be that way.

This topic is closed to new replies.

Advertisement