Swig和指针问题(python)
Swig and pointer problems (python)
我正在使用Swig为DLL文件生成python包装器。我所做的是:
- 使用
swig -c++ -python myfile.i
生成包装器和加载程序文件 - 创建一个新的DLL文件,包括myfile_wrapper.cxx和编译为_myfile.pyd
- 加载Swig在Idle中创建的模块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的源代码。
我看到了输入/输出参数,但我必须为每种输出类型定义它们吗?MyFile.h有数百个不同输出类型的函数。它只支持基元数据类型,但MyFile.h中的大多数类型都不是基元类型,而是像结构体MyHandle一样。
我已经研究了带有指针结构和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) %{
$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
的工作原理很像charm,但DoSomething
仍然试图返回MyHandle
,而不是将其作为in参数,我不明白为什么。CCD_ 5仅针对CCD_。
问题2:我不知道如何制作之类的类型映射
int GetFileName(char *szPathBuffer, int iLength);
如何创建一个字符缓冲区并将其发送进来,如IC:
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);
@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
标头的破解实现。。。
#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)
相关文章:
- Python 3.7 和 excess_args 的 SWIG 问题
- 使用转换器提升 Python 问题 - 静态链接
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- 在Visual Studio 2017中使用c ++组件构建python解决方案时出现问题
- 从 C++ 到 Python 的 SWIG:未定义的符号导入问题
- 在C++中实现类似 python "map"函数的问题:调用类成员函数
- SWIG c++到python:向量问题
- 我最近更改了编译器路径以运行 c++ 代码,但现在我无法运行任何 python 代码。我该如何解决这个问题?
- python 和 swig 版本兼容性问题
- 从 c++ 运行 python 脚本时出现问题
- C++、Python 3.7.4、SWIG 4.0.0 和 Windows 10 的问题 - ModuleNotFoun
- Or-Tools:CPP中琐碎的问题是不可行的,但在Python中起作用
- 与 boost.python 和 python3 链接时出现问题
- 链接boost.python示例的问题
- Python和C 集成.与动态库的问题
- Swig和指针问题(python)
- Python C-API:PYDICT_GETITEM上的分割故障,可能的参考问题
- 嵌入式Python问题
- 将C 类包装到Python的问题
- 在 Python 中使用 OpenCV 将打包的 BGRA 图像缓冲区转换为 RGB 时遇到问题