Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using struct timeval in Python

I have a C program containing a structure

struct S{
           int x;
           struct timeval t;
 };

and a function

int func(struct S s1, struct S s2)

I need to call this function from my python program. I am using ctypes.The parallel structure on Python

import ctypes
from ctypes import *
class S(Structure):
        _fields_ = [("x",c_int),
                     ("t", ?)]

Now, my question is what will I write in the ? place and any dependencies related to it. Thanks in advance.

like image 529
Sudip Avatar asked Feb 20 '23 20:02

Sudip


2 Answers

Find the definition of struct timeval in your platform's C include files (the Internet suggests sys/time.h), then transcode that into a ctypes structure.

On my platform a struct timeval is

struct timeval {
  long tv_sec;
  long tv_usec;
};

(and I suppose this is the standard anyway), so

class timeval(Structure):
    _fields_ = [("tv_sec", c_long), ("tv_usec", c_long)]

class S(Structure):
    _fields_ = [("x",c_int), ("t", timeval)]

would probably fit the bill.

like image 95
AKX Avatar answered Feb 23 '23 09:02

AKX


without any further info, it will be the definition of timeval:

class timeval(Structure):
    _fields_ = [("tv_sec",c_long),
                 ("tv_usec", c_long)]

class S(Structure):
    _fields_ = [("x",c_int),
                 ("t", timeval)]
like image 29
Lie Ryan Avatar answered Feb 23 '23 09:02

Lie Ryan