| | |
python extending with c(doubts)
![]() |
•
•
Join Date: Jan 2007
Posts: 1
Reputation:
Solved Threads: 0
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.
2. 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[i]=1+j;
//printf("%d\n",a[i]);
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
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.
2. 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[i]=1+j;
//printf("%d\n",a[i]);
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
Python Syntax (Toggle Plain Text)
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.
Python Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Jan 14th, 2007 at 4:21 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
May 'the Google' be with you!
Is it possible to call an exe from python? Is the Pope Catholic? Sure, Python can do it ...
python Syntax (Toggle Plain Text)
# 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"])
May 'the Google' be with you!
![]() |
Similar Threads
- Python! Thoughts? (Python)
- Extending a Language: (Python)
- Junior Developer have some doubts in CF_SQL_NUMERIC (ColdFusion)
Other Threads in the Python Forum
- Previous Thread: Function will freeze program (on occasion)
- Next Thread: Project ideas - Medium difficulty
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






