By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,208 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,208 IT Pros & Developers. It's quick & easy.

Problem with "PyRun_SimpleString" while interfacing Python with C

P: 6
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_time) {
Py_Initialise();
PyRun_SimpleString("import pyfile"); //pyfile is my python file
}
PyRun_SimpleString("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_SimpleString("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..
Jul 24 '07 #1
Share this Question
Share on Google+
3 Replies


missinglinq
Expert 2.5K+
P: 3,532
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)>
Jul 24 '07 #2

100+
P: 208
Expand|Select|Wrap|Line Numbers
  1. while(first_time) {
  2. Py_Initialise();
  3. PyRun_SimpleString("import pyfile"); //pyfile is my python file
  4. }
This is an infinite loop. Need to set first_time to something other than 1 after you run
Expand|Select|Wrap|Line Numbers
  1. PyRun_SimpleString("import pyfile");
This probably won't fix it but its something you should do.
Jul 24 '07 #3

P: 6
This is an infinite loop. Need to set first_time to something other than 1 after you run
Expand|Select|Wrap|Line Numbers
  1. PyRun_SimpleString("import pyfile");
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.
Jul 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.