Python初始化段错误
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要的是:
Py_Initialize()
是必要的,try { ... } catch () { ... }
块会产生 Python 错误类似于您从解释器收到的消息,并且bp::import
仅适用于模块,不适用于导入模块的属性:-)I think what you want is the following:
Py_Initialize()
is necessary, thetry { ... } catch () { ... }
-block produces a Python error message like the one you would get from the interpreter andbp::import
only works for modules, not for attributes of imported modules :-)