PyRun_SimpleFile() crashes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lixinyi.23@gmail.com

    PyRun_SimpleFile() crashes

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

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

    FILE *file_1 = fopen("a2l_read er.py","r+");
    PyRun_SimpleFil e(file_1,"a2l_r eader.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?

  • lixinyi.23@gmail.com

    #2
    Re: PyRun_SimpleFil e() crashes

    On 9¤ë24¤é, ¤È«á3:39, lixinyi...@gmai l.com wrote:
    my code:
    main.cpp
    #include <Python.h>
    >
    int main(int argc, char **argv)
    {
    Py_Initialize() ;
    >
    FILE *file_1 = fopen("a2l_read er.py","r+");
    PyRun_SimpleFil e(file_1,"a2l_r eader.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_SimpleFil e would just crash my application.
    but PyRun_SimpleStr ing works fine.

    But why???

    Comment

    • Nick Craig-Wood

      #3
      Re: PyRun_SimpleFil e() crashes

      lixinyi.23@gmai l.com <lixinyi.23@gma il.comwrote:
      my code:
      main.cpp
      #include <Python.h>
      >
      int main(int argc, char **argv)
      {
      Py_Initialize() ;
      >
      FILE *file_1 = fopen("a2l_read er.py","r+");
      PyRun_SimpleFil e(file_1,"a2l_r eader.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



      --
      Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

      Comment

      • Aaron \Castironpi\ Brady

        #4
        Re: PyRun_SimpleFil e() crashes

        On Sep 24, 6:30 am, Nick Craig-Wood <n...@craig-wood.comwrote:
        lixinyi...@gmai l.com <lixinyi...@gma il.comwrote:
         my code:
         main.cpp
         #include <Python.h>
        >
         int main(int argc, char **argv)
         {
         Py_Initialize() ;
        >
         FILE *file_1 = fopen("a2l_read er.py","r+");
         PyRun_SimpleFil e(file_1,"a2l_r eader.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-simple...dows-but-not-o....
        >
        --
        Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
        There's a workaround.

        filename = "Entire path of the python file";
        PyObject* PyFileObject = PyFile_FromStri ng(filename, "r");
        PyRun_SimpleFil e(PyFile_AsFile (PyFileObject), filename);
        // decref PyFileObject




        Comment

        • Aaron \Castironpi\ Brady

          #5
          Re: PyRun_SimpleFil e() crashes

          On Sep 24, 11:05 am, "Aaron \"Castironpi \" Brady"
          <castiro...@gma il.comwrote:
          On Sep 24, 6:30 am, Nick Craig-Wood <n...@craig-wood.comwrote:
          >
          >
          >
          lixinyi...@gmai l.com <lixinyi...@gma il.comwrote:
           my code:
           main.cpp
           #include <Python.h>
          >
           int main(int argc, char **argv)
           {
           Py_Initialize() ;
          >
           FILE *file_1 = fopen("a2l_read er.py","r+");
           PyRun_SimpleFil e(file_1,"a2l_r eader.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
          >>
          --
          Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
          >
          There's a workaround.
          >
          filename = "Entire path of the python file";
          PyObject* PyFileObject = PyFile_FromStri ng(filename, "r");
          PyRun_SimpleFil e(PyFile_AsFile (PyFileObject), filename);
          // decref PyFileObject
          >
          http://mail.python.org/pipermail/pyt...&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.

          Comment

          • lixinyi.23@gmail.com

            #6
            Re: PyRun_SimpleFil e() crashes

            On 9¤ë25¤é, ¤È«e1:05, "Aaron \"Castironpi \" Brady" <castiro....@gm ail.com>
            wrote:
            On Sep 24, 6:30 am, Nick Craig-Wood <n...@craig-wood.comwrote:
            >
            >
            >
            lixinyi...@gmai l.com <lixinyi...@gma il.comwrote:
            my code:
            main.cpp
            #include <Python.h>
            >
            int main(int argc, char **argv)
            {
            Py_Initialize() ;
            >
            FILE *file_1 = fopen("a2l_read er.py","r+");
            PyRun_SimpleFil e(file_1,"a2l_r eader.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
            >>
            --
            Nick Craig-Wood <n...@craig-wood.com--http://www.craig-wood.com/nick
            >
            There's a workaround.
            >
            filename = "Entire path of the python file";
            PyObject* PyFileObject = PyFile_FromStri ng(filename, "r");
            PyRun_SimpleFil e(PyFile_AsFile (PyFileObject), filename);
            // decref PyFileObject
            >
            http://mail.python.org/pipermail/pyt...&t=1554&p=6567
            Wow this one works fine for me.
            Thank you!

            Comment

            Working...