Improved struct module
Jeremy Hylton
jeremy at cnri.reston.va.us
Thu Oct 14 15:23:47 EDT 1999
>>>>> "RB" == Robin Boerdijk <robin.boerdijk at nl.origin-it.com> writes:
RB> I do want to come back on the buffer interface though. I think I
RB> have no alternative for using it. How else can I encapsulate a C
RB> API function that expects a pre-allocated buffer as a parameter.
RB> For example, how could I encapsulate the following C function:
RB> int GetMessageFromQueue(void* buffer, int bufsize);
RB> where buffer is an application supplied buffer of size
RB> bufsize. The function will return the part of the buffer
RB> actually filled. The only one who knows what a reasonable size
RB> of the buffer is is the application programmer (who is
RB> programming in Python).
This sounds a lot like the recv method on sockets.
The C API to recv is:
ssize_t recv(int socket, void *buffer, size_t length,
int flags);
The Python program calls the recv method on a socket and passes a
buffer size (call the arg buflen).
The C implementation of the recv method is pretty simple:
1. Allocate a string of size buflen.
2. Call recv and pass the Python string's ob_sval as the buffer
argument.
3. Check how many bytes were actually returned and Resize the string
to that size.
Jeremy
More information about the Python-list
mailing list