user3696153
user3696153

Reputation: 752

using swig to wrap read/write like functions

This question is about using SWIG, to create a Python/C++ interface.

My C++ code, has some functions that model the classic unix read/write and I need to call these from Python. The C++ class is like this:

class DEVICE {
    ... other stuff ...
    int write_bytes( const uint8_t *bytes, size_t nbytes, unsigned timeout_msecs );
    int read_bytes(  uint8_t *bytes, size_t nbytes, unsigned timeout_msecs );
}

These functions exactly model the unix read() and write(), but with a timeout parameter, these return the number of actual bytes transferred.

I am looking for the "correct" python API I should provide. I am also a python Noob .. so If I am doing something wrong with python please correct me.

I think I can figure the WRITE side out ..

Python WRITE: The Python function should have 2 parameters: An array of bytes, and a timeout value. Since Python arrays contain their length ... the length is thus an implied parameter.

C++ Code for WRITE: Since memory is coming from python, I only need to ask the array how many bytes it contains, get the raw byte pointer, and call my C function some how.

The READ side ... I need help with.

Python READ: Again, 2 parameters, an array of bytes and a timeout value. Here, since I do not know the actual number of bytes that will be read - I think my C code should allocate memory on behalf of Python (or is that wrong? I do not know). Then - how do I set the resulting length? I don't see/find an example of this. Or do I make my user/victim pre-allocate the array? (that seems wrong)

I have looked in the LLDB code for examples, specifically - I see these %typemaps - Specifically:

https://github.com/carlokok/lldb/blob/master/scripts/Python/python-typemaps.swig#L134

I do not fully understand typemaps (I am also new to swig...) but they appear to be very close. But not exactly what I am looking for. Can somebody explain how these work? The SWIG docs are not helpful (reason: There are lots of 'snippits' of code, I cannot find a complete end to end coherent example)

Can somebody create a better (coherent) example? Or point me to an example to read?

Upvotes: 1

Views: 394

Answers (2)

Oliver
Oliver

Reputation: 29483

Your case is already covered by the SWIG typemap library. This means you should be able to use the %apply (Type* INPUT, size_t length) directive as explained in a couple of places in SWIG docs, such as section 8.3. Since you don't show any code in your question, I suggest you give this a shot and if you have any trouble, update your question to show the code you tried and what happens, I or someone else may be able to provide a more detailed answer.

Upvotes: 1

IWishIKnew
IWishIKnew

Reputation: 69

There are two ways that I know of that you can do this:

1: you can have the program redirect it's standard io streams to files, and then your python (or whatever program) program can just write to the input file to input, and read from the output file to get output.

2: you can call the functions by useing argv and argc. While output can not (unless you redirect the program's output from an outside source) retrieve the program's output, it allows you to enter input.

I havn't messed with redirecting streams (yet), but I presume it's as easy as opening up some file streams, storing the previous iostreams in new stream, and setting std::cout and std::cin to (std::istream) or (std::ostream) casts of std::ifstream or std::ofstream. If it isn't, then you'll have to do some research.

Upvotes: 1

Related Questions