473,714 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with "PyRun_SimpleSt ring" while interfacing Python with C

6 New Member
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..
Jul 24 '07 #1
3 6397
missinglinq
3,532 Recognized Expert Specialist
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
Silent1Mezzo
208 New Member
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
ShambhuHubli
6 New Member
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

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

Similar topics

2
2883
by: disgracelands | last post by:
I've been looking at using python and it's been going fairly well so far, i can call C from python and python from C fine but now i've hit a snag and i'm wondering how you guys have got around it. I have to initialise python inside my C project so that i can make calls to python modules whenever i need to but i can't create a dll for the project as it's got to produce an exe or at least a lib so that i can execute the initialisation...
3
5121
by: Link | last post by:
Dear sir, I am used to developing C program on MSVC7.0 platform. This is my first time to use Python for calling by C program. Now, it is hard to deal with the problem about extracting the variable which be definied in Python. That how can I do? I have gotten the JPython 's sample that the following code can be looked. Have anyone help me to obtain same results that be called by Python's C API? What do I mean?
3
1642
by: Bill Orcutt | last post by:
Having seen the number of lost souls asking questions about embedding Python in the archives of this group, I hesitate to add myself to their number, but I've hit a problem I can't quite get my head around. I have a simple C program that embeds a python interpreter to execute python commands and return stdout, and to a point everything works as intended. The problem that each command seems to be executed in its own context and knows...
5
2277
by: eq | last post by:
Hi, I'm trying to create a program(written in C) that does the following things using embedded Python: 1. Create a module(say, "MyModule") 2. Create a class in that module(say, "MyClass") 3. Create a function in that module(say, "MyFunction") The Python-code would look like this: MyClass.py:
2
2769
by: Ugo Di Girolamo | last post by:
I have the following code, that seems to make sense to me. However, it crashes about 1/3 of the times. My platform is Python 2.4.1 on WXP (I tried the release version from the msi and the debug version built by me, both downloaded today to have the latest version).
1
3080
by: freesteel | last post by:
I have posted about this problem before. SInce then I found a much better article to help with embedding python in a multithreaded application: http://www.linuxjournal.com/article/3641 I found this article very good and it clarified for me what needs doing. Now I have an example application that almost works, but it is far from reliable. If I run this several times I get crashes telling me that the heap is modified after deallocation....
2
3248
Bulldog
by: Bulldog | last post by:
I am new to this board and I am using Python 2.2.1 on Windows XP SP2. I am working on embedding some python functionality in a C++ win32 DLL, and I am having issues using the PyImport_ functions available in the python C API. More specifically I am trying to use Pyimport_ImportModule() to import sys, and it doesn't work. I have also tried using PyString_FromString() to convert the the string "sys" to a Python object to then use...
3
6865
by: Jim Hill | last post by:
Well, I've found about a hundred thousand web pages where people have had the same problem I have but nary a page with a solution that works for me. I want to do a simple embed, so I've followed the example in the Extending and Embedding documentation: In the .c file, #include <Python.h>
4
2833
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application. I want to be able to debug the Python script by using a debug server, like Winpdb (winpdb.org). I use ActivePython 2.5.2.2, Microsoft Visual Studio 2005, and Winpdb 1.3.8.
0
8797
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8704
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9309
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6629
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.