Got it solved.
I had the following and it worked:
time1 = myclass.intArray(2)
myclass.get_time_value2("time1", time1)
print time1[0], ' = ', time1[1]
ctran08 wrote:
>
> Hi,
>
> I have the following C function that returns the value of type struct
> timeval based on the name passed:
>
> struct timeval get_time_value (char *name) {
> static char *tmval_var[3] = { "time1", "time2", "time3" };
> struct timeval tmval[3];
> int i;
> INFO *ps;
>
> ps = get_ps();
> tmval[0] = ps->time1;
> tmval[1] = ps->time2;
> tmval[2] = ps->time3;
>
> for (i = 0; i < 3; i++) {
> if (strcmp(name, tmval_var[i]) == 0)
> return tmval[i];
> }
> return;
> }
>
> In swig *.i file:
>
> %inline %{
> extern struct timeval get_time_value (char *name);
> }
>
> When access from python:
>
> time1 = myclass.get_time_value("time1");
>
> I got an error:
> swig/python detected a memory leak of type 'struct timeval *', no
> destructor found.
>
> Anyone worked with this struct timeval, please help me ? Thanks.
>
> --------------------------------
>
> My other approach to above problem by getting result returned in the array
> method.
>
> int get_time_value2 (char *name, int *rslt) {
> ...
> for (i = 0; i < 3; i++) {
> if (strcmp(name, tmval_var[i]) == 0) {
> rslt[0] = tmval[i].tv_sec;
> rslt[1] = tmval[i].tv_usec;
> return 0;
> }
> }
> }
>
> In swig *.i file:
>
> %include "typemaps.i"
> %inline %{
> extern int get_time_value2 (char *name, int *OUTPUT);
> }
>
> In python:
> time1 = myclass.get_time_value2("time1");
> print time1
>
> I got no error but I only get a list returned with status and tv_sec part
> of timeval. tv_usec is not returned. It looks like I need to tell swig I
> pass array to output by using "carrays.i" but I'm stuck at the moment.
> I'm thinking of something like
>
> %include "carrays.i"
> %array_class (int, intArray)
> %inline %{
> extern int get_time_value2 (char *name, int *OUTPUT);
> }
>
> time1 = intArray(2) <--- Based on example of swig/python doc but not sure
> where to get this
> myclass.get_time_value2("time1", time1)
> print time1
>
> Anyone dealed with intarray before, please help !
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
--
View this message in context: http://www.nabble.com/get-value-of-type-struct-timeval-from-C-to-python-tp19512649p19520246.html
Sent from the swig-user mailing list archive at Nabble.com.
|