19 void PythonLoader::initialize(
const std::string &python_home)
21 static internal::PythonRunning _running(python_home);
25 PyObject * PythonLoader::load_module_from_file(
const std::string& fname) {
29 std::ifstream file_stream( fname.c_str() );
31 INPUT_CHECK(! file_stream.fail(),
"Can not open input file '%s'.\n", fname.c_str() );
32 file_stream.exceptions ( ifstream::failbit | ifstream::badbit );
34 std::stringstream buffer;
35 buffer << file_stream.rdbuf();
38 unsigned int pos = fname.rfind(
"/");
39 if (pos != string::npos)
40 module_name = fname.substr(pos+1);
47 return load_module_from_string(module_name, buffer.str() );
52 PyObject * PythonLoader::load_module_from_string(
const std::string& module_name,
const std::string& source_string) {
56 char * tmp_name =
new char[ module_name.size() + 2 ];
57 strcpy( tmp_name, module_name.c_str() );
58 PyObject * result = PyImport_ExecCodeModule(tmp_name,
59 Py_CompileString( source_string.c_str(),
"flow123d_python_loader", Py_file_input ) );
63 std::cerr <<
"Error: Can not load python module '" << module_name <<
"' from string:" << std::endl;
64 std::cerr << source_string << std::endl;
75 PythonRunning::PythonRunning(
const std::string& python_home)
81 if (python_home !=
"") {
82 static string _python_home_storage = python_home;
85 Py_SetProgramName( &(_python_home_storage[0]) );
86 std::cout << Py_GetPath() << std::endl;
93 PythonRunning::~PythonRunning() {