PyRun_SimpleFile() crashes

Aaron "Castironpi" Brady castironpi at gmail.com
Wed Sep 24 12:05:04 EDT 2008


On Sep 24, 6:30 am, Nick Craig-Wood <n... at craig-wood.com> wrote:
> lixinyi... at gmail.com <lixinyi... at gmail.com> wrote:
> >  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?
>
> 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...
>
> --
> Nick Craig-Wood <n... at craig-wood.com> --http://www.craig-wood.com/nick

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




More information about the Python-list mailing list