Salome HOME
updated copyright message
[tools/py2cpp.git] / src / PyPtr.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 "PyPtr.hxx"
20 #include "Errors.hxx"
21
22 namespace py2cpp
23 {
24
25 PyPtr::PyPtr()
26 : _PyPtr()
27 {
28 }
29
30 PyPtr::PyPtr(std::nullptr_t copy)
31 : _PyPtr(copy)
32 {
33 }
34
35 PyPtr::PyPtr(PyObject* pyObj)
36 : _PyPtr(pyObj)
37 {
38 }
39
40 PyPtr::PyPtr(const PyPtr& copy)
41 : _PyPtr(copy.get())
42 {
43   Py_XINCREF(copy.get());
44 }
45
46 PyPtr::PyPtr(PyPtr&& move)
47 : _PyPtr(std::move(move))
48 {
49 }
50
51 PyPtr& PyPtr::operator=(std::nullptr_t copy)
52 {
53   _PyPtr::operator=(copy);
54   return *this;
55 }
56
57 PyPtr& PyPtr::operator=(const PyPtr& copy)
58 {
59   PyObject* pyCopy = copy.get();
60   if(get() != pyCopy)
61   {
62     Py_XINCREF(pyCopy);
63     reset(pyCopy);
64   }
65   return *this;
66 }
67
68 PyPtr& PyPtr::operator=(PyPtr&& move)
69 {
70   _PyPtr::operator=(std::move(move));
71   return *this;
72 }
73
74 PyPtr PyPtr::getAttr(const std::string& attribute)const
75 {
76   PyObject* result = nullptr;
77   PyObject* thisObj = get();
78   if(nullptr == thisObj)
79   {
80     std::string message = "Cannot get attribute ";
81     message += attribute;
82     message += " on a NULL object.\n";
83     throw AttributeException(message);
84   }
85
86   if(!PyObject_HasAttrString(thisObj, attribute.c_str()))
87   {
88     std::string message = "Attribute ";
89     message += attribute;
90     message += " does not exist.\n";
91     throw AttributeException(message);
92   }
93
94   result = PyObject_GetAttrString(thisObj, attribute.c_str());
95   return PyPtr(result);
96 }
97
98 void PyPtr::setAttr(const std::string& attribute, const PyPtr& value)const
99 {
100   PyObject* thisObj = get();
101   if(nullptr == thisObj)
102   {
103     std::string message = "Cannot set attribute ";
104     message += attribute;
105     message += " on a NULL object.\n";
106     throw AttributeException(message);
107   }
108
109   if(0 > PyObject_SetAttrString(thisObj, attribute.c_str(), value.get()))
110   {
111     std::string message = "Failed to set attribute ";
112     message += attribute;
113     message += ".\n";
114     message += getLastPyError();
115     throw AttributeException(message);
116   }
117 }
118
119 std::string PyPtr::repr()const
120 {
121   std::string result;
122   PyObject* thisObj = get();
123   if(thisObj)
124   {
125     PyObject* pyResult = PyObject_Repr(thisObj);
126     if(pyResult && PyUnicode_Check(pyResult))
127     {
128       const char* charstr = PyUnicode_AsUTF8(pyResult);
129       if(charstr != nullptr)
130         result = charstr;
131     }
132     Py_XDECREF(pyResult);
133   }
134   return result;
135 }
136
137 }