Debian Codegraph @

src/App/ExtensionContainerPyImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2016 Stefan Tröger <[email protected]> *
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 #include <sstream>
24
25 #include "Application.h"
26
27 #include <App/ExtensionContainerPy.h>
28 #include <App/ExtensionContainerPy.cpp>
29 #include <App/Extension.h>
30
31 using namespace App;
32
33 // returns a string which represent the object e.g. when printed in python
34 std::string ExtensionContainerPy::representation() const
35 {
36 return {"<extension>"};
37 }
38
39 int ExtensionContainerPy::initialization()
40 {
41
42 if (!this->ob_type->tp_dict) {
43 if (PyType_Ready(this->ob_type) < 0) {
44 return 0;
45 }
46 }
47
48 ExtensionContainer::ExtensionIterator it = this->getExtensionContainerPtr()->extensionBegin();
49 for (; it != this->getExtensionContainerPtr()->extensionEnd(); ++it) {
50
51 // The PyTypeObject is shared by all instances of this type and therefore
52 // we have to add new methods only once.
53 PyObject* obj = (*it).second->getExtensionPyObject();
54 PyMethodDef* meth = obj->ob_type->tp_methods;
55 PyTypeObject* type = this->ob_type;
56 PyObject* dict = type->tp_dict;
57
58 // make sure to do the initialization only once
59 if (meth->ml_name) {
60 PyObject* item = PyDict_GetItemString(dict, meth->ml_name);
61 if (!item) {
62 // Note: this adds the methods to the type object to make sure
63 // it appears in the call tips. The function will not be bound
64 // to an instance
65 Py_INCREF(dict);
66 while (meth->ml_name) {
67 PyObject* func;
68 func = PyCFunction_New(meth, 0);
69 if (!func) {
70 break;
71 }
72 if (PyDict_SetItemString(dict, meth->ml_name, func) < 0) {
73 break;
74 }
75 Py_DECREF(func);
76 ++meth;
77 }
78
79 Py_DECREF(dict);
80 }
81 }
82
83 Py_DECREF(obj);
84 }
85 return 1;
86 }
87
88 int ExtensionContainerPy::finalization()
89 {
90 /*
91 //we need to delete all added python extensions, as we are the owner!
92 ExtensionContainer::ExtensionIterator it =
93 this->getExtensionContainerPtr()->extensionBegin(); for(; it !=
94 this->getExtensionContainerPtr()->extensionEnd(); ++it) {
95 if((*it).second->isPythonExtension())
96 delete (*it).second;
97 }*/
98 return 1;
99 }
100
101 PyObject* ExtensionContainerPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
102 {
103 // create a new instance of @self.export.Name@ and the Twin object
104 return nullptr;
105 }
106
107 // constructor method
108 int ExtensionContainerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
109 {
110 return 0;
111 }
112
113 PyObject* ExtensionContainerPy::getCustomAttributes(const char* attr) const
114 {
115 if (Base::streq(attr, "__dict__")) {
116 PyObject* dict = PyDict_New();
117 PyObject* props = PropertyContainerPy::getCustomAttributes("__dict__");
118 if (props && PyDict_Check(props)) {
119 PyDict_Merge(dict, props, 0);
120 Py_DECREF(props);
121 }
122
123 ExtensionContainer::ExtensionIterator it =
124 this->getExtensionContainerPtr()->extensionBegin();
125 for (; it != this->getExtensionContainerPtr()->extensionEnd(); ++it) {
126 // The PyTypeObject is shared by all instances of this type and therefore
127 // we have to add new methods only once.
128 PyObject* obj = (*it).second->getExtensionPyObject();
129 PyTypeObject* tp = Py_TYPE(obj);
130 if (tp && tp->tp_dict) {
131 Py_XINCREF(tp->tp_dict);
132 PyDict_Merge(dict, tp->tp_dict, 0);
133 Py_XDECREF(tp->tp_dict);
134 }
135 Py_DECREF(obj);
136 }
137
138 return dict;
139 }
140 // Search for the method called 'attr' in the extensions. If the search with
141 // Py_FindMethod is successful then a PyCFunction_New instance is returned
142 // with the PyObject pointer of the extension to make sure the method will
143 // be called for the correct instance.
144 PyObject* func = nullptr;
145 ExtensionContainer::ExtensionIterator it = this->getExtensionContainerPtr()->extensionBegin();
146 for (; it != this->getExtensionContainerPtr()->extensionEnd(); ++it) {
147 // The PyTypeObject is shared by all instances of this type and therefore
148 // we have to add new methods only once.
149 PyObject* obj = (*it).second->getExtensionPyObject();
150 PyObject* nameobj = PyUnicode_FromString(attr);
151 func = PyObject_GenericGetAttr(obj, nameobj);
152 Py_DECREF(nameobj);
153 Py_DECREF(obj);
154 if (func && PyCFunction_Check(func)) {
155 PyCFunctionObject* cfunc = reinterpret_cast<PyCFunctionObject*>(func);
156
157 // OK, that's what we wanted
158 if (cfunc->m_self == obj) {
159 break;
160 }
161 // otherwise cleanup the result again
162 Py_DECREF(func);
163 func = nullptr;
164 }
165 PyErr_Clear(); // clear the error set inside Py_FindMethod
166 }
167
168 return func;
169 }
170
171 int ExtensionContainerPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
172 {
173 return 0;
174 }
175
176 PyObject* ExtensionContainerPy::hasExtension(PyObject* args) const
177 {
178
179 char* type;
180 PyObject* deriv = Py_True;
181 if (!PyArg_ParseTuple(args, "s|O!", &type, &PyBool_Type, &deriv)) {
182 return nullptr;
183 }
184
185 // get the extension type asked for
186 bool derived = Base::asBoolean(deriv);
187 Base::Type extension = Base::Type::fromName(type);
188 if (extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
189 std::stringstream str;
190 str << "No extension found of type '" << type << "'" << std::ends;
191 throw Py::TypeError(str.str());
192 }
193
194 bool val = false;
195 if (getExtensionContainerPtr()->hasExtension(extension, derived)) {
196 val = true;
197 }
198
199 return PyBool_FromLong(val ? 1 : 0);
200 }
201
202 PyObject* ExtensionContainerPy::addExtension(PyObject* args)
203 {
204
205 char* typeId;
206 PyObject* proxy = nullptr;
207 if (!PyArg_ParseTuple(args, "s|O", &typeId, &proxy)) {
208 return nullptr;
209 }
210
211 if (proxy) {
212 PyErr_SetString(
213 PyExc_DeprecationWarning,
214 "Second argument is deprecated. It is ignored and will be removed in future versions. "
215 "The default Python feature proxy is used for extension method overrides.");
216 PyErr_Print();
217 }
218
219 // get the extension type asked for
220 Base::Type extension = Base::Type::fromName(typeId);
221 if (extension.isBad() || !extension.isDerivedFrom(App::Extension::getExtensionClassTypeId())) {
222 std::stringstream str;
223 str << "No extension found of type '" << typeId << "'" << std::ends;
224 throw Py::TypeError(str.str());
225 }
226
227 // register the extension
228 App::Extension* ext = static_cast<App::Extension*>(extension.createInstance());
229 // check if this really is a python extension!
230 if (!ext->isPythonExtension()) {
231 delete ext;
232 std::stringstream str;
233 str << "Extension is not a python addable version: '" << typeId << "'" << std::ends;
234 throw Py::TypeError(str.str());
235 }
236
237 GetApplication().signalBeforeAddingDynamicExtension(*getExtensionContainerPtr(), typeId);
238 ext->initExtension(getExtensionContainerPtr());
239
240 // The PyTypeObject is shared by all instances of this type and therefore
241 // we have to add new methods only once.
242 PyObject* obj = ext->getExtensionPyObject();
243 PyMethodDef* meth = obj->ob_type->tp_methods;
244 PyTypeObject* type = this->ob_type;
245 PyObject* dict = type->tp_dict;
246
247 // make sure to do the initialization only once
248 if (meth->ml_name) {
249 PyObject* item = PyDict_GetItemString(dict, meth->ml_name);
250 if (!item) {
251 // Note: this adds the methods to the type object to make sure
252 // it appears in the call tips. The function will not be bound
253 // to an instance
254 Py_INCREF(dict);
255 while (meth->ml_name) {
256 PyObject* func;
257 func = PyCFunction_New(meth, 0);
258 if (!func) {
259 break;
260 }
261 if (PyDict_SetItemString(dict, meth->ml_name, func) < 0) {
262 break;
263 }
264 Py_DECREF(func);
265 ++meth;
266 }
267
268 Py_DECREF(dict);
269 }
270 }
271
272 Py_DECREF(obj);
273
274 // throw the appropriate event
275 GetApplication().signalAddedDynamicExtension(*getExtensionContainerPtr(), typeId);
276
277 Py_Return;
278 }