FreeCAD

Форк
0
/
FeaturePythonPyImp.inl 
202 строки · 8.8 Кб
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
namespace App
25
{
26

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*/
33
    0,                                                /*tp_itemsize*/
34
    /* methods */
35
    FeaturePyT::PyDestructor,                         /*tp_dealloc*/
36
#if PY_VERSION_HEX >= 0x03080000
37
    0,                                                /*tp_vectorcall_offset*/
38
#else
39
    nullptr,                                          /*tp_print*/
40
#endif
41
    nullptr,                                          /*tp_getattr*/
42
    nullptr,                                          /*tp_setattr*/
43
    nullptr,                                          /*tp_compare*/
44
    nullptr,                                          /*tp_repr*/
45
    nullptr,                                          /*tp_as_number*/
46
    nullptr,                                          /*tp_as_sequence*/
47
    nullptr,                                          /*tp_as_mapping*/
48
    nullptr,                                          /*tp_hash*/
49
    nullptr,                                          /*tp_call */
50
    nullptr,                                          /*tp_str  */
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 */
62
    nullptr,                                          /*tp_iter */
63
    nullptr,                                          /*tp_iternext */
64
    nullptr,                                          /*tp_methods */
65
    nullptr,                                          /*tp_members */
66
    nullptr,                                          /*tp_getset */
67
    &FeaturePyT::Type,                                /*tp_base */
68
    nullptr,                                          /*tp_dict */
69
    nullptr,                                          /*tp_descr_get */
70
    nullptr,                                          /*tp_descr_set */
71
    0,                                                /*tp_dictoffset */
72
    FeaturePyT::__PyInit,                             /*tp_init */
73
    nullptr,                                          /*tp_alloc */
74
    nullptr,                                          /*tp_new */
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 */
82
    nullptr,                                          /*tp_del */
83
    0,                                                /*tp_version_tag */
84
    nullptr                                           /*tp_finalize */
85
#if PY_VERSION_HEX >= 0x03080000
86
    ,0                                                /*tp_vectorcall */
87
#if PY_VERSION_HEX >= 0x030c0000
88
    ,0                                                /*tp_watched */
89
#endif
90
#endif
91
};
92

93
template<class FeaturePyT>
94
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T)
95
    : FeaturePyT(static_cast<typename FeaturePyT::PointerType>(pcObject), T)
96
{
97
    Base::PyGILStateLocker lock;
98
    dict_methods = PyDict_New();
99
}
100

101
template<class FeaturePyT>
102
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT()
103
{
104
    Base::PyGILStateLocker lock;
105
    Py_DECREF(dict_methods);
106
}
107

108
template<class FeaturePyT>
109
int FeaturePythonPyT<FeaturePyT>::__setattro(PyObject *obj, PyObject *attro, PyObject *value)
110
{
111
    const char *attr;
112
    attr = PyUnicode_AsUTF8(attro);
113
    // This overwrites PyObjectBase::__setattr because this actively disallows to delete an attribute
114
    //
115

116
    if (!static_cast<Base::PyObjectBase*>(obj)->isValid()){
117
        PyErr_Format(PyExc_ReferenceError, "Cannot access attribute '%s' of deleted object", attr);
118
        return -1;
119
    }
120

121
    int ret = static_cast<Base::PyObjectBase*>(obj)->_setattr(attr, value);
122
    if (ret == 0) {
123
        static_cast<Base::PyObjectBase*>(obj)->startNotify();
124
    }
125
    return ret;
126
}
127

128

129
template<class FeaturePyT>
130
int FeaturePythonPyT<FeaturePyT>::_setattr(const char *attr, PyObject *value)
131
{
132
    App::Property *prop = FeaturePyT::getPropertyContainerPtr()->getPropertyByName(attr);
133
    if (prop && !value) {
134
        PyErr_Format(PyExc_AttributeError, "Cannot delete attribute: '%s'", attr);
135
        return -1;
136
    }
137

138
    int returnValue = FeaturePyT::_setattr(attr, value);
139
    if (returnValue == -1) {
140
        PyObject* dict_item = value;
141
        if (value) {
142
            if (PyFunction_Check(value)) {
143
                PyErr_Clear();
144
                dict_item = PyMethod_New(value, this);
145
                returnValue = PyDict_SetItemString(dict_methods, attr, dict_item);
146
                Py_XDECREF(dict_item);
147
            }
148
        }
149
        else {
150
            // delete
151
            PyErr_Clear();
152
            returnValue = PyDict_DelItemString(dict_methods, attr);
153
            if (returnValue < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
154
                PyErr_SetString(PyExc_AttributeError, attr);
155
        }
156
    }
157
    return returnValue;
158
}
159

160
template<class FeaturePyT>
161
PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(const char *attr)
162
{
163
    // See CallTipsList::extractTips
164
    if (Base::streq(attr, "__fc_template__")) {
165
        Py_INCREF(Py_None);
166
        return Py_None;
167
    }
168

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
174
        if (!tp->tp_dict) {
175
            if (PyType_Ready(tp) < 0)
176
                return nullptr;
177
        }
178

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);
185
        }
186
        return dict;
187
    }
188

189
    // find the attribute in the dict
190
    PyObject *dict_item = nullptr;
191
    dict_item = PyDict_GetItemString(dict_methods, attr);
192
    if (dict_item) {
193
        Py_INCREF(dict_item);
194
        return dict_item;
195
    }
196

197
    // search for the attribute in the base class
198
    PyErr_Clear();
199
    return FeaturePyT::_getattr(attr);
200
}
201

202
} //namespace App
203

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.