【问题标题】:Swig and pointer problems (python)Swig 和指针问题(python)
【发布时间】:2017-10-24 12:21:52
【问题描述】:

我正在使用 Swig 为 DLL 文件生成 python 包装器。我要做的是:

  1. 使用swig -c++ -python myfile.i生成包装器和加载器文件
  2. 创建一个新的 DLL 文件,包括 myfile_wrapper.cxx 和 编译为 _myfile.pyd。
  3. 在 Idle 中加载 Swig 创建的模块 myfile.py 并尝试使用它。

接口文件如下:

%module myfile 
/* Make a test with cpointer - needed?? */
%include cpointer.i 
%pointer_functions(MyHandle, my_handle_p);
%pointer_class(int, intp);
%{
#define SWIG_FILE_WITH_INIT
#include "MyFile.h"
}%
%include "MyFile.h"

函数看起来像

 typedef struct tagMyHandle
   {
      void* reserved;
   } *MyHandle;

int OpenFile(const char *szPath, MyHandle* pFile); // pFile is an out parameter
int GetNumberOfItems(MyHandle hFile, int *pnNrOfItems); // pnNrOfItems is an out parameter

如果我尝试从 Python 中使用它,我必须这样做:

import myfile
handle_p = myfile.new_my_handle_p()
myfile.OpenFile("Path", handle_p)
handle = myfile.my_file_p_value(handle_p)
num_items_p = myfile.new_intp()
myfile.GetNumberOfItems(handle, num_items_p)
num_items = num_items_p.value()

我是否错误地使用了 Swig?感觉调用应该为Python包装的函数是一种很繁琐的方式。

我想做这样的事情:

result, handle = OpenFile("path")
result, items = GetNumberIfItems(handle)

我无法更改 myfile.h 的源代码。

我查看了input/output parameters,但我必须为每种输出类型定义它们吗? MyFile.h 有数百个具有不同输出类型的函数。而且它只支持原始数据类型,但是MyFile.h中的大多数类型都不是原始类型,而是像struct MyHandle一样。

我也看过SWIG function with pointer structhttp://www.swig.org/Doc3.0/Python.html#Python_nn18,但没有任何好的解决方案。

更新 1 经过多方帮助,大部分问题我都解决了,但还有一些我不明白的地方。

问题一:

// For the out parameter, shouldn't be needed?
%typemap(in,numinputs=0) MyHandle* pOutParam (MyHandle h) %{
    $1 = &h;
%}

// For all others
%typemap(in,numinputs=0) MyHandle* (MyHandle h) %{
    $1 = &h;
%}

// For the return type
%typemap(argout) MyHandle* pOutParam (PyObject* o) %{
    o = PyLong_FromVoidPtr(*$1);
    $result = SWIG_Python_AppendOutput($result,o);
%}

%typemap(in) MyHandle %{
    $1 = reinterpret_cast<MyHandle>(PyLong_AsVoidPtr($input));
%}

和代码

int OpenFile(const char *szPath, MyHandle* pOutParam);
int DoSomething(MyHandle* pInParam);

OpenFile 像魅力一样工作,但 DoSomething 仍然尝试返回 MyHandle 而不是将其作为 in 参数,我不明白为什么。 %typemap(argout) MyHandle* 仅为pOutParam 定义。

问题 2: 我不明白如何为类似的东西制作类型映射

int GetFileName(char *szPathBuffer, int iLength);

如何创建一个 char 缓冲区并将其发送进去,就像 I C:

char szBuffer[MAX_PATH]; GetFileName(szBuffer, MAX_PATH);

也许与cstring_bounded_output 一起做一些事情,或者我应该做一些类似的事情

%typemap(in) (char*, int) { 
    $2 = PyString_Size($input); 
    $1 = (char*) malloc($2 * sizeof(char*)); 
} 

但它在哪里被释放?

问题 3: 枚举值的正确映射是什么。如果我有

typedef enum tagMyEnum {
MyTrue = 1,
MyFalse = 0 } MyEnum;

和功能

int IsCorrect(MyEnum* pOutValue);

@马克托隆宁: 感谢大家的帮助!对此,我真的非常感激!我学到了很多关于 Swig 的新东西!

【问题讨论】:

  • 是和不是。这只是使用 SWIG 的一种方式。您可以创建更高级的类型映射,它允许更简单的界面,例如参见如何使用numpy.i 包装 NumPy。如果您不熟悉编写高级类型映射,我可以推荐 %extend 功能,其中扩展是用目标语言制作的

标签: python c++ swig


【解决方案1】:

下面是一个示例,其界面类似于您想使用类型映射重新定义界面来说明的内容:

myfile.h

typedef struct tagMyHandle
{
    void* reserved;
} *MyHandle;

int OpenFile(const char *szPath, MyHandle* pFile);
int GetNumberOfItems(MyHandle hFile, int *pnNrOfItems);
// Added this to free the allocated handle.
void CloseFile(MyHandle hFile);

myfile.cpp

header 的 hack 实现...

#include "myfile.h"

int OpenFile(const char *szPath, MyHandle* pFile)
{
    *pFile = new tagMyHandle;
    (*pFile)->reserved = new int(7);
    return 1;
}

int GetNumberOfItems(MyHandle hFile, int *pnNrOfItems)
{
    *pnNrOfItems = *reinterpret_cast<int*>(hFile->reserved) + 5;
    return 1;
}

// mirrors OpenFile to free the allocated handle.
void CloseFile(MyHandle hFile)
{
    delete reinterpret_cast<int*>(hFile->reserved);
    delete hFile;
}

myfile.i

%module myfile 

%{
#include "MyFile.h"
%}

// An input typemap for the an output parameter, called before the C++ function is called.
// It suppresses requiring the parameter from Python, and uses a temporary
// variable to hold the output value.
%typemap(in,numinputs=0) MyHandle* (MyHandle h) %{
    $1 = &h;
%}

// An output argument typemap, called after the C++ function is called.
// It retrieves the output value and converts it to a Python int,
// then appends it to the existing return value.  Python will get a tuple of
// (return_value,handle).
%typemap(argout) MyHandle* (PyObject* o) %{
    o = PyLong_FromVoidPtr(*$1);
    $result = SWIG_Python_AppendOutput($result,o);
%}

// An input typemap that converts a Python int to a MyHandle*.
%typemap(in) MyHandle %{
    $1 = reinterpret_cast<MyHandle>(PyLong_AsVoidPtr($input));
%}

// This applies a pre-defined int* output typemap to all int* parameters.
%apply int *OUTPUT {int *};

%include "MyFile.h"

输出

>>> import myfile
>>> s,h = myfile.OpenFile('path')
>>> s,h
(1, 7706832)
>>> s,v = myfile.GetNumberOfItems(h)
>>> s,v
(1, 12)
>>> myfile.CloseFile(h)

【讨论】:

  • 谢谢!这就像魅力!那我就知道怎么解决我的问题了!
  • 我还有一个问题!如何处理像 GetName(char* buffer, int length) 这样的东西来填充缓冲区并发回?我也需要一个类型图吗?
  • 我还有一个问题。如何使这项工作 OtherFunc(MyHandle* inParamaterCanBeNull)。如何使它与输出参数不同?
  • 例如,是否可以为不同的函数名称设置不同的类型名称?这可以解决问题。
  • 对于缓冲区/长度,您可以使类型映射 a 与一对参数一起使用。对于输入与输出参数,您可以使输入参数 const 来区分,也可以制作一个仅应用于特定命名参数的类型映射。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
  • 2017-05-02
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
相关资源
最近更新 更多