How to use Swig to treat a void * function parameter as Python string

自闭症网瘾萝莉.ら 提交于 2021-02-19 09:05:22

问题


Here is my c++ code:

struct Impl
{
  DT* data_ptr_;

  Impl(void* data_ptr)
    : data_ptr_((DT*)data_ptr)
  {
    //do something to decipher data
  }
};

Impl class takes a void pointer, as a input parameter.

What I want to do is just pass a Python string(binary string as in Python 2.x) as parameter:

data = "I'm a stirng data!"
impl = Impl(data)

But Swig generated module raises this

TypeError: in method 'new_Impl', argument 1 of type 'void *'

I'm new to swig and have searched in SWIG documentation for a whole day now. The only thing worked for me is in swig document 8.3 C String Handling. But my function don't really take a size integer.

I think my problem is rather simple, I must missed something, please help.

Thanks!


回答1:


I found a solution myself.

As I only needed the void* pointer as input:

%typemap(in) void* = char*;

will do the trick.

Swig accept char* parameter as string type, and take void* just as an pointer. so use the char* input type will be okay.



来源:https://stackoverflow.com/questions/12686400/how-to-use-swig-to-treat-a-void-function-parameter-as-python-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!