1 // Copyright (C) 2019-2024 EDF
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 #ifndef PY2CPP_FUNCTIONCALLS_HXX
20 #define PY2CPP_FUNCTIONCALLS_HXX
22 #include "py2cppExports.hxx"
25 #include "TypeConversions.hxx"
30 class PY2CPP_EXPORT PyFunction : public PyPtr
33 /*! Load a callable object from a python module.*/
34 bool load(const std::string& module, const std::string& function);
35 /*! Load a callable object member of a python object.*/
36 bool load(const PyPtr& obj, const std::string& function);
37 bool load(PyObject* obj, const std::string& function);
39 // The following versions of the functions throw ExecutionException in case of
41 void loadExp(const std::string& module, const std::string& function);
42 void loadExp(const PyPtr& obj, const std::string& function);
43 void loadExp(PyObject* obj, const std::string& function);
46 * The evaluation returns nullptr if the python function throws an exception.
47 * See PyObject_CallObject documentation.
48 * You can use getLastPyError in order to get the last python error.
50 template <class ...Ts>
51 PyPtr safeEval(const Ts&... args)
54 PyObject * myFunc = get();
55 if(myFunc && PyCallable_Check(myFunc))
57 std::tuple<const Ts&...> tupleArgs(args...);
58 PyPtr pyArgs(toPy(tupleArgs));
59 result.reset(PyObject_CallObject(myFunc, pyArgs.get()));
65 * The evaluation throws ExecutionException if the python function throws an
68 template <class ...Ts>
69 PyPtr operator()(const Ts&... args)
71 PyPtr result = safeEval(args...);
74 std::string errorMessage = "Failed to execute python function.\n";
75 throw ExecutionException(errorMessage+getLastPyError());
82 #endif //PY2CPP_FUNCTIONCALLS_HXX