类型映射资源并将列表转换为矢量(并返回)

Typemap resources and converting a list to vector (and back)

本文关键字:返回 转换 映射 资源 列表 类型      更新时间:2023-10-16

我正在使用SWIG将c ++包装在python中,并且需要使用类型映射以使我的python脚本尽可能简单。作为第一次尝试,我只是发送 2 个列表,将它们转换为向量,添加两个向量,然后将结果返回到新列表中。

我的问题是,我一直发现SWIG手册不是很有启发性,很难遵循,并且没有给出任何关于如何编写自己的类型图的可靠,完整的示例。

我的问题是:

  1. 我将如何确保我的列表正确转换为矢量,然后再转换为矢量?
  2. 对于如何编写类型图,以及所有语法/函数的含义,是否有更好的教程/参考?

这是我的代码:

add_array.h

#include <vector>
#include <functional>
std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2);

add_array.i

%module add_array
%{
#include "add_array.h"
%}
%include std_vector.i 
%template(vectorInt) std::vector<int>;
%include "add_array.h"

add_array.cpp

#include "add_array.h"
#include <cassert>
#include <cstring>
std::vector<int> add_array(std::vector<int> src1, std::vector<int> src2) {  
assert(src1.size() == src2.size());
std::vector<int> dst;
dst.resize(src1.size());
for (size_t i = 0; i < src1.size(); i++) {
dst[i] = src1[i] + src2[i];
}
return dst;
}

生成文件

all:
rm -f *.so *.o *_wrap.* *.pyc *.gch add_array.py
swig -c++ -python add_array.i
g++ -fpic -c add_array_wrap.cxx add_array.h add_array.cpp -I/home/tools/anaconda3/pkgs/python-3.7.3-h0371630_0/include/python3.7m/
g++ -shared add_array_wrap.o add_array.o -o _add_array.so

array.py(这是我正在运行的文件(

import add_array
a = [1, 2, 3, 4, 5, 6]
b = [5, 6, 7, 8, 9, 10]
c = add_array.add_array(a, b)
print(c)

输出:(6, 8, 10, 12, 14, 16(

这是作为一个元组出现的(我希望它是一个列表(。 看起来我很幸运,它可以将输入列表转换为向量(虽然另一个方向不那么幸运(,但我真的很想知道这是怎么发生的,以及如果需要,我如何为未来的代码进行更改。

谢谢!

我不知道是否有具体原因,但包含的std_vector.i将输出向量转换为元组而不是列表。如果需要列表,则需要编写自定义类型映射表。

示例(无错误检查(:

%module add_array
%{
#include "add_array.h"
%}
%include <std_vector.i>
%template(vectorInt) std::vector<int>;
// Override the template output typemap with one that returns a list.
// An "out" typemap controls how a value is returned.
// When a function returns std::vector<int> this template will convert it to
// a Python object.  In this case, a PyList.
// 
// Note: PyObject* tmp declares a local variable that will be used by this code snippet.
// Make sure to look at the generated wrapper code and find the add_array_wrap function
// and how this code is integrated into it.
// 
%typemap(out) std::vector<int> (PyObject* tmp) %{
// Allocate a PyList object of the requested size.
// $1 references the first type in the type list (in this case, std::vector<int>)
// and represents the c++ return value of a function that returns
// this type; therefore, we can call methods on that value to get the size.
//
// Note: The elements of the new PyList are null pointers and MUST be
//       populated before returning it to Python.
//
tmp = PyList_New($1.size());
// Populate the PyList.  PyLong_FromLong converts a C++ "long" to a
// Python PyLong object.  PyList_SET_ITEM takes a PyList object (tmp),
// an index (i), and a Python object to put in the list.  This particular
// function *steals* the reference to the Python object, so you don't have to
// Py_DECREF the object to free it later.
//
for(int i = 0; i < $1.size(); ++i)
PyList_SET_ITEM(tmp,i,PyLong_FromLong($1[i]));
// $result is where you assign the Python object that should be returned
// after converting the C++ $1 object.  SWIG_Python_AppendOutput is not
// well-documented, but it appends the return object to an existing
// returned object.  It's most useful for "argout" typemaps where multiple
// output or in/out arguments in C++ can be returned as a tuple of
// return values in Python.  For example, a function like:
//
//     void func(int* pValue1, int* pValue2);
//
// would normally return None ($result = Py_None), but an "argout" typemap
// could *$1 to a PyLong and use SWIG_Python_AppendOutput to add it to
// the result.  The template would be applied twice and you'd get a tuple.
//
$result = SWIG_Python_AppendOutput($result,tmp);
%}
%include "add_array.h"

输出:

>>> import add_array
>>> add_array.add_array([1,2,3],[4,5,6])
[5, 7, 9]

就教程而言,我只读过 SWIG 文档和 C 语言扩展的特定语言文档。 作为文档,它实际上非常好,但你不能只是挑选要阅读的内容。 研究前十几个部分的基础知识,然后跳到特定于语言的部分(例如 Python(。SWIG安装下还有一个示例目录。

引用:

  • PyLong_FromLong
  • PyList_New
  • PyList_SET_ITEM
  • std_vector.i

您必须查看SWIG资源以获取有关SWIG_Python_AppendOutput的任何信息。 或者只是谷歌搜索其他人的例子。