【问题标题】:SWIG "out" typemap returns address instead of dataSWIG“out”类型映射返回地址而不是数据
【发布时间】:2019-04-11 06:43:30
【问题描述】:

我正在使用 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) $1[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($1_dim0);  //changed from 3 to $1_dim0
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PyFloat_FromDouble((double) $1[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 中这样一个可变大小的数据?

【问题讨论】:

  • 由于您的函数按值获取向量,因此当您尝试使用返回的指针时,您的行为未定义。您还需要让函数的调用者知道数组的实际大小。
  • 无论如何,[ANY] 类型映射在这里不是正确的选择,因为在编译时不知道大小,因此不会被应用。
  • 这是否意味着我需要计算 cc 文件中向量的大小,然后将其传递给接口文件以确定它有多大?

标签: python c++ swig


【解决方案1】:

您的实现中有未定义的行为。为什么不使用 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)

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

%module test

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

%typemap(out) std::vector<float> %{
    $result = PyList_New($1.size());
    for (int i = 0; i < $1.size(); ++i)
        PyList_SET_ITEM($result,i,PyFloat_FromDouble($1[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]

【讨论】:

  • 感谢您的意见!这是有原因的(尽管也许可以避免,但我还没有找到解决方案,或者对 c++ 没有太多经验)。我为此使用了 Tensorflow,显然在编译期间返回向量会导致编译失败。因此,我试图避免使用向量作为输出(慢慢测试,如果向量的输入有效,我会保持原样)。目前建议我尝试通过引用或指针返回此值。我是 C++ 新手,现在正在研究如何操作。
猜你喜欢
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多