Boost-Python:从文件执行脚本时崩溃
当我的控制台应用程序尝试执行 boost::python::exec_file() 时,它会挂起一秒钟然后崩溃。
它可以毫无问题地执行 boost::python::exec 。
我尝试不使用 then boost 绑定并直接从 python api 执行,但同样的事情发生了,它挂起一会儿然后崩溃:
FILE *file = fopen("test.py", "r+");
PyRun_SimpleFile(file, "test");
所以我猜这是 python api 的问题,因为这是 boost.python 链接到的?
我正在使用 Boost.Python 的共享版本,并链接到 Python 3.2.2 32 位库 (libpython32.a) 的预编译版本,并且我正在 Windows 7 上通过 QtCreator 使用 MinGW 4.6 进行编译,
这是我的 main.cpp:
#include <Python.h>
#include <boost/python.hpp>
#include <iostream>
namespace python = boost::python;
int main( int argc, char ** argv ) {
try {
// File path
std::string filepath;
if(argv[1] != NULL) {
filepath = argv[1];
}
// Initialize the interpreter
Py_Initialize();
std::cout << "Using Python " << Py_GetVersion() << std::endl;
// Create the python environment
python::object main = python::import("__main__");
python::object global(main.attr("__dict__"));
//python::exec("print('hello world')", global, global);
python::object result = python::exec_file("test.py", global, global);
} catch (python::error_already_set const &) {
python::handle_exception();
}
}
这是我试图执行的脚本文件(test.py):
print("hello world")
我的 .pro 文件看起来像这样:
#Application config
TEMPLATE = app
CONFIG += console
CONFIG -= qt
#Path and environment info
PYTHONTEST_ROOT = $$PWD
PYTHONTEST_BIN = $$PYTHONTEST_ROOT /bin
PYTHONTEST_LIB = $$PYTHONTEST_ROOT /lib
PYTHONTEST_INCLUDE = $$PYTHONTEST_ROOT /include
PYTHONTEST_TMP = $$PYTHONTEST_ROOT /tmp
MOC_DIR = $$PYTHONTEST_TMP/mocs
OBJECTS_DIR = $$PYTHONTEST_TMP/objs
#Includes and dependencies
INCLUDEPATH += C:/Python32/include
INCLUDEPATH += C:/Boost_1_48_0
#Build info
Debug {
win32 {
#Libs
LIBS += -LC:/Python32/libs -lpython32
LIBS += -LC:/Boost_1_48_0/stage/lib -lboost_python-mgw46-mt-d-1_48.dll
#Build config
DESTDIR = $$PYTHONTEST_BIN/debug/win32/
TARGET = pythontest_d
}
unix {
}
macx {
}
}
Release {
win32 {
#Libs
LIBS += -LC:/Python32/libs -lpython32
LIBS += -LC:/Boost_1_48_0/stage/lib -lboost_python-mgw46-mt-1_48.dll
#Build config
DESTDIR = $$PYTHONTEST_BIN/release/win32/
TARGET = pythontest
}
unix {
}
macx {
}
}
#Source code
SOURCES += main.cpp
HEADERS +=
所有这些似乎工作正常,没有编译或链接错误,我确实得到了一些但警告:
In file included from c:\Boost_1_48_0/boost/python/object/make_instance.hpp:9,
from c:\Boost_1_48_0/boost/python/object/make_ptr_instance.hpp:8,
from c:\Boost_1_48_0/boost/python/to_python_indirect.hpp:11,
from c:\Boost_1_48_0/boost/python/converter/arg_to_python.hpp:10,
from c:\Boost_1_48_0/boost/python/call.hpp:15,
from c:\Boost_1_48_0/boost/python/object_core.hpp:14,
from c:\Boost_1_48_0/boost/python/args.hpp:25,
from c:\Boost_1_48_0/boost/python.hpp:11,
from ..\PythonTest\main.cpp:2:
c:\Boost_1_48_0/boost/python/object/instance.hpp:14: warning: type attributes ignored after type is already defined
编辑 现在这并不能解释或解决崩溃,但我可以使用我自己的读取函数并仅传递 boost::python::exec 输出来解决它。那么我不必使用 exec_file 或 PyRun_SimpleFile
#include <fstream>
std::string read_file(std::string const &filepath)
{
std::string output;
std::ifstream file;
file.open(filepath.c_str());
if(file.is_open()){
while(!file.eof()){
std::string line;
std::getline(file,line);
output += line.append("\n");
}
}
file.close();
return output;
}
When my console application tries to execute boost::python::exec_file() it hangs for a second and then crashes.
It can execute the boost::python::exec without problems.
I tried not using then boost bindings and execute from the python api directly but the same thing happens, it hangs for a bit and then crashes:
FILE *file = fopen("test.py", "r+");
PyRun_SimpleFile(file, "test");
So I guess it is a problem with the python api since this is what boost.python links to?
I am using a shared build of Boost.Python, and linking to a precompiled version of Python 3.2.2 32bit library (libpython32.a) and I am compiling with MinGW 4.6 through QtCreator on Windows 7
This is my main.cpp:
#include <Python.h>
#include <boost/python.hpp>
#include <iostream>
namespace python = boost::python;
int main( int argc, char ** argv ) {
try {
// File path
std::string filepath;
if(argv[1] != NULL) {
filepath = argv[1];
}
// Initialize the interpreter
Py_Initialize();
std::cout << "Using Python " << Py_GetVersion() << std::endl;
// Create the python environment
python::object main = python::import("__main__");
python::object global(main.attr("__dict__"));
//python::exec("print('hello world')", global, global);
python::object result = python::exec_file("test.py", global, global);
} catch (python::error_already_set const &) {
python::handle_exception();
}
}
This is the script file I am trying to execute (test.py):
print("hello world")
My .pro file looks like this:
#Application config
TEMPLATE = app
CONFIG += console
CONFIG -= qt
#Path and environment info
PYTHONTEST_ROOT = $PWD
PYTHONTEST_BIN = $PYTHONTEST_ROOT /bin
PYTHONTEST_LIB = $PYTHONTEST_ROOT /lib
PYTHONTEST_INCLUDE = $PYTHONTEST_ROOT /include
PYTHONTEST_TMP = $PYTHONTEST_ROOT /tmp
MOC_DIR = $PYTHONTEST_TMP/mocs
OBJECTS_DIR = $PYTHONTEST_TMP/objs
#Includes and dependencies
INCLUDEPATH += C:/Python32/include
INCLUDEPATH += C:/Boost_1_48_0
#Build info
Debug {
win32 {
#Libs
LIBS += -LC:/Python32/libs -lpython32
LIBS += -LC:/Boost_1_48_0/stage/lib -lboost_python-mgw46-mt-d-1_48.dll
#Build config
DESTDIR = $PYTHONTEST_BIN/debug/win32/
TARGET = pythontest_d
}
unix {
}
macx {
}
}
Release {
win32 {
#Libs
LIBS += -LC:/Python32/libs -lpython32
LIBS += -LC:/Boost_1_48_0/stage/lib -lboost_python-mgw46-mt-1_48.dll
#Build config
DESTDIR = $PYTHONTEST_BIN/release/win32/
TARGET = pythontest
}
unix {
}
macx {
}
}
#Source code
SOURCES += main.cpp
HEADERS +=
All of this seems to work fine, no compile or link errors, I do get some warnings though:
In file included from c:\Boost_1_48_0/boost/python/object/make_instance.hpp:9,
from c:\Boost_1_48_0/boost/python/object/make_ptr_instance.hpp:8,
from c:\Boost_1_48_0/boost/python/to_python_indirect.hpp:11,
from c:\Boost_1_48_0/boost/python/converter/arg_to_python.hpp:10,
from c:\Boost_1_48_0/boost/python/call.hpp:15,
from c:\Boost_1_48_0/boost/python/object_core.hpp:14,
from c:\Boost_1_48_0/boost/python/args.hpp:25,
from c:\Boost_1_48_0/boost/python.hpp:11,
from ..\PythonTest\main.cpp:2:
c:\Boost_1_48_0/boost/python/object/instance.hpp:14: warning: type attributes ignored after type is already defined
EDIT
Now this does not explain or solve the crash, but I can get around it, using my own read function and just passing boost::python::exec the output. then I do not have to use exec_file or PyRun_SimpleFile
#include <fstream>
std::string read_file(std::string const &filepath)
{
std::string output;
std::ifstream file;
file.open(filepath.c_str());
if(file.is_open()){
while(!file.eof()){
std::string line;
std::getline(file,line);
output += line.append("\n");
}
}
file.close();
return output;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论