swig、python 和 wchar_t 问题

发布于 2024-10-17 06:33:01 字数 1636 浏览 2 评论 0原文

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

>>> 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 文件:

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

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

ExampleTypes.h 如下所示:

#ifndef ExampleTypes_H
#define ExampleTypes_H

typedef wchar_t ExampleChar;

#endif /* ExampleTypes_H */

ExampleSDK.h 如下所示:

#ifndef ExampleSDK_H
#define ExampleSDK_H

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

#endif /* ExampleSDK_H */

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

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 如下所示:

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

#include "ExampleSDK.h"

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

我不确定它是什么错了。我希望有人能够指出我在这里犯的错误。谢谢。

问候,

林川

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 *'

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 looks like this:

#ifndef ExampleTypes_H
#define ExampleTypes_H

typedef wchar_t ExampleChar;

#endif /* ExampleTypes_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

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.

Regards,

Chuan Lim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我也只是我 2024-10-24 06:33:01

上次我将 wchat_t 与 SWIG+Python 一起使用时,我最终需要

%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 接口文件中添加以下内容:

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);
}

in my Swig interface file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文