Salome HOME
updated copyright message
[tools/py2cpp.git] / src / PyFunction.hxx
1 // Copyright (C) 2019-2023 EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef PY2CPP_FUNCTIONCALLS_HXX
20 #define PY2CPP_FUNCTIONCALLS_HXX
21
22 #include "py2cppExports.hxx"
23 #include <Python.h>
24 #include <tuple>
25 #include "TypeConversions.hxx"
26 #include "Errors.hxx"
27
28 namespace py2cpp
29 {
30 class PY2CPP_EXPORT PyFunction : public PyPtr
31 {
32 public:
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);
38
39   // The following versions of the functions throw ExecutionException in case of
40   // failure.
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);
44
45   /*!
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.
49    */
50   template <class ...Ts>
51   PyPtr safeEval(const Ts&... args)
52   {
53     PyPtr result;
54     PyObject * myFunc = get();
55     if(myFunc && PyCallable_Check(myFunc))
56     {
57       std::tuple<const Ts&...> tupleArgs(args...);
58       PyPtr pyArgs(toPy(tupleArgs));
59       result.reset(PyObject_CallObject(myFunc, pyArgs.get()));
60     }
61     return result;
62   }
63
64   /*!
65    * The evaluation throws ExecutionException if the python function throws an
66    * exception.
67    */
68   template <class ...Ts>
69   PyPtr operator()(const Ts&... args)
70   {
71     PyPtr result = safeEval(args...);
72     if(!result)
73     {
74       std::string errorMessage = "Failed to execute python function.\n";
75       throw ExecutionException(errorMessage+getLastPyError());
76     }
77     return result;
78   }
79 };
80 }
81
82 #endif //PY2CPP_FUNCTIONCALLS_HXX