Boost.Python __init __()应该返回None,而不是'NoneType' [英] Boost.Python __init__() should return None, not 'NoneType'

查看:449
本文介绍了Boost.Python __init __()应该返回None,而不是'NoneType'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆正在工作的C ++代码,我想为其编写Python绑定.我正在尝试使用Boost.Python,因为它似乎是使此工作最简单的方法,但它并不合作.这是我要构建的扩展模块的代码的一部分:

I have a whole bunch of working C++ code that I want to write Python bindings for. I'm trying to use Boost.Python since it seems to be the easiest way to get this working, but it isn't cooperating. Here's part of the code for the extension module I'm trying to build:

BOOST_PYTHON_MODULE(libpcap_ext) {
    using namespace boost::python;
    class_<PacketEngine>("PacketEngine")
        .def("getAvailableDevices", &PacketEngine_getAvailableDevices);
}

Bjam似乎很痛苦,拒绝识别我的Python路径或允许我与libpcap链接,所以我正在使用CMake.这是我的CMakeLists文件,可以很好地导入和构建所有文件(按预期输出libpcap.so):

Bjam seems to be a pain and refuses to recognize my Pythonpath or allow me to link with libpcap, so I'm using CMake. Here's my CMakeLists file, which can import and build everything just fine (outputs libpcap.so as expected):

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE "DEBUG")
  #SET(CMAKE_BUILD_TYPE "RELEASE")
  #SET(CMAKE_BUILD_TYPE "RELWITHDEBINFO")
  #SET(CMAKE_BUILD_TYPE "MINSIZEREL")
ENDIF()

FIND_PACKAGE(Boost 1.55.0)
find_package(PythonLibs REQUIRED)
IF(Boost_FOUND)
  INCLUDE_DIRECTORIES("${Boost_INCLUDE_DIRS}" "${PYTHON_INCLUDE_DIRS}")
  SET(Boost_USE_STATIC_LIBS OFF)
  SET(Boost_USE_MULTITHREADED ON)
  SET(Boost_USE_STATIC_RUNTIME OFF)
  FIND_PACKAGE(Boost 1.55.0 COMPONENTS python)

  ADD_LIBRARY(pcap_ext MODULE PacketWarrior/pcap_ext.cc PacketWarrior/PacketEngine.h PacketWarrior/PacketEngine.cc PacketWarrior/Packet.h PacketWarrior/Packet.cc)
  TARGET_LINK_LIBRARIES(pcap_ext pcap)
  TARGET_LINK_LIBRARIES(pcap_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ELSEIF(NOT Boost_FOUND)
  MESSAGE(FATAL_ERROR "Unable to find correct Boost version. Did you set BOOST_ROOT?")
ENDIF()

ADD_DEFINITIONS("-Wall")

还有我的pcap.py文件,它试图利用该模块:

And my pcap.py file that attempts to utilize the module:

import libpcap_ext
engine = libpcap_ext.PacketEngine()
print engine.getAvailableDevices()

但是,每当我尝试运行模块时,都会出现以下错误:

But whenever I try to run the module, I get the following error:

Traceback (most recent call last):
  File "../pcap.py", line 2, in <module>
    engine = libpcap_ext.PacketEngine()
TypeError: __init__() should return None, not 'NoneType

我认为这是因为Boost.Python试图使用Python 3,而我的系统默认值为Python 2.7.3.我尝试将我的user-config.jam文件(在我的boost_1_55_0目录中)更改为指向Python 2.7,并尝试构建:

I'm assuming it's because Boost.Python is trying to use Python 3 and my system default is Python 2.7.3. I've tried changing my user-config.jam file (in my boost_1_55_0 directory) to point to Python 2.7 and tried building:

# Configure specific Python version.
# using python : 2.7 : /usr/bin/python2.7 : /usr/include/python2.7 : /usr/lib ;

Boost.Python的安装说明[0]似乎对我来说失败了,当我尝试使用bjam构建快速入门时(很多警告),因此我尝试按照Boost入门指南[1]来构建Python标头二进制文件,该文件我认为是什么导致了这个问题.关于如何解决此问题的任何建议都将是惊人的,我已经花了几个小时了.

Boost.Python's installation instructions [0] seem to fail for me when I try to build quickstart with bjam (lots of warnings), so I tried following the Boost Getting Started instructions [1] to build a Python header binary, which is I think what is causing this problem. Any recommendations as to how to fix this would be amazing, I've spent hours on this.

推荐答案

此错误可能是由于链接到错误的Python库引起的.确保您的扩展程序以及Boost Python库与用于导入模块的Python安装已链接.

This error is probably due to linking against the wrong Python library. Make sure your extension as well as the Boost Python library are linked against the Python installation you are using to import the module.

在Linux上,您可以检查与ldd链接的库.在OS X上,otool -L会执行相同的操作.例如,

On Linux you can check against which libraries you've linked with ldd. On OS X otool -L does the same thing. So, for example

otool -L libpcap_ext.so
otool -L /path/to/libboost_python-mt.dylib

应列出与它们链接的Python库.

should list the Python library they are linked against.

使用CMake可以使用变量PYTHON_LIBRARY更改使用哪个Python库.例如,您可以在命令行上使用

With CMake you can use the variable PYTHON_LIBRARY to change which Python library is used. As an example, on the command line you can set it with

cmake -DPYTHON_LIBRARY="/path/to/libpython2.7.dylib" source_dir

最后,在OS X上,快速且肮脏的方式(即无需重新编译)来更改动态链接的库是install_name_tool -change.

Lastly, on OS X a quick and dirty way (i.e. without recompiling) to change the dynamically linked libraries is install_name_tool -change.

这篇关于Boost.Python __init __()应该返回None,而不是'NoneType'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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