0

我正在尝试使用 sip 创建从 c++ 到 python 3.8 的 python 绑定。 我在这里找到了一个简单的示例 并对其进行了更新以使其与我使用 pip 安装的 sip 版本 5.4 一起工作。 详细信息可以在这里找到

我将名称从 word 更改为 basicword,因为我用字符串重写并测试了单词 example。为此,我必须编写一堆特定于 sip 的代码来使字符串库的导入工作,并认为必须有一种更简单的方法。

我的假设是,使用 char * (就像在原始教程中一样)对于 sip 来说会“更容易”,我错过了什么?

我的 sip 文件 basicword.sip:

// Define the SIP wrapper to the basicword library.

%Module(name=basicword, language="C++")

class Basicword {

%TypeHeaderCode
#include <basicword.h>
%End

public:
    Basicword(const char *w);

    char *reverse() const;
};

我的 pyproject.toml 文件:

# Specify sip v5 as the build system for the package.
[build-system]
requires = ["sip >=5, <6"]
build-backend = "sipbuild.api"

# Specify the PEP 566 metadata for the project.
[tool.sip.metadata]
name = "basicword"

# Configure the building of the basicword bindings.
[tool.sip.bindings.basicword]
headers = ["basicword.h"]
include-dirs = ["."]
libraries = ["basicword"]
library-dirs = ["."]

我的 basicword.h 文件:

#ifndef BASICWORD_H
#define BASICWORD_H


// Define the interface to the basicword library.

class Basicword {

private:
    const char *the_word;

public:
    Basicword(const char *w);

    char *reverse() const;
};


#endif //BASICWORD_H

我的 basicword.cpp 文件:

#include "basicword.h"

#include <cstring>

Basicword::Basicword(const char *w) {
    the_word = w;
}

char* Basicword::reverse() const {
    int len = strlen(the_word);
    char *str = new char[len+1];
    for(int i = len-1;i >= 0 ;i--) {
        str[len-1-i] = the_word[i];
    }
    str[len+1]='\0';
    return str;
}

我的文件 test.py:

from basicword import Basicword

w = Basicword("reverse me") // -> error thrown here


if __name__ == '__main__':
    print(w.reverse())

错误信息:

Traceback (most recent call last):
  File "<path to testfile>/test.py", line 3, in <module>
    w = Basicword("reverse me")
TypeError: arguments did not match any overloaded call:
  Basicword(str): argument 1 has unexpected type 'str'
  Basicword(Basicword): argument 1 has unexpected type 'str'

谢谢您的回答!

再见强尼

4

1 回答 1

0

简而言之,Python2 默认使用 str-type 和 python3 byte-type,所以我不得不更改 python3 的默认编码。

我在这里找到了相关的细节

编码

This string annotation specifies that the corresponding argument (which should be either char, const char, char * or const char *)

指一个编码字符或具有指定编码的 '\0' 终止的编码字符串。编码可以是“ASCII”、“Latin-1”、“UTF-8”或“None”。“无”的编码意味着相应的参数是指未编码的字符或字符串。

The default encoding is specified by the %DefaultEncoding directive. If the directive is not specified then None is used.

Python v3 will use the bytes type to represent the argument if the encoding is "None" and the str type otherwise.

Python v2 will use the str type to represent the argument if the encoding is "None" and the unicode type otherwise.

在 basicword.sip 中添加以下简单...

// Define the SIP wrapper to the basicword library.

%Module(name=basicword, language="C++")

%DefaultEncoding "UTF-8" // Missing Encoding!

class Basicword {

%TypeHeaderCode
#include <basicword.h>
%End

public:
    Basicword(const char *w);

    char *reverse() const;
};

现在一切正常。

于 2020-10-22T08:10:46.933 回答