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
30 PyPtr::PyPtr(std::nullptr_t copy)
35 PyPtr::PyPtr(PyObject* pyObj)
40 PyPtr::PyPtr(const PyPtr& copy)
43 Py_XINCREF(copy.get());
46 PyPtr::PyPtr(PyPtr&& move)
47 : _PyPtr(std::move(move))
51 PyPtr& PyPtr::operator=(std::nullptr_t copy)
53 _PyPtr::operator=(copy);
57 PyPtr& PyPtr::operator=(const PyPtr& copy)
59 PyObject* pyCopy = copy.get();
68 PyPtr& PyPtr::operator=(PyPtr&& move)
70 _PyPtr::operator=(std::move(move));
74 PyPtr PyPtr::getAttr(const std::string& attribute)const
76 PyObject* result = nullptr;
77 PyObject* thisObj = get();
78 if(nullptr == thisObj)
80 std::string message = "Cannot get attribute ";
82 message += " on a NULL object.\n";
83 throw AttributeException(message);
86 if(!PyObject_HasAttrString(thisObj, attribute.c_str()))
88 std::string message = "Attribute ";
90 message += " does not exist.\n";
91 throw AttributeException(message);
94 result = PyObject_GetAttrString(thisObj, attribute.c_str());
98 void PyPtr::setAttr(const std::string& attribute, const PyPtr& value)const
100 PyObject* thisObj = get();
101 if(nullptr == thisObj)
103 std::string message = "Cannot set attribute ";
104 message += attribute;
105 message += " on a NULL object.\n";
106 throw AttributeException(message);
109 if(0 > PyObject_SetAttrString(thisObj, attribute.c_str(), value.get()))
111 std::string message = "Failed to set attribute ";
112 message += attribute;
114 message += getLastPyError();
115 throw AttributeException(message);
119 std::string PyPtr::repr()const
122 PyObject* thisObj = get();
125 PyObject* pyResult = PyObject_Repr(thisObj);
126 if(pyResult && PyUnicode_Check(pyResult))
128 const char* charstr = PyUnicode_AsUTF8(pyResult);
129 if(charstr != nullptr)
132 Py_XDECREF(pyResult);