SWIG“out”typemap返回地址而不是数据

问题描述 投票:0回答:1

我正在使用SWIG从Python接受一个大小可变的列表,将其发送到C ++以对其执行某些操作,然后将其发送回Python以进行打印。

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

该列表可以从Python中显示,但只有在out typemap中给定一个set size时才能显示。目前我需要让它处理各种大小的列表。

当试图实现这一点时,我最终返回地址而不是Python中的列表。

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

custom vector.曹操

#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]

然后我编辑了我的界面文件,以便out typemap现在使用[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这样一个可变大小的数据?

python c++ swig
1个回答
0
投票

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

返回向量的默认行为是使用元组。可以使用typemap覆盖:

%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]
© www.soinside.com 2019 - 2024. All rights reserved.