Boost-Python: crash while executing script from file

When my console application tries to execute boost :: python :: exec_file (), it freezes for a second and then crashes.

It can execute boost :: python :: exec without any problems.

I did not try to use boost bindings and executed directly from the python api, but the same thing happens, it hangs a bit and then crashes:

FILE *file = fopen("test.py", "r+"); PyRun_SimpleFile(file, "test"); 

So, I think this is a problem with python api, since this is what boost.python links to?

I use the general assembly of Boost.Python and a link to a precompiled version of the 32-bit Python 3.2.2 library (libpython32.a), and I compile with MinGW 4.6 via 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'm trying to execute (test.py):

 print("hello world") 

My .pro file is as follows:

 #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 this seems to work fine, doesn't compile or bind errors, however I get some warnings:

 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 does not resolve the failure, but I can get around it using my own read function and just passing boost :: python :: exec the output. then I do not need 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; } 
+4
source share

Source: https://habr.com/ru/post/1396386/


All Articles