swig,python和wchar_t问题 [英] swig, python and wchar_t problem

查看:179
本文介绍了swig,python和wchar_t问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python C绑定的新手,并且已经尝试解决这一问题已有一段时间了.我有一个外部C库(Example.c),我想从Python调用它.我阅读了Swig教程,并能够立即生成包装器.现在的问题是,当我调用API并得到此信息时:

I am new to the Python C binding swig and have been trying to solve this problem for a while now. I have an external C library (Example.c) that I would like to call from Python. I read Swig tutorial and able to generate the wrapper in no time. The problem now is that when I invoke the API and I got this:

>>> import Example
>>> dir(Example)
['Example_CreateConnection', 'trimmed to fit the screen']
>>> Example.Example_CreateConnection("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'Example_CreateConnection', argument 1 of type 'ExampleChar const *'

似乎找不到类型ExampleChar.以下是我的Swig文件:

It seemed like it cannot find the type ExampleChar. The following is my swig file:

%module Example
%{
#include "ExampleSDK.h"
%}

%include "ExampleTypes.h"
%include "ExampleSDK.h"

ExampleTypes.h看起来像这样:

ExampleTypes.h looks like this:

#ifndef ExampleTypes_H
#define ExampleTypes_H

typedef wchar_t ExampleChar;

#endif /* ExampleTypes_H */

ExampleSDK.h看起来像这样:

ExampleSDK.h looks like this:

#ifndef ExampleSDK_H
#define ExampleSDK_H

#include "ExampleTypes.h"
void Example_CreateConnection(const ExampleChar *temp);

#endif /* ExampleSDK_H */

以下是被调用以生成包装器的命令行:

The following are the command lines being invoked to generate the wrapper:

swig -python -I. Example.i
gcc -c Example.c -I/Developer/SDKs/MacOSX10.6.sdk/usr/include/
gcc -c Example_wrap.c -I/usr/include/python2.6 -I.
gcc -bundle -flat_namespace -undefined suppress -o _Example.so Example_wrap.o Example.o -L/usr/lib/python2.6/config/ -lpython2.6

这是Example.c的样子:

Here is how the Example.c looks like:

#include "runetype.h" // for Mac wchar_t definition

#include "ExampleSDK.h"

void Example_CreateConnection(const ExampleChar *temp)
{
    //do nothing
}

我不确定这是怎么回事.我希望有人能够指出我在这里所做的错误.谢谢.

I am not sure what is wrong with it. I hope someone will be able to point out the mistake(s) I have done over here. Thank you.

此致

川林

推荐答案

上次我将wchat_t与SWIG + Python结合使用时,我最终需要添加以下内容:

Last time I used wchat_t with SWIG+Python I ended up needing to add something like:

%include "pywstrings.swg"
%include "pystrings.swg"
%include "std_string.i"
%include "typemaps.i"  

%fragment("SWIG_AsVal_wchar_t", "header", fragment="<wchar.h>") {
    SWIGINTERN int SWIG_AsVal_wchar_t(PyObject* p, wchar_t* c) {
        return SWIG_OK;
    }
}
%fragment("SWIG_From_wchar_t", "header", fragment="<wchar.h>") {
    SWIGINTERNINLINE PyObject* SWIG_From_wchar_t(wchar_t c) {
        return SWIG_Py_Void();
    }
} 

// Python -> C
%typemap(in) wchar_t const * {
  $1 = PyString_to_wchar_t($input);
}

// C -> Python
%typemap(out) wchar_t * {
  $result = wchar_t_to_PyObject($1);
}

在我的Swig界面文件中.

in my Swig interface file.

这篇关于swig,python和wchar_t问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆