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
24 std::string getLastPyError()
26 std::string result="";
29 PyObject *ptype, *pvalue, *ptraceback;
30 PyObject *pystr, *module_name, *pyth_module, *pyth_func;
31 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
32 PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
33 pystr = PyObject_Str(pvalue);
34 const char* charstr = PyUnicode_AsUTF8(pystr);
35 if(charstr != nullptr)
36 result = std::string(charstr);
40 /* See if we can get a full traceback */
43 module_name = PyUnicode_FromString("traceback");
44 pyth_module = PyImport_Import(module_name);
45 Py_DECREF(module_name);
48 pyth_func = PyObject_GetAttrString(pyth_module, "format_exception");
49 if (pyth_func && PyCallable_Check(pyth_func))
52 pyList = PyObject_CallFunctionObjArgs(pyth_func, ptype, pvalue, ptraceback, NULL);
55 int n = PyList_Size(pyList);
56 for(int i=0; i<n; i++)
58 pystr = PyList_GetItem(pyList,i);
59 const char* charstr = PyUnicode_AsUTF8(pystr);
60 if(charstr != nullptr)
66 Py_XDECREF(pyth_func);
67 Py_DECREF(pyth_module);
72 Py_XDECREF(ptraceback);
77 ConversionCheck::ConversionCheck()
82 ConversionCheck::ConversionCheck(const std::string& error)
87 ConversionCheck::~ConversionCheck()
91 ConversionCheck::operator bool()const
93 return _message.size() == 0;
96 const std::string& ConversionCheck::getMessage()const
101 void ConversionCheck::addError(const std::string& expectedType, PyObject * obj)
103 std::string newMessage;
107 PyObject* pyResult = PyObject_Repr(obj);
108 if(pyResult && PyUnicode_Check(pyResult))
110 const char* charstr = PyUnicode_AsUTF8(pyResult);
111 if(charstr != nullptr)
115 pyRepr = "unknown representation";
116 Py_XDECREF(pyResult);
117 if(pyRepr.size() > 80)
119 pyRepr = pyRepr.substr(0, 76);
122 const std::string beginMessage = "Cannot convert the python object ";
123 const std::string midMessage = " to c++ type ";
124 int messageSize = pyRepr.size() + beginMessage.size() + midMessage.size();
125 newMessage = beginMessage;
126 if(messageSize >= 80)
129 newMessage += pyRepr;
133 newMessage += " to c++ type ";
134 newMessage += expectedType;
139 newMessage = "Cannont convert a NULL python object to c++ type ";
140 newMessage += expectedType;
143 addErrorMessage(newMessage);
146 void ConversionCheck::addError(const ConversionCheck& err)
148 _message += err.getMessage();
151 void ConversionCheck::addErrorMessage(const std::string& message)
156 void ConversionCheck::reset()
161 Exception::Exception(const std::string& message)
167 const char* Exception::what() const noexcept
169 return _message.c_str();
172 ConversionException::ConversionException(const std::string& message)
177 ExecutionException::ExecutionException(const std::string& message)
182 AttributeException::AttributeException(const std::string& message)