35#include <boost/core/ignore_unused.hpp>
49 PyObject *type_ptr =
nullptr, *value_ptr =
nullptr,
50 *traceback_ptr =
nullptr;
52 PyErr_Fetch(&type_ptr, &value_ptr, &traceback_ptr);
55 std::string ret(
"Unfetchable Python error");
58 if (type_ptr !=
nullptr)
60 py::handle<> h_type(type_ptr);
61 py::str type_pstr(h_type);
63 py::extract<std::string> e_type_pstr(type_pstr);
66 if (e_type_pstr.check())
72 ret =
"Unknown exception type";
78 if (value_ptr !=
nullptr)
80 py::handle<> h_val(value_ptr);
82 py::extract<std::string> returned(a);
85 ret +=
": " + returned();
89 ret += std::string(
": Unparseable Python error: ");
94 if (traceback_ptr !=
nullptr)
96 py::handle<> h_tb(traceback_ptr);
99 py::object tb(py::import(
"traceback"));
100 py::object fmt_tb(tb.attr(
"format_tb"));
103 py::object tb_list(fmt_tb(h_tb));
106 py::object tb_str(py::str(
"\n").join(tb_list));
109 py::extract<std::string> returned(tb_str);
110 if (returned.check())
112 ret +=
": " + returned();
116 ret += std::string(
": Unparseable Python traceback");
128 for (
int i = 0; i < pFields.size(); ++i)
130 expLists.append(py::object(pFields[i]));
140 const std::shared_ptr<EquationSystem> &pEquation,
142 :
Filter(pSession, pEquation)
144 auto it = pParams.find(
"PythonFile");
145 ASSERTL0(it != pParams.end(),
"Empty parameter 'PythonFile'.");
146 std::string pythonFile = it->second;
149 std::string pythonCode;
152 m_global = py::import(
"__main__").attr(
"__dict__");
156 std::ifstream t(pythonFile);
157 std::string str((std::istreambuf_iterator<char>(t)),
158 std::istreambuf_iterator<char>());
163 ASSERTL0(
false,
"Error reading Python file: " + pythonFile);
167 if (pSession->DefinesCmdLineArgument(
"verbose"))
169 std::cout <<
"-----------------" << std::endl;
170 std::cout <<
"BEGIN PYTHON CODE" << std::endl;
171 std::cout <<
"-----------------" << std::endl;
172 std::cout << pythonCode << std::endl;
173 std::cout <<
"-----------------" << std::endl;
174 std::cout <<
"END PYTHON CODE" << std::endl;
175 std::cout <<
"-----------------" << std::endl;
182 auto nekpy = py::import(
"NekPy");
183 auto multireg = py::import(
"NekPy.MultiRegions");
189 catch (py::error_already_set
const &)
198 it = pParams.find(
"FilterName");
199 if (it != pParams.end())
201 std::string filterName = it->second;
205 "Unable to locate filter named '" + filterName +
"'");
212 catch (py::error_already_set
const &)
240 catch (py::error_already_set
const &)
262 catch (py::error_already_set
const &)
284 catch (py::error_already_set
const &)
#define ASSERTL0(condition, msg)
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
std::map< std::string, std::string > ParamMap
void v_Finalise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
std::shared_ptr< Filter > m_pyFilter
SOLVER_UTILS_EXPORT ~FilterPython() override
bool v_IsTimeDependent() override
void v_Update(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
SOLVER_UTILS_EXPORT FilterPython(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const ParamMap &pParams)
static FilterSharedPtr create(const LibUtilities::SessionReaderSharedPtr &pSession, const std::shared_ptr< EquationSystem > &pEquation, const std::map< std::string, std::string > &pParams)
Creates an instance of this class.
static std::string className
Name of the class.
void v_Initialise(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields, const NekDouble &time) override
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::string parse_python_exception()
Temporarily stolen from boost examples.
py::list ArrayOneDToPyList(const Array< OneD, const MultiRegions::ExpListSharedPtr > &pFields)
FilterFactory & GetFilterFactory()