编译并运行后提升python在__init__中给我一个TypeError

时间:2017-10-21 12:29:24

标签: python c++ boost boost-python

我已成功编译以下程序

Set<InetSocketAddress>

当我尝试使用Python解释器执行classes模块时,收到以下错误:

#include <boost/python.hpp>
#include <boost/python/list.hpp>
#include <boost/python/extract.hpp>
#include <string>
#include <sstream>
#include <vector>

struct World
{
    void set(std::string msg) { mMsg = msg; }
    void many(boost::python::list msgs) {
        long l = len(msgs);
        std::stringstream ss;
        for (long i = 0; i<l; ++i) {
            if (i>0) ss << ", ";
            std::string s = boost::python::extract<std::string>(msgs[i]);
            ss << s;
        }
        mMsg = ss.str();
    }
    std::string greet() { return mMsg; }
    std::string mMsg;
};

using namespace boost::python;

BOOST_PYTHON_MODULE(classes)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
        .def("many", &World::many)
    ;
};

我正在使用:

  • osx 10.13
  • python 3.6 anaconda
  • boost 1.65.1
  • clang 4.0.1

我使用以下clang ++命令编译程序:

Python 3.6.2 |Anaconda custom (x86_64)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import classes
>>> classes.World()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'NoneType'

我检查了我的* .so文件是否正确链接:

clang++ -o classes.so -shared -fPIC\
        -L/usr/local/anaconda/lib \
        -lboost_python3 \
        -lpython3.6m \
        -I/usr/local/anaconda/include \
        -I/usr/local/anaconda/include/python3.6m \
        classes.cpp

我已经检查了以下问题https://stackoverflow.com/questions/20437769,但是,在我的情况下,似乎我已经将我的程序与优秀的Python库相关联(与导入模块的Python安装相同)。

我该如何解决?

0 个答案:

没有答案