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,'text1.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 7180
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,'text1.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(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
}
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
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
9 posts
views
Thread by timothy.williams |
last post: by
|
7 posts
views
Thread by Morgan |
last post: by
|
4 posts
views
Thread by tony.ha |
last post: by
|
28 posts
views
Thread by Bill |
last post: by
|
3 posts
views
Thread by erikcw |
last post: by
|
10 posts
views
Thread by Robert Dailey |
last post: by
|
6 posts
views
Thread by lisp9000 |
last post: by
|
24 posts
views
Thread by =?Utf-8?B?U3dhcHB5?= |
last post: by
|
12 posts
views
Thread by raylopez99 |
last post: by
| | | | | | | | | | | |