4

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;
}
mXed
  • 71
  • 8
  • Check that boost python is compiled against the same python that you're linking against using http://dependencywalker.com/ – James Feb 26 '12 at 11:36
  • Just checked your example on a machine running Linux, boost-1.52 and python-3.3. It works flawlessly. I also verified that the implementation of `boost::python::exec_file` did not change between 1.48 (the version you are using) and 1.52. So, I don't think `boost.python` is the culprit either. My best guess is that your environment is causing the problem. Like James suggested, I'd look into your dependencies first and make sure all is tight. – André Anjos Feb 08 '13 at 07:56

0 Answers0