Swig 和指针问题 (python)
Swig and pointer problems (python)
我正在使用 Swig 为 DLL 文件生成 python 包装器。我做的是:
- 使用
swig -c++ -python
myfile.i
生成包装器和加载器文件
- 创建一个新的 DLL 文件,包括 myfile_wrapper.cxx 和
编译为 _myfile.pyd。
- 在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 struct and http://www.swig.org/Doc3.0/Python.html#Python_nn18,但是没有什么好的解决办法。
更新 1
经过多方帮助,我已经解决了大部分问题,但还有一些我不明白。
问题 1:
// For the out parameter, shouldn't be needed?
%typemap(in,numinputs=0) MyHandle* pOutParam (MyHandle h) %{
= &h;
%}
// For all others
%typemap(in,numinputs=0) MyHandle* (MyHandle h) %{
= &h;
%}
// For the return type
%typemap(argout) MyHandle* pOutParam (PyObject* o) %{
o = PyLong_FromVoidPtr(*);
$result = SWIG_Python_AppendOutput($result,o);
%}
%typemap(in) MyHandle %{
= reinterpret_cast<MyHandle>(PyLong_AsVoidPtr($input));
%}
和代码
int OpenFile(const char *szPath, MyHandle* pOutParam);
int DoSomething(MyHandle* pInParam);
OpenFile
像魅力一样工作,但 DoSomething
仍然尝试 return MyHandle
而不是将其作为 in 参数,我不明白为什么。 %typemap(argout) MyHandle*
仅为 pOutParam
.
定义
问题二:
我不明白如何为
之类的东西制作类型映射
int GetFileName(char *szPathBuffer, int iLength);
如何创建一个字符缓冲区并将其发送,就像我 C:
char szBuffer[MAX_PATH]; GetFileName(szBuffer, MAX_PATH);
也许与 cstring_bounded_output
一起做一些事情,或者我应该做一些类似
的事情
%typemap(in) (char*, int) {
= PyString_Size($input);
= (char*) malloc( * sizeof(char*));
}
但是它在哪里释放?
问题三:
枚举值的正确映射是什么。如果我有
typedef enum tagMyEnum {
MyTrue = 1,
MyFalse = 0 } MyEnum;
和函数
int IsCorrect(MyEnum* pOutValue);
@Mark Tolonen:
感谢所有帮助!对此,我真的非常感激!我学到了很多关于 Swig 的新知识!
下面是一个示例,其接口类似于您要说明的使用类型映射重新定义接口的示例:
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) %{
= &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(*);
$result = SWIG_Python_AppendOutput($result,o);
%}
// An input typemap that converts a Python int to a MyHandle*.
%typemap(in) MyHandle %{
= 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)
我正在使用 Swig 为 DLL 文件生成 python 包装器。我做的是:
- 使用
swig -c++ -python myfile.i
生成包装器和加载器文件 - 创建一个新的 DLL 文件,包括 myfile_wrapper.cxx 和 编译为 _myfile.pyd。
- 在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 struct and http://www.swig.org/Doc3.0/Python.html#Python_nn18,但是没有什么好的解决办法。
更新 1 经过多方帮助,我已经解决了大部分问题,但还有一些我不明白。
问题 1:
// For the out parameter, shouldn't be needed?
%typemap(in,numinputs=0) MyHandle* pOutParam (MyHandle h) %{
= &h;
%}
// For all others
%typemap(in,numinputs=0) MyHandle* (MyHandle h) %{
= &h;
%}
// For the return type
%typemap(argout) MyHandle* pOutParam (PyObject* o) %{
o = PyLong_FromVoidPtr(*);
$result = SWIG_Python_AppendOutput($result,o);
%}
%typemap(in) MyHandle %{
= reinterpret_cast<MyHandle>(PyLong_AsVoidPtr($input));
%}
和代码
int OpenFile(const char *szPath, MyHandle* pOutParam);
int DoSomething(MyHandle* pInParam);
OpenFile
像魅力一样工作,但 DoSomething
仍然尝试 return MyHandle
而不是将其作为 in 参数,我不明白为什么。 %typemap(argout) MyHandle*
仅为 pOutParam
.
问题二: 我不明白如何为
之类的东西制作类型映射int GetFileName(char *szPathBuffer, int iLength);
如何创建一个字符缓冲区并将其发送,就像我 C:
char szBuffer[MAX_PATH]; GetFileName(szBuffer, MAX_PATH);
也许与 cstring_bounded_output
一起做一些事情,或者我应该做一些类似
%typemap(in) (char*, int) {
= PyString_Size($input);
= (char*) malloc( * sizeof(char*));
}
但是它在哪里释放?
问题三: 枚举值的正确映射是什么。如果我有
typedef enum tagMyEnum {
MyTrue = 1,
MyFalse = 0 } MyEnum;
和函数
int IsCorrect(MyEnum* pOutValue);
@Mark Tolonen: 感谢所有帮助!对此,我真的非常感激!我学到了很多关于 Swig 的新知识!
下面是一个示例,其接口类似于您要说明的使用类型映射重新定义接口的示例:
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) %{
= &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(*);
$result = SWIG_Python_AppendOutput($result,o);
%}
// An input typemap that converts a Python int to a MyHandle*.
%typemap(in) MyHandle %{
= 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)