Flow123d  last_with_con_2.0.0-4-g42e6930
python_loader.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file python_loader.hh
15  * @brief
16  */
17 
18 #ifndef PYTHON_UTILS_HH_
19 #define PYTHON_UTILS_HH_
20 
21 #include "global_defs.h"
22 
23 #ifdef FLOW123D_HAVE_PYTHON
24 
25 #include "Python.h"
26 #include <string>
27 
28 
29 namespace internal {
30 /**
31  * Class to implement initialization and finalization of the python module
32  */
33 class PythonRunning {
34 public:
35  PythonRunning(const std::string &python_home);
36  ~PythonRunning();
37 };
38 } // close namespace internal
39 
40 
41 
42 /**
43  * Class with static only members, should be used to load and compile Python sources either from file or from string.
44  * Implement correct initialization and finalization.
45  * TODO:
46  * - check validity of results and throw exceptions
47  * - better loading of modules from files
48  * - explain and resolve RuntimeWarning:
49  * "flow123d_python_loader:1: RuntimeWarning: Parent module 'field_python_script' not found while handling absolute import"
50  * that appears during field_python_test.cpp
51  *
52  */
53 class PythonLoader {
54 public:
55 
56  /**
57  * Definition of exception thrown by python compiler or interpreter.
58  */
59  TYPEDEF_ERR_INFO(EI_PythonMessage, std::string);
60  DECLARE_EXCEPTION(ExcPythonError,
61  << "Python Error: " << EI_PythonMessage::val << "\n");
62 
63 
64  /**
65  * Calls python initialization and guarantee that appropriate finalization will be called.
66  * Do nothing if initialization was done.
67  *
68  * The method with no parameters is called at the beginning of every function of this class, so an explicit call
69  * to it has only sense if one would like to provide alternative python home directories.
70  * The string has form <prefix>[:<exec_prefix>] where <prefix> is prefix for platform independent libraries
71  * (namely sources of python standard libraries) and <exec_prefix> is prefix for platform dependent libraries namely
72  * for the python executable.
73  */
74 
75  static void initialize(const std::string &python_home="");
76 
77  /**
78  * This function loads a module from the given file.
79  * Resulting module has to be deallocated by Py_DECREF() macro.
80  */
81  static PyObject * load_module_from_file(const std::string& fname);
82  /**
83  * This function compile code in the given string and creates a module with name @p module_name.
84  * Resulting module has to be deallocated by Py_DECREF() macro.
85  */
86  static PyObject * load_module_from_string(const std::string& module_name, const std::string& source_string);
87  /**
88  * Method which loads module by given module_name
89  * module_name can (and probably will) contain packages path (will contain dots '.' which detonates package)
90  *
91  * Example:
92  * PyObject * python_module = PythonLoader::load_module_by_name ("profiler.profiler_formatter_module")
93  *
94  * will import module 'profiler_formatter_module' from package 'profiler'
95  */
96  static PyObject * load_module_by_name(const std::string& module_name);
97  /**
98  * Tests whether the error indicator is set, if yes formats and throws exception.
99  */
100  static void check_error();
101  /**
102  * Check if python function is callable, if not throws exception.
103  */
104  static PyObject * get_callable(PyObject *module, const std::string &func_name);
105 };
106 
107 
108 
109 #endif // FLOW123D_HAVE_PYTHON
110 
111 #endif /* PYTHON_UTILS_HH_ */
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:150
Global macros to enhance readability and debugging, general constants.
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:186