1 // Copyright (C) 2019 EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "PyFunction.hxx"
22 bool PyFunction::load(const std::string& module, const std::string& function)
26 PyPtr moduleName(PyUnicode_FromString(module.c_str()));
27 PyPtr moduleObj(PyImport_Import(moduleName.get()));
30 PyObject * newFunction = PyObject_GetAttrString(moduleObj.get(),
32 if(newFunction && PyCallable_Check(newFunction))
41 bool PyFunction::load(const PyPtr& obj, const std::string& function)
43 return load(obj.get(), function);
46 bool PyFunction::load(PyObject* obj, const std::string& function)
50 if(obj && PyObject_HasAttrString(obj, function.c_str()))
52 PyObject * fn = PyObject_GetAttrString(obj, function.c_str());
53 if(fn && PyCallable_Check(fn))
64 void PyFunction::loadExp(const std::string& module, const std::string& function)
66 if(!load(module, function))
68 std::string errorMessage = "Failed to load function '";
69 errorMessage += function;
70 errorMessage += "' from module '";
71 errorMessage += module;
72 errorMessage += "'\n";
73 throw ExecutionException(errorMessage+getLastPyError());
77 void PyFunction::loadExp(const PyPtr& obj, const std::string& function)
79 if(!load(obj, function))
81 std::string errorMessage = "Failed to load function '";
82 errorMessage += function;
83 errorMessage += "' from a python object.\n";
84 throw ExecutionException(errorMessage+getLastPyError());
88 void PyFunction::loadExp(PyObject* obj, const std::string& function)
90 if(!load(obj, function))
92 std::string errorMessage = "Failed to load function '";
93 errorMessage += function;
94 errorMessage += "' from a python object.\n";
95 throw ExecutionException(errorMessage+getLastPyError());