开发者

Getting type/size of `time_t` using ctypes

I'm accessing a C struct which contains some time_t fields using python ctypes module.

Given its non comple开发者_Python百科tely portable nature, I cannot define these fields statically as of c_int or c_long type.

How can I define them to make my code portable?

Example C struct definition:

#import <sys/types.h>
#import <time.h>

typedef struct my_struct {
    time_t timestap;
    uint16_t code;      
};

Respective python ctypes structure:

from ctypes import *

c_time = ? # What do I have to put here?

class MyStruct(Structure):
    _fields_ = [
        ('timestamp', c_time),
        ('code', c_int16),
    ]


Your best bet is by introspecting the system your script runs on and making a best bet on which integral type to use. Something like,

if sys.platform == 'win32':
    time_t = ctypes.c_uint64
# ...

The bottom line is that time_t is not defined in the standard. It's up to the OS and compiler. So your definition of time_t in your Python script depends on the DLL/so you are interacting with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜