Python初始化段错误

发布于 2024-10-22 08:44:00 字数 2017 浏览 0 评论 0原文

我正在使用 boost.python 构建应用程序。我有一些名为 ScriptsManager 的单例类,它具有 initPython 函数,它的作用是:

mMainModule = bp::import("__main__");
mMainNamespace = bp::import("__dict__");

bp::object ignored = bp::exec("hello = file('hello.txt', 'w')\n"
                  "hello.write('Hello world!')\n"
                  "hello.close()", mMainNamespace);

mMainModule、mMainNamespace 都是 boost::python::对象。

因此,当我开始申请时,我得到:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff5d5efd9 in PyEval_GetGlobals () from /usr/lib/libpython2.7.so.1.0
#2  0x00007ffff5d79113 in PyImport_Import () from /usr/lib/libpython2.7.so.1.0
#3  0x00007ffff5d7935c in PyImport_ImportModule () from /usr/lib/libpython2.7.so.1.0
#4  0x00007ffff5a6d8bd in boost::python::import(boost::python::str) () from /usr/lib/libboost_python.so.1.46.0
#5  0x0000000000510b1b in ScriptsManager::initPython (this=0x7b6850) at /home/ockonal/Workspace/Themisto/src/Core/ScriptsManager.cpp:24
#6  0x0000000000547650 in Application::main (args=...) at /home/ockonal/Workspace/Themisto/src/main.cpp:60
#7  0x00007ffff4ebbf86 in main () from /usr/lib/libclan22App-2.2.so.1
#8  0x00007ffff24c4dcd in __libc_start_main () from /lib/libc.so.6
#9  0x00000000004c9769 in _start ()

这里可能出了什么问题?


UPD1

当我在 bp::import 之前调用 Py_Initialize() 时,我得到:

抛出后终止调用 的实例 'boost::python::error_already_set'


UPD2

似乎问题出在代码中:

mMainNamespace = bp::import("__dict__");

结果代码是:

Py_Initialize();
mMainModule = bp::import("__main__");
mMainNamespace = mMainModule.attr("__dict__");

我不确定它是否正确。


UPD3

是的,第二次更新有效。太奇怪了, mMainNamespace = bp::import("__dict__") 是在官方 boost 文档中编写的。

I'm building app with boost.python. I have some singleton class named ScriptsManager, it has function initPython which does:

mMainModule = bp::import("__main__");
mMainNamespace = bp::import("__dict__");

bp::object ignored = bp::exec("hello = file('hello.txt', 'w')\n"
                  "hello.write('Hello world!')\n"
                  "hello.close()", mMainNamespace);

both mMainModule, mMainNamespace are boost::python::object.

So, when I start application, I get:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x00007ffff5d5efd9 in PyEval_GetGlobals () from /usr/lib/libpython2.7.so.1.0
#2  0x00007ffff5d79113 in PyImport_Import () from /usr/lib/libpython2.7.so.1.0
#3  0x00007ffff5d7935c in PyImport_ImportModule () from /usr/lib/libpython2.7.so.1.0
#4  0x00007ffff5a6d8bd in boost::python::import(boost::python::str) () from /usr/lib/libboost_python.so.1.46.0
#5  0x0000000000510b1b in ScriptsManager::initPython (this=0x7b6850) at /home/ockonal/Workspace/Themisto/src/Core/ScriptsManager.cpp:24
#6  0x0000000000547650 in Application::main (args=...) at /home/ockonal/Workspace/Themisto/src/main.cpp:60
#7  0x00007ffff4ebbf86 in main () from /usr/lib/libclan22App-2.2.so.1
#8  0x00007ffff24c4dcd in __libc_start_main () from /lib/libc.so.6
#9  0x00000000004c9769 in _start ()

What could be wrong here?


UPD1

When I call Py_Initialize() before bp::import I get:

terminate called after throwing an
instance of
'boost::python::error_already_set'


UPD2

Seems that problem was in code:

mMainNamespace = bp::import("__dict__");

The result code is:

Py_Initialize();
mMainModule = bp::import("__main__");
mMainNamespace = mMainModule.attr("__dict__");

I'm not sure it's right.


UPD3

Yep, 2-nd update works. So strange, mMainNamespace = bp::import("__dict__") is written in official boost docs.

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

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

发布评论

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

评论(1

猫七 2024-10-29 08:44:00

我认为你想要的是:

int main (int argc, char** argv)
{
    try
    {
        // If you're going to use threads: PyEval_InitThreads();
        Py_Initialize();
        PySys_SetArgv(argc, argv);

        bp::object mMainModule = bp::import('__main__');
        bp::object mMainNamespace = mMainModule.attr('__dict__');

        bp::object ignored = bp::exec("hello = file('hello.txt', 'w')\n"
              "hello.write('Hello world!')\n"
              "hello.close()", mMainNamespace);
   }
   catch (bp::error_already_set const&)
   {
        PyErr_Print();
   }
}

Py_Initialize() 是必要的, try { ... } catch () { ... } 块会产生 Python 错误类似于您从解释器收到的消息,并且 bp::import 仅适用于模块,不适用于导入模块的属性:-)

I think what you want is the following:

int main (int argc, char** argv)
{
    try
    {
        // If you're going to use threads: PyEval_InitThreads();
        Py_Initialize();
        PySys_SetArgv(argc, argv);

        bp::object mMainModule = bp::import('__main__');
        bp::object mMainNamespace = mMainModule.attr('__dict__');

        bp::object ignored = bp::exec("hello = file('hello.txt', 'w')\n"
              "hello.write('Hello world!')\n"
              "hello.close()", mMainNamespace);
   }
   catch (bp::error_already_set const&)
   {
        PyErr_Print();
   }
}

Py_Initialize() is necessary, the try { ... } catch () { ... }-block produces a Python error message like the one you would get from the interpreter and bp::import only works for modules, not for attributes of imported modules :-)

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