コンソール アプリケーションが boost::python::exec_file() を実行しようとすると、1 秒間ハングしてからクラッシュします。
boost::python::exec を問題なく実行できます。
バインディングをブーストしてPython APIから直接実行しないようにしましたが、同じことが起こり、少しハングしてからクラッシュします。
FILE *file = fopen("test.py", "r+");
PyRun_SimpleFile(file, "test");
これはboost.pythonがリンクしているものなので、python apiの問題だと思いますか?
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;
}