Boost-Python:从文件执行脚本时崩溃

时间:2012-02-14 11:41:06

标签: qt-creator boost-python python-embedding

当我的控制台应用程序尝试执行boost :: python :: exec_file()时,它会挂起一秒然后崩溃。

它可以毫无问题地执行boost :: python :: exec。

我尝试不使用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;
}

0 个答案:

没有答案
相关问题