hai,

This is punitha.Iam having some doubts
regarding python (python extending with c).In our

application python is used as a glue language.It is used to call c
funcions(which is in the form of dlls).Once a c function

is called,Pyhton variable receives the return value and it passes that
value to the next c function.Python is linked

with c using swig.Swig is an automatic wrapper interface generator
which contains only the function declaration.

Our main aim is to allow the user to write
his own code and link it with

the provided primitive functions(C functions). We tried this in two
ways.

  1. User writes his own code in C language in the form of DLL and then
    it is linked with other primitive function(C

function) using python.

  1. User writes his own code in python itself and it is linked with the
    other primitive blocks.

My problem is with the second try,where user writes their own code.

1.In python code,C function will be called and the value returned by
that function will be used in the python code.In my

case,the c function returns an array,python is receiving the address
and printing only the address not the value.

I dont know how to get those array values and use it in python.

This problem exist in both the linux and windows version.I have given
an example below which i tried in linux.

/***********************(C function which returns an array)
-(last.c)*****************/


#include<stdio.h>
int* new_last()
{
int a[10];
int i,j=0;
for(i=0;i<=5;i++)
{
a=1+j;
//printf("%d\n",a);
j++;
}
return(a);
}


/***************************************************************************************/


/**************************swig
file-(last.i)*****************************************/


%module last
%inline %{
int *new_last();
%}


/****************************************************************************************/


/**************************python
file(files.py)*****************************************/


#!/usr/local/bin/python
import last                  #HERE IAM CALLING THE C FUNCTION AND
PRINTING THE RETURNED ARRAY.
k=last.new_last()
print k


/**********************************************************************************************************/
Here the c file(last.c) and the interface file(last.i) is
created,compiled and .so files are created.


Also the python file(files.py) is created and executed.


/**********************************************************************************************************/


[punitha@sys-23 lasttry]$ vi last.c


[punitha@sys-23 lasttry]$ vi last.i


[punitha@sys-23 lasttry]$ vi files.py


[punitha@sys-23 lasttry]$ swig -python last.i


[punitha@sys-23 lasttry]$ gcc -c last.c last_wrap.c
\-I/usr/include/python2.3


[punitha@sys-23 lasttry]$ ld -shared last.o last_wrap.o -o _last.so


[punitha@sys-23 lasttry]$ chmod +x files.py


[punitha@sys-23 lasttry]$ python files.py


_e07decfe_p_int


[punitha@sys-23 lasttry]$
/************************************************************************************************************/

When i run python files.py command,it is returning the starting address
of the array.How to get the array value

from this address in python?

2.Python is not supporting long double.But in our application we need
long double.Is there any solution to get it?

3.Is it possible to call a c function(which is in the form of exe)
residing in turbo C from python?

DLL calling from python is working both in windows(vc++) and linux.If
it is possible to call an exe from python in

vc++,then i can do it in turbo c.I failed in calling an exe from
python.Is it possible to call an exe from python?

4.Is there any book available for Python extention & embedding with C?

Please give me some suggestion regarding this.

----regards

punitha

include<stdio.h>
int* new_last()
{
int a[10];
int i,j=0;
for(i=0;i<=5;i++)
{
a[i]=1+j;
//printf("%d\n",a[i]);
j++;
}
return(a);
}

C functions can not return arrays like that. They must be pointers and size allocated at runtime, like this:

include<stdio.h>
int* new_last()
{

int* a;
a = malloc(10 * sizeof(int));

int i,j=0;
for(i=0;i<=5;i++)
{
a[i]=1+j;
//printf("%d\n",a[i]);
j++;
}
return(a);
}

Now one problem is: can python deallocate the memory that was allocated by the c probram? If you put that code in a dll then the problem becomes even worse because memory allocated in a dll must normally be deallocated in the dll.

I think a better approach for you is to allocate the array in the python program and pass it to C for it to fill in. You won't have the memory allocation/deallocation problems described above.

include<stdio.h>
int* new_last(int array[10])
{
int i,j=0;
for(i=0;i<=5;i++)
{
a[i]=1+j;
//printf("%d\n",a[i]);
j++;
}
return(a);
}

Sorry, but I can not answer your other questions because I know next to nothing about python language. But I know there are others here who can help you.

Here is an article that discusses linking C functions with python. Hope it helps.

Have you taken a good look at BoostPython?
http://wiki.python.org/moin/boost%2epython

Boost works much smoother with Python then the old Swig approach. You could also use Jython. Jython has Python syntax but works very well with Java. It compiles to JVM bytecode rather then PVM bytecode and can use the Java library and Java applets. Think of Java as being as close to C++ as you might want to get. There is some C in all of this too.

Is it possible to call an exe from python? Is the Pope Catholic? Sure, Python can do it ...

# run an external program from within Python code with
# subprocess.call(["program-name", "arg1", "arg2", ...])
# safer than the usual   os.system("program-name arg1 arg2")
# subprocess is new in Python24
import subprocess

subprocess.call(["C:/Python24/Python.exe", "Skripta.py", "arg1", "arg2"])
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.