Python: fetching data from a buffer using ctypes

I can successfully call a function with ctypes in Python. Now I have a buffer filled with data structures that I want to retrieve. What is the best strategy for this? What else should I post?

Functions:

class list(): def __init__(self): #[...] def getdirentries(self, path): self.load_c() self.fd = os.open(path, os.O_RDONLY) self.statinfo = os.fstat(self.fd) self.buffer = ctypes.create_string_buffer(self.statinfo.st_size) nbytes = self.statinfo.st_size transferred_bytes = self.libc.getdirentries( self.fd, ctypes.byref(self.buffer), nbytes, ctypes.byref(self.basep) ) #[...] 

Structure:

 class dirent(ctypes.Structure): _fields_ = [ ("d_fileno", ctypes.c_uint32), # /* file number of entry */ ("d_reclen", ctypes.c_uint16), # /* length of this record */ ("d_type", ctypes.c_uint8), # /* file type */ ("d_namlen", ctypes.c_uint8), # /* length of string in d_name */ ("d_name", ctypes.c_char * (MAXNAMELEN + 1) ) ] 

Some results:
Bytes transmitted: 156
sizeof buffer: 272
Buffer: <ctypes.c_char_Array_272 object at 0x8c3f0>

+4
source share
1 answer

I wonder why you use os.stat () instead of calling statinfo and os.path.walk () instead of calling getdirentries?

Usually, when you have data buffers that you want to transfer and exit from C, for this you should use a package of structural modules and unpack the methods.

0
source

All Articles