问题
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")
回答1:
Just like $1 is a reference to the Python input object in the input typemap, $1 is a reference to the C++ output variable in the argout typemap. Using this, you can generate a Python object for that data and append it to the result.
Here's a functional example for Windows:
test.h
#ifdef EXPORT
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
struct ResultType
{
int x;
double y;
};
API void execute(int x, double y, ResultType& result1, ResultType& result2);
test.cpp
#define EXPORT
#include "test.h"
API void execute(int x, double y, ResultType& result1, ResultType& result2)
{
result1.x = 2 * x;
result1.y = 2 * y;
result2.x = 3 * x;
result2.y = 3 * y;
}
test.i
%module test
%{
#include "test.h"
%}
%include <windows.i>
%typemap(in,numinputs=0) ResultType& %{
// Create a persistent object to hold the result;
$1 = new ResultType;
%}
%typemap(argout) ResultType& (PyObject* tmp) %{
// Store the persistent object in a PyObject* that will be destroyed
// when it goes out of scope.
tmp = SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN);
$result = SWIG_Python_AppendOutput($result, tmp);
%}
%include "test.h"
Output
>>> import test
>>> r = test.execute(2,3)
>>> r[0].x
4
>>> r[0].y
6.0
>>> r[1].x
6
>>> r[1].y
9.0
来源:https://stackoverflow.com/questions/49121312/python-swig-convert-c-return-parameter-to-return-value-and-convert-raw-c-t