Salome HOME
updated copyright message
[tools/py2cpp.git] / src / PyFunction.cxx
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 #include "PyFunction.hxx"
20 namespace py2cpp
21 {
22 bool PyFunction::load(const std::string& module, const std::string& function)
23 {
24   bool result = false;
25   reset(nullptr);
26   PyPtr moduleName(PyUnicode_FromString(module.c_str()));
27   PyPtr moduleObj(PyImport_Import(moduleName.get()));
28   if(moduleObj)
29   {
30     PyObject * newFunction = PyObject_GetAttrString(moduleObj.get(),
31                                                     function.c_str());
32     if(newFunction && PyCallable_Check(newFunction))
33     {
34       reset(newFunction);
35       result = true;
36     }
37   }
38   return result;
39 }
40
41 bool PyFunction::load(const PyPtr& obj, const std::string& function)
42 {
43   return load(obj.get(), function);
44 }
45
46 bool PyFunction::load(PyObject* obj, const std::string& function)
47 {
48   bool result = false;
49   reset(nullptr);
50   if(obj && PyObject_HasAttrString(obj, function.c_str()))
51   {
52     PyObject * fn = PyObject_GetAttrString(obj, function.c_str());
53     if(fn && PyCallable_Check(fn))
54     {
55       reset(fn);
56       result = true;
57     }
58     else
59       Py_XDECREF(fn);
60   }
61   return result;
62 }
63
64 void PyFunction::loadExp(const std::string& module, const std::string& function)
65 {
66   if(!load(module, function))
67   {
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());
74   }
75 }
76
77 void PyFunction::loadExp(const PyPtr& obj, const std::string& function)
78 {
79   if(!load(obj, function))
80   {
81     std::string errorMessage = "Failed to load function '";
82     errorMessage += function;
83     errorMessage += "' from a python object.\n";
84     throw ExecutionException(errorMessage+getLastPyError());
85   }
86 }
87
88 void PyFunction::loadExp(PyObject* obj, const std::string& function)
89 {
90   if(!load(obj, function))
91   {
92     std::string errorMessage = "Failed to load function '";
93     errorMessage += function;
94     errorMessage += "' from a python object.\n";
95     throw ExecutionException(errorMessage+getLastPyError());
96   }
97 }
98
99 }