1
/***************************************************************************
2
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
27
/// Type structure of FeaturePythonPyT
28
template<class FeaturePyT>
29
PyTypeObject FeaturePythonPyT<FeaturePyT>::Type = {
30
PyVarObject_HEAD_INIT(&PyType_Type,0)
31
"FeaturePython", /*tp_name*/
32
sizeof(FeaturePythonPyT), /*tp_basicsize*/
35
FeaturePyT::PyDestructor, /*tp_dealloc*/
36
#if PY_VERSION_HEX >= 0x03080000
37
0, /*tp_vectorcall_offset*/
41
nullptr, /*tp_getattr*/
42
nullptr, /*tp_setattr*/
43
nullptr, /*tp_compare*/
45
nullptr, /*tp_as_number*/
46
nullptr, /*tp_as_sequence*/
47
nullptr, /*tp_as_mapping*/
51
FeaturePyT::__getattro, /*tp_getattro*/
52
__setattro, /*tp_setattro*/
53
/* --- Functions to access object as input/output buffer ---------*/
54
nullptr, /* tp_as_buffer */
55
/* --- Flags to define presence of optional/expanded features */
56
Py_TPFLAGS_BASETYPE|Py_TPFLAGS_DEFAULT, /*tp_flags */
57
"This is the father of all Feature classes", /*tp_doc */
58
nullptr, /*tp_traverse */
59
nullptr, /*tp_clear */
60
nullptr, /*tp_richcompare */
61
0, /*tp_weaklistoffset */
63
nullptr, /*tp_iternext */
64
nullptr, /*tp_methods */
65
nullptr, /*tp_members */
66
nullptr, /*tp_getset */
67
&FeaturePyT::Type, /*tp_base */
69
nullptr, /*tp_descr_get */
70
nullptr, /*tp_descr_set */
72
FeaturePyT::__PyInit, /*tp_init */
73
nullptr, /*tp_alloc */
75
nullptr, /*tp_free Low-level free-memory routine */
76
nullptr, /*tp_is_gc For PyObject_IS_GC */
77
nullptr, /*tp_bases */
78
nullptr, /*tp_mro method resolution order */
79
nullptr, /*tp_cache */
80
nullptr, /*tp_subclasses */
81
nullptr, /*tp_weaklist */
83
0, /*tp_version_tag */
84
nullptr /*tp_finalize */
85
#if PY_VERSION_HEX >= 0x03080000
87
#if PY_VERSION_HEX >= 0x030c0000
93
template<class FeaturePyT>
94
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T)
95
: FeaturePyT(static_cast<typename FeaturePyT::PointerType>(pcObject), T)
97
Base::PyGILStateLocker lock;
98
dict_methods = PyDict_New();
101
template<class FeaturePyT>
102
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT()
104
Base::PyGILStateLocker lock;
105
Py_DECREF(dict_methods);
108
template<class FeaturePyT>
109
int FeaturePythonPyT<FeaturePyT>::__setattro(PyObject *obj, PyObject *attro, PyObject *value)
112
attr = PyUnicode_AsUTF8(attro);
113
// This overwrites PyObjectBase::__setattr because this actively disallows to delete an attribute
116
if (!static_cast<Base::PyObjectBase*>(obj)->isValid()){
117
PyErr_Format(PyExc_ReferenceError, "Cannot access attribute '%s' of deleted object", attr);
121
int ret = static_cast<Base::PyObjectBase*>(obj)->_setattr(attr, value);
123
static_cast<Base::PyObjectBase*>(obj)->startNotify();
129
template<class FeaturePyT>
130
int FeaturePythonPyT<FeaturePyT>::_setattr(const char *attr, PyObject *value)
132
App::Property *prop = FeaturePyT::getPropertyContainerPtr()->getPropertyByName(attr);
133
if (prop && !value) {
134
PyErr_Format(PyExc_AttributeError, "Cannot delete attribute: '%s'", attr);
138
int returnValue = FeaturePyT::_setattr(attr, value);
139
if (returnValue == -1) {
140
PyObject* dict_item = value;
142
if (PyFunction_Check(value)) {
144
dict_item = PyMethod_New(value, this);
145
returnValue = PyDict_SetItemString(dict_methods, attr, dict_item);
146
Py_XDECREF(dict_item);
152
returnValue = PyDict_DelItemString(dict_methods, attr);
153
if (returnValue < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
154
PyErr_SetString(PyExc_AttributeError, attr);
160
template<class FeaturePyT>
161
PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(const char *attr)
163
// See CallTipsList::extractTips
164
if (Base::streq(attr, "__fc_template__")) {
169
// get only attributes of this type
170
if (Base::streq(attr, "__dict__")) {
171
// Return the default dict
172
PyTypeObject *tp = this->ob_type;
173
// register type if needed
175
if (PyType_Ready(tp) < 0)
179
PyObject* dict = FeaturePyT::_getattr(attr);
180
if (dict && PyDict_CheckExact(dict)) {
181
PyObject* dict_old = dict;
182
dict = PyDict_Copy(dict_old);
183
Py_DECREF(dict_old); // delete old dict
184
PyDict_Merge(dict, dict_methods, 0);
189
// find the attribute in the dict
190
PyObject *dict_item = nullptr;
191
dict_item = PyDict_GetItemString(dict_methods, attr);
193
Py_INCREF(dict_item);
197
// search for the attribute in the base class
199
return FeaturePyT::_getattr(attr);