Problem with "PyRun_SimpleString" while interfacing Python with C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShambhuHubli
    New Member
    • Jul 2007
    • 6

    Problem with "PyRun_SimpleString" while interfacing Python with C

    Hi all!! .. I am New member to this group.

    And also new to C/PYTHON API coding.

    I am trying to have two way communication i,e i am calling from python to C and then from C to python.
    I have no problem calling c func from python....
    But problem is for calling C to Python
    I am trying to call a python function multiple times by passing one parameter 'fun(param)' from C code .
    I am getting segmentation fault error while executing .
    The code i am is below...

    # Python code..
    import API # It is a shared library created by using 'Swig' Tool
    class MyApp :
    __init__(self, parent) # function to create GUI
    //Code to create GUI.. (Text box)
    API.function() ## here I am calling 'function()' (which is in 'call.c')

    def fun(recv) :
    //Here i am inserting the received(recv) value in GUI Text Widget.
    return;

    app = MyApp() //creating object to class 'MyApp'

    // C code...
    // call.c

    function()
    {
    printf"Called from python\n"
    call();
    }

    call()
    {
    while(first_tim e) {
    Py_Initialise() ;
    PyRun_SimpleStr ing("import pyfile"); //pyfile is my python file
    }
    PyRun_SimpleStr ing("pyfile.app .fun('SUCCESS') "); //fun is function inside pyfile and I am passing 'SUCCESS' as the parameter
    // Py_Finalize();
    }

    To be more specific i am calling PyRun_SimpleStr ing("pyfile.fun ('SUCCESS')")
    multiple times , initializing and importing only once...And Finalizing in the end .(If i initialize and finalize everytime i invoke the function i got segmentation fault error)


    Can anyboby give a reason and solution to this problem...
    Thanks in advance..
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Welcome to TheScripts! You have inadvertantly posted your question in the MS Access Forum! We have both Python and C/C++ forums here, but since you indicate that your problem is calling Python from C, I'm moving your post to the C/C++ forum!

    Linq ;0)>

    Comment

    • Silent1Mezzo
      New Member
      • Feb 2007
      • 208

      #3
      Originally posted by ShambhuHubli
      [CODE=c]while(first_tim e) {
      Py_Initialise() ;
      PyRun_SimpleStr ing("import pyfile"); //pyfile is my python file
      }[/CODE]
      This is an infinite loop. Need to set first_time to something other than 1 after you run [CODE=c]PyRun_SimpleStr ing("import pyfile");[/CODE]

      This probably won't fix it but its something you should do.

      Comment

      • ShambhuHubli
        New Member
        • Jul 2007
        • 6

        #4
        Originally posted by Silent1Mezzo
        This is an infinite loop. Need to set first_time to something other than 1 after you run [CODE=c]PyRun_SimpleStr ing("import pyfile");[/CODE]

        This probably won't fix it but its something you should do.
        No actually actually i have declared first time as, static int first_time=0. And I am incrementing it inside the loop. But still I am grtting the problem.

        Comment

        Working...