PyRun_SimpleFile() crashes

L

lixinyi.23

my code:
main.cpp
#include <Python.h>

int main(int argc, char **argv)
{
Py_Initialize();

FILE *file_1 = fopen("a2l_reader.py","r+");
PyRun_SimpleFile(file_1,"a2l_reader.py");

Py_Finalize();
}

compile under windows using MinGW:
g++ main.cpp libpython25.a -o a

no error was found. But when I run a.exe the program just crashes.

What should I do?
 
L

lixinyi.23

my code:
main.cpp
#include <Python.h>

int main(int argc, char **argv)
{
Py_Initialize();

FILE *file_1 = fopen("a2l_reader.py","r+");
PyRun_SimpleFile(file_1,"a2l_reader.py");

Py_Finalize();

}

compile under windows using MinGW:
g++ main.cpp libpython25.a -o a

no error was found. But when I run a.exe the program just crashes.

What should I do?


PyRun_File and PyRun_SimpleFile would just crash my application.
but PyRun_SimpleString works fine.

But why???
 
A

Aaron \Castironpi\ Brady

Run it under gdb (which should have come with MinGW).

Check that you actually opened the file, ie file_1 != 0.

This might be relevant

 http://effbot.org/pyfaq/pyrun-simplefile-crashes-on-windows-but-not-o....

There's a workaround.

filename = "Entire path of the python file";
PyObject* PyFileObject = PyFile_FromString(filename, "r");
PyRun_SimpleFile(PyFile_AsFile(PyFileObject), filename);
// decref PyFileObject

http://mail.python.org/pipermail/python-list/2007-March/431725.html
http://python-forum.org/pythonforum/viewtopic.php?f=15&t=1554&p=6567
 
A

Aaron \Castironpi\ Brady

There's a workaround.

filename = "Entire path of the python file";
PyObject* PyFileObject = PyFile_FromString(filename, "r");
PyRun_SimpleFile(PyFile_AsFile(PyFileObject), filename);
// decref PyFileObject

http://mail.python.org/pipermail/py.../pythonforum/viewtopic.php?f=15&t=1554&p=6567

Just to follow up-- The links say that the crash comes from opening a
file with two different versions of a library. Would it be possible
to get an API entry point to the version the build uses?

It does use the 'fopen' function by name, but not the same version.
And creating the entire object just to get its f_fp field is really
long.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top