python - SWIG "out"类型映射返回地址而不是数据

标签 python c++ swig

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

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

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

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

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

自定义 vector .cc

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

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

自定义 vector .h

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

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

自定义 vector .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]

然后我编辑了我的接口(interface)文件,以便输出类型映射现在使用 [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 中?

最佳答案

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

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

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

关于python - SWIG "out"类型映射返回地址而不是数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55626210/

相关文章:

c++ - 构造函数初始化列表中的段错误

c++ - SWIG 为 Python 包装 C++ : translating a list of strings to an STL vector of STL strings

python - 为什么 Python 脚本可以在 Windows 而不是 Linux 上运行?

python - Django 设置 : raise KeyError, 引发 ImproperlyConfigured 或使用默认值?

c++ - 我可以在单独的 ssl 上下文之间重用 openssl x509store 吗?

c++ - 当鼠标悬停在控件上方时拦截鼠标点击

python - 使用 SWIG 将 C++ 文件包装为 Python 文件

windows - 如何安装痛饮?

使用 cmake 和 pybind11 构建示例应用程序时找不到 Python.h

python - `is` 对象标识表示比较 : direct object comparison vs. 比较