Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Embedding Python does not throw an error, but the program crashes anyways
Forum Updated to NodeBB v4.3 + New Features

Embedding Python does not throw an error, but the program crashes anyways

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lfreeman6490
    wrote on 1 Sept 2021, 15:51 last edited by lfreeman6490 9 Jan 2021, 16:35
    #1

    I am trying to embed a python interpreter into my c++ gui. I am including Python.h and then in my function

    void pl::on_pb_connect_disconnect_clicked()
    {
        PyObject* pName, *pModule, *pDict, *pFunc;
    
        Py_Initialize();
    
        std::string filename = "test_python_file.py";
        const char* cstr1 = filename.c_str();
    
        pName = PyUnicode_FromString(cstr1);
    
        pModule = PyImport_Import(pName);
    
        pDict = PyModule_GetDict(pModule);
    
        std::string function_name = "test_function";
        const char* cstr = function_name.c_str();
    
        pFunc = PyDict_GetItemString(pDict, cstr);
    
        if (PyCallable_Check(pFunc))
        {
            PyObject_CallObject(pFunc, NULL);
        }
        else
        {
            PyErr_Print();
        }
    
        Py_Finalize();
    }
    

    The basic idea is that once the 'connect' button is hit, it calls test_function in test_python_file.py. It builds and runs successfully, once it is launched and I hit the 'connect' button, the program crashes. I have cleaned, rebuild, ran qmake, etc. Running in debug mode gives me a division by 0 error. If I comment out the code line by line I see that the errors comes from

    pDict = PyModule_GetDict(pModule);
    

    Once that line is used again, the program crashes.

    J 1 Reply Last reply 2 Sept 2021, 06:50
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 1 Sept 2021, 18:00 last edited by
      #2

      Hi,

      Since you are using a relative path, is your Python file in the same folder as the executable ?
      You should check whether your objects are null just in case something goes wrong.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 1 Sept 2021, 23:06
      2
      • S SGaist
        1 Sept 2021, 18:00

        Hi,

        Since you are using a relative path, is your Python file in the same folder as the executable ?
        You should check whether your objects are null just in case something goes wrong.

        L Offline
        L Offline
        lfreeman6490
        wrote on 1 Sept 2021, 23:06 last edited by
        #3

        @SGaist Hi, Thanks for the response. The python files are in the same folder as my executable. How would I check for null objects? Am I not doing that already? I thought that

        if (PyCallable_Check(pFunc))
            {
                PyObject_CallObject(pFunc, NULL);
            }
            else
            {
                PyErr_Print();
            }
        

        Would print out the error message that is returned, is that not correct?

        E 1 Reply Last reply 1 Sept 2021, 23:11
        0
        • L lfreeman6490
          1 Sept 2021, 23:06

          @SGaist Hi, Thanks for the response. The python files are in the same folder as my executable. How would I check for null objects? Am I not doing that already? I thought that

          if (PyCallable_Check(pFunc))
              {
                  PyObject_CallObject(pFunc, NULL);
              }
              else
              {
                  PyErr_Print();
              }
          

          Would print out the error message that is returned, is that not correct?

          E Offline
          E Offline
          eyllanesc
          wrote on 1 Sept 2021, 23:11 last edited by
          #4

          @lfreeman6490 Have you verified that the pointers are not null?

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          L 1 Reply Last reply 1 Sept 2021, 23:14
          0
          • E eyllanesc
            1 Sept 2021, 23:11

            @lfreeman6490 Have you verified that the pointers are not null?

            L Offline
            L Offline
            lfreeman6490
            wrote on 1 Sept 2021, 23:14 last edited by
            #5

            @eyllanesc How would I check for that? Is my if loop at the end of my code not checking for error messages?

            E 1 Reply Last reply 1 Sept 2021, 23:16
            0
            • L lfreeman6490
              1 Sept 2021, 23:14

              @eyllanesc How would I check for that? Is my if loop at the end of my code not checking for error messages?

              E Offline
              E Offline
              eyllanesc
              wrote on 1 Sept 2021, 23:16 last edited by eyllanesc 9 Jan 2021, 23:16
              #6

              @lfreeman6490 PyErr_Print only returns python errors, it does not check memory.

              what is the output of:

              qDebug() << pName << pModule  << pDict;
              

              before PyDict_GetItemString(...)?

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              L 1 Reply Last reply 1 Sept 2021, 23:21
              1
              • E eyllanesc
                1 Sept 2021, 23:16

                @lfreeman6490 PyErr_Print only returns python errors, it does not check memory.

                what is the output of:

                qDebug() << pName << pModule  << pDict;
                

                before PyDict_GetItemString(...)?

                L Offline
                L Offline
                lfreeman6490
                wrote on 1 Sept 2021, 23:21 last edited by
                #7

                @eyllanesc There is no difference in the output when entering that line. I cleaned, reran qmake, rebuilt, then ran directly from the executable as well as from qtcreator. The output just says

                The program has finished unexpectedly
                
                E 1 Reply Last reply 1 Sept 2021, 23:23
                0
                • L lfreeman6490
                  1 Sept 2021, 23:21

                  @eyllanesc There is no difference in the output when entering that line. I cleaned, reran qmake, rebuilt, then ran directly from the executable as well as from qtcreator. The output just says

                  The program has finished unexpectedly
                  
                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 1 Sept 2021, 23:23 last edited by
                  #8

                  @lfreeman6490 I never said that was the solution, do not rush. I'm just asking for some debugging information to analyze the cause of the error.

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  L 1 Reply Last reply 1 Sept 2021, 23:27
                  1
                  • E eyllanesc
                    1 Sept 2021, 23:23

                    @lfreeman6490 I never said that was the solution, do not rush. I'm just asking for some debugging information to analyze the cause of the error.

                    L Offline
                    L Offline
                    lfreeman6490
                    wrote on 1 Sept 2021, 23:27 last edited by
                    #9

                    @eyllanesc Okay, thanks. I just wanted to clearly show what the error message was and what I had done already

                    E 1 Reply Last reply 1 Sept 2021, 23:29
                    0
                    • L lfreeman6490
                      1 Sept 2021, 23:27

                      @eyllanesc Okay, thanks. I just wanted to clearly show what the error message was and what I had done already

                      E Offline
                      E Offline
                      eyllanesc
                      wrote on 1 Sept 2021, 23:29 last edited by
                      #10

                      @lfreeman6490 Comment on the line that generates the error and tell me what is printed on the console

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      L 1 Reply Last reply 2 Sept 2021, 00:58
                      1
                      • E eyllanesc
                        1 Sept 2021, 23:29

                        @lfreeman6490 Comment on the line that generates the error and tell me what is printed on the console

                        L Offline
                        L Offline
                        lfreeman6490
                        wrote on 2 Sept 2021, 00:58 last edited by
                        #11

                        @eyllanesc Okay, commenting out that line gives me hex code errors

                        0x67b0128 0x0 0x57290610 
                        
                        E 1 Reply Last reply 2 Sept 2021, 01:06
                        0
                        • L lfreeman6490
                          2 Sept 2021, 00:58

                          @eyllanesc Okay, commenting out that line gives me hex code errors

                          0x67b0128 0x0 0x57290610 
                          
                          E Offline
                          E Offline
                          eyllanesc
                          wrote on 2 Sept 2021, 01:06 last edited by eyllanesc 9 Feb 2021, 01:11
                          #12

                          @lfreeman6490 From the print we can see that pModule is null, so you get this behavior.

                          Change to:

                          if(!(pModule = PyImport_Import(pName))){
                              PyErr_Print();
                              return;
                          }
                          

                          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                          L 1 Reply Last reply 2 Sept 2021, 01:50
                          1
                          • E eyllanesc
                            2 Sept 2021, 01:06

                            @lfreeman6490 From the print we can see that pModule is null, so you get this behavior.

                            Change to:

                            if(!(pModule = PyImport_Import(pName))){
                                PyErr_Print();
                                return;
                            }
                            
                            L Offline
                            L Offline
                            lfreeman6490
                            wrote on 2 Sept 2021, 01:50 last edited by lfreeman6490 9 Feb 2021, 01:51
                            #13

                            @eyllanesc Where in the code should I add that? Should that be directly after creating pModule? Or at the very last spot in the code?

                            E 1 Reply Last reply 2 Sept 2021, 02:22
                            0
                            • L lfreeman6490
                              2 Sept 2021, 01:50

                              @eyllanesc Where in the code should I add that? Should that be directly after creating pModule? Or at the very last spot in the code?

                              E Offline
                              E Offline
                              eyllanesc
                              wrote on 2 Sept 2021, 02:22 last edited by
                              #14

                              @lfreeman6490 Change pModule = PyImport_Import(pName);

                              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                              L 1 Reply Last reply 2 Sept 2021, 02:50
                              0
                              • E eyllanesc
                                2 Sept 2021, 02:22

                                @lfreeman6490 Change pModule = PyImport_Import(pName);

                                L Offline
                                L Offline
                                lfreeman6490
                                wrote on 2 Sept 2021, 02:50 last edited by
                                #15

                                @eyllanesc Okay so by changing that it is no longer crashing, but it is not calling my function. The function is a simple test function that should return a number

                                def test_function():
                                    return 7
                                
                                E 1 Reply Last reply 2 Sept 2021, 02:56
                                0
                                • L lfreeman6490
                                  2 Sept 2021, 02:50

                                  @eyllanesc Okay so by changing that it is no longer crashing, but it is not calling my function. The function is a simple test function that should return a number

                                  def test_function():
                                      return 7
                                  
                                  E Offline
                                  E Offline
                                  eyllanesc
                                  wrote on 2 Sept 2021, 02:56 last edited by
                                  #16

                                  @lfreeman6490 It seems that you have not understood the problem. Your code has not imported the module so pModule is null.

                                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                                  1 Reply Last reply
                                  3
                                  • L lfreeman6490
                                    1 Sept 2021, 15:51

                                    I am trying to embed a python interpreter into my c++ gui. I am including Python.h and then in my function

                                    void pl::on_pb_connect_disconnect_clicked()
                                    {
                                        PyObject* pName, *pModule, *pDict, *pFunc;
                                    
                                        Py_Initialize();
                                    
                                        std::string filename = "test_python_file.py";
                                        const char* cstr1 = filename.c_str();
                                    
                                        pName = PyUnicode_FromString(cstr1);
                                    
                                        pModule = PyImport_Import(pName);
                                    
                                        pDict = PyModule_GetDict(pModule);
                                    
                                        std::string function_name = "test_function";
                                        const char* cstr = function_name.c_str();
                                    
                                        pFunc = PyDict_GetItemString(pDict, cstr);
                                    
                                        if (PyCallable_Check(pFunc))
                                        {
                                            PyObject_CallObject(pFunc, NULL);
                                        }
                                        else
                                        {
                                            PyErr_Print();
                                        }
                                    
                                        Py_Finalize();
                                    }
                                    

                                    The basic idea is that once the 'connect' button is hit, it calls test_function in test_python_file.py. It builds and runs successfully, once it is launched and I hit the 'connect' button, the program crashes. I have cleaned, rebuild, ran qmake, etc. Running in debug mode gives me a division by 0 error. If I comment out the code line by line I see that the errors comes from

                                    pDict = PyModule_GetDict(pModule);
                                    

                                    Once that line is used again, the program crashes.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 2 Sept 2021, 06:50 last edited by
                                    #17

                                    @lfreeman6490 said in Embedding Python does not throw an error, but the program crashes anyways:

                                    std::string filename = "test_python_file.py";
                                    const char* cstr1 = filename.c_str();
                                    
                                    pName = PyUnicode_FromString(cstr1);
                                    
                                    if(!(pModule = PyImport_Import(pName))){
                                        PyErr_Print();
                                        return;
                                    }
                                    

                                    From the 0x0 output you really should be able to understand that PyImport_Import(pName) returned nullptr. That means your test_python_file.py could not be loaded, Either the file could not be found, or there is some problem loading it. Hopefully the PyErr_Print(); gave some clue as to why, but you never said anything about what it output.

                                    Start by changing your test_python_file.py to the full path to where that file is.

                                    1 Reply Last reply
                                    2

                                    1/17

                                    1 Sept 2021, 15:51

                                    • Login

                                    • Login or register to search.
                                    1 out of 17
                                    • First post
                                      1/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved