| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
// clang-format off |
| 25 |
namespace App |
| 26 |
{ |
| 27 |
|
| 28 |
/// Type structure of FeaturePythonPyT |
| 29 |
template<class FeaturePyT> |
| 30 |
PyTypeObject FeaturePythonPyT<FeaturePyT>::Type = { |
| 31 |
PyVarObject_HEAD_INIT(&PyType_Type,0) |
| 32 |
"FeaturePython", /*tp_name*/ |
| 33 |
sizeof(FeaturePythonPyT), /*tp_basicsize*/ |
| 34 |
0, /*tp_itemsize*/ |
| 35 |
/* methods */ |
| 36 |
FeaturePyT::PyDestructor, /*tp_dealloc*/ |
| 37 |
0, /*tp_vectorcall_offset*/ |
| 38 |
nullptr, /*tp_getattr*/ |
| 39 |
nullptr, /*tp_setattr*/ |
| 40 |
nullptr, /*tp_compare*/ |
| 41 |
nullptr, /*tp_repr*/ |
| 42 |
nullptr, /*tp_as_number*/ |
| 43 |
nullptr, /*tp_as_sequence*/ |
| 44 |
nullptr, /*tp_as_mapping*/ |
| 45 |
nullptr, /*tp_hash*/ |
| 46 |
nullptr, /*tp_call */ |
| 47 |
nullptr, /*tp_str */ |
| 48 |
FeaturePyT::__getattro, /*tp_getattro*/ |
| 49 |
__setattro, /*tp_setattro*/ |
| 50 |
/* --- Functions to access object as input/output buffer ---------*/ |
| 51 |
nullptr, /* tp_as_buffer */ |
| 52 |
/* --- Flags to define presence of optional/expanded features */ |
| 53 |
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 54 |
"This is the father of all Feature classes", /*tp_doc */ |
| 55 |
nullptr, /*tp_traverse */ |
| 56 |
nullptr, /*tp_clear */ |
| 57 |
nullptr, /*tp_richcompare */ |
| 58 |
0, /*tp_weaklistoffset */ |
| 59 |
nullptr, /*tp_iter */ |
| 60 |
nullptr, /*tp_iternext */ |
| 61 |
nullptr, /*tp_methods */ |
| 62 |
nullptr, /*tp_members */ |
| 63 |
nullptr, /*tp_getset */ |
| 64 |
&FeaturePyT::Type, /*tp_base */ |
| 65 |
nullptr, /*tp_dict */ |
| 66 |
nullptr, /*tp_descr_get */ |
| 67 |
nullptr, /*tp_descr_set */ |
| 68 |
0, /*tp_dictoffset */ |
| 69 |
FeaturePyT::__PyInit, /*tp_init */ |
| 70 |
nullptr, /*tp_alloc */ |
| 71 |
nullptr, /*tp_new */ |
| 72 |
nullptr, /*tp_free Low-level free-memory routine */ |
| 73 |
nullptr, /*tp_is_gc For PyObject_IS_GC */ |
| 74 |
nullptr, /*tp_bases */ |
| 75 |
nullptr, /*tp_mro method resolution order */ |
| 76 |
nullptr, /*tp_cache */ |
| 77 |
nullptr, /*tp_subclasses */ |
| 78 |
nullptr, /*tp_weaklist */ |
| 79 |
nullptr, /*tp_del */ |
| 80 |
0, /*tp_version_tag */ |
| 81 |
nullptr /*tp_finalize */ |
| 82 |
,0 /*tp_vectorcall */ |
| 83 |
#if PY_VERSION_HEX >= 0x030c0000 |
| 84 |
,0 /*tp_watched */ |
| 85 |
#endif |
| 86 |
#if PY_VERSION_HEX >= 0x030d0000 |
| 87 |
,0 /*tp_versions_used */ |
| 88 |
#endif |
| 89 |
}; |
| 90 |
|
| 91 |
template<class FeaturePyT> |
| 92 |
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T) |
| 93 |
: FeaturePyT(static_cast<typename FeaturePyT::PointerType>(pcObject), T) |
| 94 |
{ |
| 95 |
Base::PyGILStateLocker lock; |
| 96 |
dict_methods = PyDict_New(); |
| 97 |
} |
| 98 |
|
| 99 |
template<class FeaturePyT> |
| 100 |
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT() |
| 101 |
{ |
| 102 |
Base::PyGILStateLocker lock; |
| 103 |
Py_DECREF(dict_methods); |
| 104 |
} |
| 105 |
|
| 106 |
template<class FeaturePyT> |
| 107 |
int FeaturePythonPyT<FeaturePyT>::__setattro(PyObject *obj, PyObject *attro, PyObject *value) |
| 108 |
{ |
| 109 |
const char *attr; |
| 110 |
attr = PyUnicode_AsUTF8(attro); |
| 111 |
// This overwrites PyObjectBase::__setattr because this actively disallows one to delete an attribute |
| 112 |
// |
| 113 |
|
| 114 |
if (!static_cast<Base::PyObjectBase*>(obj)->isValid()){ |
| 115 |
PyErr_Format(PyExc_ReferenceError, "Cannot access attribute '%s' of deleted object", attr); |
| 116 |
return -1; |
| 117 |
} |
| 118 |
|
| 119 |
int ret = static_cast<Base::PyObjectBase*>(obj)->_setattr(attr, value); |
| 120 |
if (ret == 0) { |
| 121 |
static_cast<Base::PyObjectBase*>(obj)->startNotify(); |
| 122 |
} |
| 123 |
return ret; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
template<class FeaturePyT> |
| 128 |
int FeaturePythonPyT<FeaturePyT>::_setattr(const char *attr, PyObject *value) |
| 129 |
{ |
| 130 |
App::Property *prop = FeaturePyT::getPropertyContainerPtr()->getPropertyByName(attr); |
| 131 |
if (prop && !value) { |
| 132 |
PyErr_Format(PyExc_AttributeError, "Cannot delete attribute: '%s'", attr); |
| 133 |
return -1; |
| 134 |
} |
| 135 |
|
| 136 |
int returnValue = FeaturePyT::_setattr(attr, value); |
| 137 |
if (returnValue == -1) { |
| 138 |
PyObject* dict_item = value; |
| 139 |
if (value) { |
| 140 |
if (PyFunction_Check(value)) { |
| 141 |
PyErr_Clear(); |
| 142 |
dict_item = PyMethod_New(value, this); |
| 143 |
returnValue = PyDict_SetItemString(dict_methods, attr, dict_item); |
| 144 |
Py_XDECREF(dict_item); |
| 145 |
} |
| 146 |
} |
| 147 |
else { |
| 148 |
// delete |
| 149 |
PyErr_Clear(); |
| 150 |
returnValue = PyDict_DelItemString(dict_methods, attr); |
| 151 |
if (returnValue < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 152 |
PyErr_SetString(PyExc_AttributeError, attr); |
| 153 |
} |
| 154 |
} |
| 155 |
return returnValue; |
| 156 |
} |
| 157 |
|
| 158 |
template<class FeaturePyT> |
| 159 |
PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(const char *attr) |
| 160 |
{ |
| 161 |
// See CallTipsList::extractTips |
| 162 |
if (Base::streq(attr, "__fc_template__")) { |
| 163 |
Py_INCREF(Py_None); |
| 164 |
return Py_None; |
| 165 |
} |
| 166 |
|
| 167 |
// get only attributes of this type |
| 168 |
if (Base::streq(attr, "__dict__")) { |
| 169 |
// Return the default dict |
| 170 |
PyTypeObject *tp = this->ob_type; |
| 171 |
// register type if needed |
| 172 |
if (!tp->tp_dict) { |
| 173 |
if (PyType_Ready(tp) < 0) |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
PyObject* dict = FeaturePyT::_getattr(attr); |
| 178 |
if (dict && PyDict_CheckExact(dict)) { |
| 179 |
PyObject* dict_old = dict; |
| 180 |
dict = PyDict_Copy(dict_old); |
| 181 |
Py_DECREF(dict_old); // delete old dict |
| 182 |
PyDict_Merge(dict, dict_methods, 0); |
| 183 |
} |
| 184 |
return dict; |
| 185 |
} |
| 186 |
|
| 187 |
// find the attribute in the dict |
| 188 |
PyObject *dict_item = nullptr; |
| 189 |
dict_item = PyDict_GetItemString(dict_methods, attr); |
| 190 |
if (dict_item) { |
| 191 |
Py_INCREF(dict_item); |
| 192 |
return dict_item; |
| 193 |
} |
| 194 |
|
| 195 |
// search for the attribute in the base class |
| 196 |
PyErr_Clear(); |
| 197 |
return FeaturePyT::_getattr(attr); |
| 198 |
} |
| 199 |
|
| 200 |
} //namespace App |
| 201 |
// clang-format on |