Hallo everyone,
I'm trying to convert a main function from C to Python,by using Swig and call it with a .txt argument.The code is the following : -
/*test.c*/
-
#include <stdio.h>
-
int main(int argc, char ** argv)
-
{
-
float f1,f2,f3;
-
FILE *fp;
-
fp=fopen(argv[1],"r");
-
if(fp!=NULL)
-
{
-
printf("Succesfull opening\n");
-
fscanf(fp,"%f\n%f\n%f",&f1,&f2,&f3);
-
printf("the values are \n%f\n%f\n%f\n",f1,f2,f3);
-
fclose(fp);
-
}
-
return 0;
-
}
-
-
/*test_interface.i*/
-
%module example
-
%{
-
int main(int argc,char **argv);
-
%}
-
int main(int argc,char **argv);
-
-
/*text.txt*/
0.2313
2.3123
12.123
from Linux command prompt I execute the following commands:
$ swig -python test_interface. i
$ gcc -c test.c test_interface_ wrap.c -I/usr/include/python2.5
$ ld -shared test.o test_interface_ wrap.o -o _example.so
$ python
>>>from _example import *
>>>main(1,'text 1.txt') // ERROR !!!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'main', argument 2 of type 'char **'
All i want to do is the same thing like the following C procces:
$ gcc test.c -o test
$ ./test text.txt
the values are
0.231300
2.312300
12.123000
What is the solution?
7 7340
Hallo everyone,
I'm trying to convert a main function from C to Python,by using Swig and call it with a .txt argument.The code is the following :
/*test.c*/
#include <stdio.h>
int main(int argc, char ** argv)
{
float f1,f2,f3;
FILE *fp;
fp=fopen(argv[1],"r");
if(fp!=NULL)
{
printf("Succesf ull opening\n");
fscanf(fp,"%f\n %f\n%f",&f1,&f2 ,&f3);
printf("the values are \n%f\n%f\n%f\n" ,f1,f2,f3);
fclose(fp);
}
return 0;
}
/*test_interface .i*/
%module example
%{
int main(int argc,char **argv);
%}
int main(int argc,char **argv);
/*text.txt*/
0.2313
2.3123
12.123
from Linux command prompt I execute the following commands:
$ swig -python test_interface. i
$ gcc -c test.c test_interface_ wrap.c -I/usr/include/python2.5
$ ld -shared test.o test_interface_ wrap.o -o _example.so
$ python
>>>from _example import *
>>>main(1,'text 1.txt') // ERROR !!!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'main', argument 2 of type 'char **'
All i want to do is the same thing like the following C procces:
$ gcc test.c -o test
$ ./test text.txt
the values are
0.231300
2.312300
12.123000
What is the solution?
hi
just post your test_interface_ wrap.c file
varun
hallo varun
Thanks for your answer .test_interface _wrap.c is about 2.700 lines .it is helpfull to post it?
hallo varun
Thanks for your answer .test_interface _wrap.c is about 2.700 lines .it is helpfull to post it?
only post lines of function
SWIG_wrap_main( ...) or something like that
Hallo again verun
I hope the following code of interface_wrap. c is helpful.Thank you again. -
extern "C" {
-
#endif
-
SWIGINTERN PyObject *_wrap_main(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
-
PyObject *resultobj = 0;
-
int arg1 ;
-
char **arg2 = (char **) 0 ;
-
int result;
-
int val1 ;
-
int ecode1 = 0 ;
-
void *argp2 = 0 ;
-
int res2 = 0 ;
-
PyObject * obj0 = 0 ;
-
PyObject * obj1 = 0 ;
-
-
if (!PyArg_ParseTuple(args,(char *)"OO:main",&obj0,&obj1)) SWIG_fail;
-
ecode1 = SWIG_AsVal_int(obj0, &val1);
-
if (!SWIG_IsOK(ecode1)) {
-
SWIG_exception_fail(SWIG_ArgError(ecode1), "in method '" "main" "', argument " "1"" of type '" "int""'");
-
}
-
arg1 = (int)(val1);
-
res2 = SWIG_ConvertPtr(obj1, &argp2,SWIGTYPE_p_p_char, 0 | 0 );
-
if (!SWIG_IsOK(res2)) {
-
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "main" "', argument " "2"" of type '" "char **""'");
-
}
-
arg2 = (char **)(argp2);
-
result = (int)main(arg1,arg2);
-
resultobj = SWIG_From_int((int)(result));
-
return resultobj;
-
fail:
-
return NULL;
-
}
-
-
-
static PyMethodDef SwigMethods[] = {
-
{ (char *)"main", _wrap_main, METH_VARARGS, NULL},
-
{ NULL, NULL, 0, NULL }
-
};
-
-
-
/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */
-
-
static swig_type_info _swigt__p_char = {"_p_char", "char *", 0, 0, (void*)0, 0};
-
static swig_type_info _swigt__p_p_char = {"_p_p_char", "char **", 0, 0, (void*)0, 0};
-
-
static swig_type_info *swig_type_initial[] = {
-
&_swigt__p_char,
-
&_swigt__p_p_char,
-
};
-
-
static swig_cast_info _swigc__p_char[] = { {&_swigt__p_char, 0, 0, 0},{0, 0, 0, 0}};
-
static swig_cast_info _swigc__p_p_char[] = { {&_swigt__p_p_char, 0, 0, 0},{0, 0, 0, 0}};
-
-
static swig_cast_info *swig_cast_initial[] = {
-
_swigc__p_char,
-
_swigc__p_p_char,
-
};
-
-
-
/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (END) -------- */
-
-
static swig_const_info swig_const_table[] = {
-
{0, 0, 0, 0.0, 0, 0}};
-
-
#ifdef __cplusplus
-
}
Hallo again verun
I hope the following code of interface_wrap. c is helpful.Thank you again.
extern "C" {
#endif
SWIGINTERN PyObject *_wrap_main(PyO bject *SWIGUNUSEDPARM (self), PyObject *args) {
PyObject *resultobj = 0;
int arg1 ;
char **arg2 = (char **) 0 ;
int result;
int val1 ;
int ecode1 = 0 ;
void *argp2 = 0 ;
int res2 = 0 ;
PyObject * obj0 = 0 ;
PyObject * obj1 = 0 ;
if (!PyArg_ParseTu ple(args,(char *)"OO:main",&ob j0,&obj1)) SWIG_fail;
ecode1 = SWIG_AsVal_int( obj0, &val1);
if (!SWIG_IsOK(eco de1)) {
SWIG_exception_ fail(SWIG_ArgEr ror(ecode1), "in method '" "main" "', argument " "1"" of type '" "int""'");
}
arg1 = (int)(val1);
res2 = SWIG_ConvertPtr (obj1, &argp2,SWIGTYPE _p_p_char, 0 | 0 );
if (!SWIG_IsOK(res 2)) {
SWIG_exception_ fail(SWIG_ArgEr ror(res2), "in method '" "main" "', argument " "2"" of type '" "char **""'");
}
arg2 = (char **)(argp2);
result = (int)main(arg1, arg2);
resultobj = SWIG_From_int(( int)(result));
return resultobj;
fail:
return NULL;
}
static PyMethodDef SwigMethods[] = {
{ (char *)"main", _wrap_main, METH_VARARGS, NULL},
{ NULL, NULL, 0, NULL }
};
/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */
static swig_type_info _swigt__p_char = {"_p_char", "char *", 0, 0, (void*)0, 0};
static swig_type_info _swigt__p_p_cha r = {"_p_p_char" , "char **", 0, 0, (void*)0, 0};
static swig_type_info *swig_type_init ial[] = {
&_swigt__p_char ,
&_swigt__p_p_ch ar,
};
static swig_cast_info _swigc__p_char[] = { {&_swigt__p_cha r, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info _swigc__p_p_cha r[] = { {&_swigt__p_p_c har, 0, 0, 0},{0, 0, 0, 0}};
static swig_cast_info *swig_cast_init ial[] = {
_swigc__p_char,
_swigc__p_p_cha r,
};
/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (END) -------- */
static swig_const_info swig_const_tabl e[] = {
{0, 0, 0, 0.0, 0, 0}};
#ifdef __cplusplus
}
hi
sorry for the late responce
i think you need to manually write for (int argc,char *argv[])
try this code in your .i file - %typemap(in) (int argc, char *argv[]) {
-
int i;
-
if (!PyList_Check($input)) {
-
PyErr_SetString(PyExc_ValueError, "Expecting a list");
-
return NULL;
-
}
-
$1 = PyList_Size($input);
-
$2 = (char **) malloc(($1+1)*sizeof(char *));
-
for (i = 0; i < $1; i++) {
-
PyObject *s = PyList_GetItem($input,i);
-
if (!PyString_Check(s)) {
-
free($2);
-
PyErr_SetString(PyExc_ValueError, "List items must be strings");
-
return NULL;
-
}
-
$2[i] = PyString_AsString(s);
-
}
-
$2[i] = 0;
-
}
-
-
%typemap(freearg) (int argc, char *argv[]) {
-
if ($2) free($2);
-
}
I try to and it works. Thank you a lot varuns.
Hello, varuns.
I also meet a similar question, I don't know how to deal with the arguments of the main() function, should I replace the main() function in the c file? Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: timothy.williams |
last post by:
Hi.
I trying to write an extension module to call some C libraries so I can
use them in Python. Several of the library functions pass pointers to
structures as arguments. I was thinking that I could create a class for
each structure, but I'm not sure how to get the data back and forth.
The example the "Extending and Embedding the Python...
|
by: Morgan |
last post by:
I have read much posts on the argument but no one
clearly says if this operation is possible or not.
Simply I have a routine which reads from a text
file some integer arrays (1 or 2D).
The first array in the file has known dimension,
but it contains the dimensions (so unknown at
compile time) of all the other arrays.
So I can allocate...
|
by: tony.ha |
last post by:
Hello,
I have open a Python program in the IDLE, but when I select the "run
module" under "run" menu, it
does not allow me to pass an argument to my Python program!
How do you pass an argument to a Python program under the IDLE? Thanks for
you help!
|
by: Bill |
last post by:
Hello All,
I am trying to pass a struct to a function. How would that best be
accomplished?
Thanks,
Bill
|
by: erikcw |
last post by:
Hi,
I'm getting the following error when I try to pass a list into a
function.
My List: crea =
Traceback (most recent call last):
File "wa.py", line 118, in ?
curHandler.walkData()
| |
by: Robert Dailey |
last post by:
Hi,
I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the function is
also modified.
Sometimes I wish to work with "copies", in that when I pass in an
integer variable into a function, I want the...
|
by: lisp9000 |
last post by:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
|
by: =?Utf-8?B?U3dhcHB5?= |
last post by:
Can anyone suggest me to pass more parameters other than two parameter for
events like the following?
Event:
Onbutton_click(object sender, EventArgs e)"
Event handler:
button.Click += new EventHandler(Onbutton_click);
I want to pass more information related that event. & want to use that
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms.
Here is a newbie mistake that I found myself doing (as a newbie), and
that even a master programmer, the guru of this forum, Jon Skeet,
missed!...
|
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. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |