SWIG "out" typemap returns 地址而不是数据

SWIG "out" typemap returns address instead of data

我正在使用 SWIG 接受大小可变的列表 Python,将其发送到 C++ 以对其进行处理,然后将其发送回 Python 以进行打印.

我是 Python、C++ 和 Swig 的新手。目前,发送的列表将在我的 C++ 函数中作为向量参数处理。之后从函数返回的是一个指针,由 "out" 类型映射处理。

该列表可以从 Python 显示,但前提是在输出类型图中给定了一个设置大小。目前我需要让它处理各种大小的列表。

在尝试实现此目的时,我最终返回了地址而不是 Python 中的列表。

下面展示了在给定固定大小时有效的代码。

customvector.cc

#include "customvector.h"
#include <algorithm>
#include <functional>

float * summy(std::vector<float> a)
{
    float * p = a.data();
    return p;
}

customvector.h

#include <stdio.h>
#include <iostream>
#include <vector>

float * summy(std::vector<float> a);

customvector.i

/* File: customvector.i */
%module customvector

%{
#define SWIG_FILE_WITH_INIT
#include "customvector.h"
%}

%include "std_vector.i"
%include <stdint.i>

namespace std {
%template(Line) vector < float >;

}

%typemap(out) float* summy{ 
  int i;
  $result = PyList_New(3); 
  for (i = 0; i < 3; i++) {
    PyObject *o = PyFloat_FromDouble((double) [i]);
    PyList_SetItem($result,i,o);
  }
}

float * summy(std::vector<float> a);

我的 python 结果:

>>> import customvector
>>> a = [1,2,3]
>>> customvector.summy(a)
[1.0, 2.0, 3.0]

然后我编辑了我的接口文件,这样输出类型映射现在使用 [ANY] 而不是 3 来允许长度变化。

已编辑 customvector.i

/* File: customvector.i */
%module customvector

%{
#define SWIG_FILE_WITH_INIT
#include "customvector.h"
%}

%include "std_vector.i"
%include <stdint.i>

namespace std {
%template(Line) vector < float >;

}

%typemap(out) float* summy [ANY]{ //changed from 3 to [ANY]
  int i;
  $result = PyList_New(_dim0);  //changed from 3 to _dim0
  for (i = 0; i < _dim0; i++) {
    PyObject *o = PyFloat_FromDouble((double) [i]);
    PyList_SetItem($result,i,o);
  }
}

float * summy(std::vector<float> a);

来自 Python 的结果:

>>> import customvector
>>> a = [1,2,3]
>>> customvector.summy(a)
<Swig Object of type 'float *' at 0x000001E4E32E6420>

这不是我想要的,它应该显示之前显示的内容。

我尝试按照此处某处列出的文档进行操作:http://www.swig.org/Doc2.0/Typemaps.html#Typemaps_nn40 让 SWIG 获得比输出更有价值的值,但它似乎不起作用。

我也遇到了这个允许长度变化的解决方案:Python/SWIG: Output an array 但是我不确定它是如何工作的,因为我尝试使用它但是代码没有编译(说没有定义 templen)。

如何从 C++ 输出到 python 这样大小可变的数据?

您的实现中有未定义的行为。为什么不对 return 值也使用 std::vector?

%module test

%include <std_vector.i>
%template() std::vector<float>;

%inline %{
std::vector<float> summy(std::vector<float> a)
{
    for(auto& i: a)
        i += 1;
    return a;
}
%}

演示:

>>> import test
>>> test.summy([1,2,3,4])
(2.0, 3.0, 4.0, 5.0)

returned 向量的默认行为是使用元组。可以用类型映射覆盖:

%module test

%include <std_vector.i>
%template() std::vector<float>;

%typemap(out) std::vector<float> %{
    $result = PyList_New(.size());
    for (int i = 0; i < .size(); ++i)
        PyList_SET_ITEM($result,i,PyFloat_FromDouble([i]));
%}

%inline %{
#include <vector>
std::vector<float> summy(std::vector<float> a)
{
    for(auto& i: a)
        i += 1;
    return a;
}
%}

演示:

>>> import test
>>> test.summy([1,2,3,4])
[2.0, 3.0, 4.0, 5.0]