Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags?

Thread Tools

Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags?

Carl Douglas
Posts: n/a
Hi Python fans,

I am developing a DLL that is loaded by a host application on windows.
I'm using python 2.5.

My DLL uses an embedded python interpreter which can access the host
application through an API which I have exposed using SWIG 1.3.31.

Therefore I have both extended and embedded Python at once: the SWIG
generated code is statically linked into my DLL, and one of the
functions in my DLL executes PyRun_SimpleFile.

Using python25_d.dll, I stepped through some python code to understand
what is happening, and it appears that PyRun_SimpleFile is returning
-1 because python cannot create the module __main__.

Here's the code from pythonrun.c:

PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
PyCompilerFlags *flags)
PyObject *m, *d, *v;
const char *ext;

m = PyImport_AddModule("__main__");
if (m == NULL)
return -1;

BTW, PyImport_AddModule fails because deeper down a dictionary check fails:

if (!PyDict_Check(op))
return NULL;

Can someone suggest what I need to do to get this to work?

Here are the relevant lines from my code:

if (GetOpenFileName(&ofn)==TRUE)
init_mymodule(); // SWIG generated method

PyObject* PyFileObject = PyFile_FromString(ofn.lpstrFile, "r");

if (PyRun_SimpleFile(PyFile_AsFile(PyFileObject), ofn.lpstrFile)==-1)
MessageBox(NULL, "error running script", "Python", MB_ICONERROR);



Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Embedded and extending python 2.5: trouble with __main__ inPyRun_SimpleFileExFlags? Carl Douglas Python 0 03-05-2007 01:51 AM
Extending Embedded Python and execute external script Vertilka Python 1 01-04-2007 01:02 PM
Extending embedded Python: Adding single methods Torsten Bronger Python 3 03-09-2006 07:24 AM
Extending Embedded Python Richard Townsend Python 1 09-25-2005 09:01 AM
import __main__ where can i find a module called ""? wen Python 2 08-21-2005 02:09 PM