21 void PythonLoader::initialize(
const std::string &python_home)
23 static internal::PythonRunning _running(python_home);
27 PyObject * PythonLoader::load_module_from_file(
const std::string& fname) {
31 std::ifstream file_stream( fname.c_str() );
33 INPUT_CHECK(! file_stream.fail(),
"Can not open input file '%s'.\n", fname.c_str() );
34 file_stream.exceptions ( ifstream::failbit | ifstream::badbit );
36 std::stringstream buffer;
37 buffer << file_stream.rdbuf();
40 unsigned int pos = fname.rfind(
"/");
41 if (pos != string::npos)
42 module_name = fname.substr(pos+1);
49 return load_module_from_string(module_name, buffer.str() );
54 PyObject * PythonLoader::load_module_from_string(
const std::string& module_name,
const std::string& source_string) {
58 char * tmp_name =
new char[ module_name.size() + 2 ];
59 strcpy( tmp_name, module_name.c_str() );
60 PyObject * compiled_string = Py_CompileString( source_string.c_str(),
"flow123d_python_loader", Py_file_input );
61 if (! compiled_string) {
63 std::cerr <<
"Error: Can not compile python string:\n'" << source_string << std::endl;
65 PyObject * result = PyImport_ExecCodeModule(tmp_name, compiled_string);
69 std::cerr <<
"Error: Can not load python module '" << module_name <<
"' from string:" << std::endl;
70 std::cerr << source_string << std::endl;
79 wstring to_py_string(
const string &str) {
80 wchar_t wbuff[ str.size() ];
81 size_t wstr_size = mbstowcs( wbuff, str.c_str(), str.size() );
82 return wstring( wbuff, wstr_size );
85 string from_py_string(
const wstring &wstr) {
86 char buff[ wstr.size() ];
87 size_t str_size = wcstombs( buff, wstr.c_str(), wstr.size() );
88 return string( buff, str_size );
93 #if PYTHONLIBS_VERSION_MAJOR<3
94 #define to_py_string string
95 #define from_py_string string
96 #define PY_STRING string
98 #define PY_STRING wstring
101 #define STR_EXPAND(tok) #tok
102 #define STR(tok) string(STR_EXPAND(tok))
106 PythonRunning::PythonRunning(
const std::string& program_name)
109 static PY_STRING _python_program_name = to_py_string(program_name);
110 Py_SetProgramName( &(_python_program_name[0]) );
111 PY_STRING full_program_name = Py_GetProgramFullPath();
112 cout <<
"full program name: " << from_py_string(full_program_name) << std::endl;
114 size_t pos = full_program_name.rfind( to_py_string(
"flow123d") );
116 ASSERT(pos != PY_STRING::npos,
"non flow123d binary");
117 PY_STRING full_flow_prefix=full_program_name.substr(0,pos-
string(
"/bin/").size() );
118 cout <<
"full flow prefix: " << from_py_string(full_flow_prefix) << std::endl;
119 PY_STRING default_py_prefix(to_py_string(STR(PYTHON_PREFIX)));
120 cout <<
"default py prefix: " << from_py_string(default_py_prefix) << std::endl;
122 static PY_STRING our_py_home(full_flow_prefix +
":" +default_py_prefix);
123 Py_SetPythonHome( &(our_py_home[0]) );
142 cout <<
"Python path: " << from_py_string( Py_GetPath() ) << std::endl;
143 cout <<
"Python home: " << from_py_string( Py_GetPythonHome() ) << std::endl;
144 cout <<
"Python prefix: " << from_py_string( Py_GetPrefix() ) << std::endl;
145 cout <<
"Python exec prefix: " << from_py_string( Py_GetExecPrefix() ) << std::endl;
197 PythonRunning::~PythonRunning() {
203 #endif // HAVE_PYTHON
Global macros to enhance readability and debugging, general constants.
#define INPUT_CHECK(i,...)
Debugging macros.