python - Boost::Python 和带变量的函数

标签 python boost

目标:我正在尝试使用 Boost::Python 库在我的 C++ 代码中交互地使用 python。 我的目标是从 python 解释器更改我在 c++ 中定义的某个类的变量。

代码附在下面。

问题:我可以在 python 接口(interface)中加载库(即加载 hello)并从中创建一个对象(obj = hello.World())。我什至可以访问没有变量的函数( obj.greet() ),但是当我想访问带有变量的函数(obj.Set("Hello") )时,我遇到内存访问冲突(“访问冲突读取位置 0xffffffffffffffff”)。即使该函数是一个只接受一些字符串的空函数。

struct World
{
    void set(string _msg) {}
    string greet() { return msg; }
    string msg;
};
typedef boost::shared_ptr<World> World_ptr;
BOOST_PYTHON_MODULE(hello)
{
    bp::class_<World, World_ptr>("World")
        .def("greet", &World::greet)
        .def("set", &World::set)
    ;
}
int main(int argc, char **argv)
{
    Py_Initialize();    

    bp::object main = bp::object(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__"))));
    bp::object global(main.attr("__dict__"));
    inithello();
    // Bring up Python interpreter
    Py_Main(argc, argv);

    Py_Finalize();

    return 0;
}

实际上,这来自 Boost::Python 教程,经过一些修改以使用 Python 解释器 http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

非常感谢

--------------------- 编辑 ------------------ ---

我进行了很多测试,发现问题仅在于字符串输入。即 char* 或 int 没有问题。 boost python中的字符串有问题吗?

最佳答案

I see no errors :

/**
 $ sudo apt-get install libboost-python-dev
 $ ./do
*/
#include <boost/python.hpp>

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};


namespace py = boost::python;

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

int main(int argc, char* argv[]) {
  Py_Initialize();
  inithello();

  PySys_SetArgv(argc, argv); // use later in Python
  if (PyRun_SimpleString("import hello\n"
                         "planet = hello.World()\n"
                         "planet.set('howdy')\n"
                         "print(planet.greet())\n"

                         // start interactive Python shell if `-i`
                         "import sys\n"
                         "if '-i' in sys.argv:\n"
                         "  import code\n"
                         "  code.interact()\n" //NOTE: namespace may be provided
                         ) < 0)
    return 1; // an exception is raised in Python

  Py_Finalize();
}

另请参阅How can I start the python console within a program (for easy debugging)? .

编译:

$ c++ main.cpp -o main `python-config --cflags` \
     `python-config --ldflags` -lboost_python

运行:

$ ./main -i

关于python - Boost::Python 和带变量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12083562/

相关文章:

Python:声明为整数和字符

c++ - 来自 Boost : cannot find the libraries to link with 的链接器错误

c++ - 在多个线程上发布任务时 boost::asio::thread_pool 线程安全吗?

c++ - Boost::log:基于级别的不同格式(HTML 格式)

c++ - boost.asio.detail.read_op 是什么意思?

c++ - 使用 boost 库在队列和堆栈数据结构上保存和加载数据时出错

python 子类

python - 如何让用户使用 "sqrt()"+ 输入求输入的平方根?

python - 测试 protobuf 消息中是否存在重复字段

python - 使用 pandas.to_excel 时格式化 timedelta64