[C++-sig] Wrapping functions that take void* parameters

Gustavo Carneiro gjcarneiro at gmail.com
Thu Aug 7 17:17:31 CEST 2008


2008/8/7 Geoff <kuroutadori at gmail.com>

> I'm trying to wrap a C++ library that is used for serial communications. It
> has functions such as:
>
> ssize_t Read (void * const buffer, size_t count);
>
> and
>
> ssize_t Write (const void * const buffer, size_t count);
>
> There are two issues here that I haven't been able to solve:
>
> 1) How to wrap the void pointers. Ideally, I'd like to be able to do things
> like pass in a buffer of raw data or a Python string and have it written.
> Half a day of searching hasn't turned up anything even vaguely related to
> this more recent than 2003, so I was hoping there may have been some
> progress since then.


>
> 2) The Read() method expects buffer to point to some pre-allocated space
> into which it can put the data that is read from the serial port. I'm not
> sure how I could wrap this, because just passing in a Python string or
> something doesn't guarantee that that string has enough space to store the
> data. My first instinct would be to write a wrapper function that allocates
> space based on count, then calls Read(), passing in a pointer to that space,
> but I don't know how I could return that to the Python side.


Normally this type of memory buffers are mapped to Python strings, since
Python strings can hold NULL bytes.  Use PyString_FromStringAndSize to
convert from C to Python (return), and "s#" format chars in
PyArg_ParseTuple* to convert from Python to C.

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080807/352f5fb0/attachment.htm>


More information about the Cplusplus-sig mailing list