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

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 3.5k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    2
    • SGaistS SGaist

      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 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?

      eyllanescE 1 Reply Last reply
      0
      • L lfreeman6490

        @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?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on 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
        0
        • eyllanescE eyllanesc

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

          L Offline
          L Offline
          lfreeman6490
          wrote on 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?

          eyllanescE 1 Reply Last reply
          0
          • L lfreeman6490

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

            eyllanescE Offline
            eyllanescE Offline
            eyllanesc
            wrote on last edited by eyllanesc
            #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
            • eyllanescE eyllanesc

              @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 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
              
              eyllanescE 1 Reply Last reply
              0
              • L lfreeman6490

                @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
                
                eyllanescE Offline
                eyllanescE Offline
                eyllanesc
                wrote on 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
                • eyllanescE eyllanesc

                  @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 last edited by
                  #9

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

                  eyllanescE 1 Reply Last reply
                  0
                  • L lfreeman6490

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

                    eyllanescE Offline
                    eyllanescE Offline
                    eyllanesc
                    wrote on 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
                    1
                    • eyllanescE eyllanesc

                      @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 last edited by
                      #11

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

                      0x67b0128 0x0 0x57290610 
                      
                      eyllanescE 1 Reply Last reply
                      0
                      • L lfreeman6490

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

                        0x67b0128 0x0 0x57290610 
                        
                        eyllanescE Offline
                        eyllanescE Offline
                        eyllanesc
                        wrote on last edited by eyllanesc
                        #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
                        1
                        • eyllanescE eyllanesc

                          @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 last edited by lfreeman6490
                          #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?

                          eyllanescE 1 Reply Last reply
                          0
                          • L lfreeman6490

                            @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?

                            eyllanescE Offline
                            eyllanescE Offline
                            eyllanesc
                            wrote on 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
                            0
                            • eyllanescE eyllanesc

                              @lfreeman6490 Change pModule = PyImport_Import(pName);

                              L Offline
                              L Offline
                              lfreeman6490
                              wrote on 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
                              
                              eyllanescE 1 Reply Last reply
                              0
                              • L lfreeman6490

                                @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
                                
                                eyllanescE Offline
                                eyllanescE Offline
                                eyllanesc
                                wrote on 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

                                  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.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on 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

                                  • Login

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