Salome HOME
Add a safe evaluation of a python function which does not throw any exception.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Thu, 21 Feb 2019 12:37:26 +0000 (13:37 +0100)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Thu, 21 Feb 2019 12:37:26 +0000 (13:37 +0100)
example/main.cxx
src/PyFunction.hxx

index e07ef3b913b7632f7e57f3554e3c77aa85f620c0..5d2514954435f13137600afebef401aa36a9478b 100644 (file)
@@ -25,6 +25,7 @@
 int main()
 {
   Py_Initialize();
+  {
   std::string s;
   double d;
   py2cpp::PyFunction fn;
@@ -48,6 +49,7 @@ int main()
       std::cerr << err.what();
     }
   }
+  }
   Py_Finalize();
   return 0;
 }
index 65cb05265a0d1e24cbdf99cb87efc062091fec1f..6e208478ba77ba1d91b7c0ca9674f00ed5e5c579 100644 (file)
@@ -46,7 +46,7 @@ public:
    * You can use getLastPyError in order to get the last python error.
    */
   template <class ...Ts>
-  PyPtr operator()(const Ts&... args)
+  PyPtr safeEval(const Ts&... args)
   {
     PyPtr result;
     PyObject * myFunc = get();
@@ -56,6 +56,17 @@ public:
       PyPtr pyArgs(toPy(tupleArgs));
       result.reset(PyObject_CallObject(myFunc, pyArgs.get()));
     }
+    return result;
+  }
+
+  /*!
+   * The evaluation throws ExecutionException if the python function throws an
+   * exception.
+   */
+  template <class ...Ts>
+  PyPtr operator()(const Ts&... args)
+  {
+    PyPtr result = safeEval(args...);
     if(!result)
     {
       std::string errorMessage = "Failed to execute python function.\n";