I'm trying to modify an existing SWIG Python interface for a C++ library, to add Python wrappers for more functions, and I would greatly appreciate some help from someone experienced with SWIG.
Specifically I'm working on a function with a signature like this:
void execute(int x, double y, ResultType& result1, ResultType& result2);
This function accepts two empty ResultType objects and fills them in as a output parameters. In Python, this has to translate to a function that takes only x
and y
, then returns a tuple of result1
and result2
.
ResultType is a container type that is used widely throughout the library.
typemap(in)
From research, I think I understand that I need to add a typemap "in" for result1 and result2, which swallows the arguments and saves them to temporary variables. I also found that the reference is converted to a pointer by SWIG, hence &temp
instead of temp
. Here is my typemap "in":
typemap(in, numinputs=0) ResultType& result1 (ResultType temp) {
$1 = &temp;
}
typemap(in, numinputs=0) ResultType& result2 (ResultType temp) {
$1 = &temp;
}
typemap(argout)
Next, I added a typemap "argout" that appends the values to a return tuple:
%typemap(argout) ResultType& result1 {
$result = SWIG_Python_AppendOutput($result, temp$argnum);
}
%typemap(argout) ResultType& result2 {
$result = SWIG_Python_AppendOutput($result, temp$argnum);
}
However, this obviously won't work, because temp$argnum
will be of the raw C++ type ResultType
, whereas I need to have a PyObject *
in order to append to a tuple. ResultType already has a working SWIG wrapper. So, in Python I can call ResultType()
to construct an instance of it without a problem. Assuming that I am on the right track so far, how do I convert the raw C++ ResultType
object to a PyObject *
belonging to the SWIG-generated wrapper for ResultType
? (Sorry if too much detail, I'm trying to avoid the "XY Problem")