store Python attributes of all extension types in one dict

This commit is contained in:
wmayer
2018-09-27 21:48:30 +02:00
parent 1d1abb6d29
commit 3d2d4bdfcb

View File

@@ -112,6 +112,30 @@ int ExtensionContainerPy::PyInit(PyObject* /*args*/, PyObject* /*kwd*/)
PyObject *ExtensionContainerPy::getCustomAttributes(const char* attr) const
{
if (Base::streq(attr, "__dict__")) {
PyObject* dict = PyDict_New();
PyObject* props = PropertyContainerPy::getCustomAttributes("__dict__");
if (props && PyDict_Check(props)) {
PyDict_Merge(dict, props, 0);
Py_DECREF(props);
}
ExtensionContainer::ExtensionIterator it = this->getExtensionContainerPtr()->extensionBegin();
for (; it != this->getExtensionContainerPtr()->extensionEnd(); ++it) {
// The PyTypeObject is shared by all instances of this type and therefore
// we have to add new methods only once.
PyObject* obj = (*it).second->getExtensionPyObject();
PyTypeObject *tp = Py_TYPE(obj);
if (tp && tp->tp_dict) {
Py_XINCREF(tp->tp_dict);
PyDict_Merge(dict, tp->tp_dict, 0);
Py_XDECREF(tp->tp_dict);
}
Py_DECREF(obj);
}
return dict;
}
// Search for the method called 'attr' in the extensions. If the search with
// Py_FindMethod is successful then a PyCFunction_New instance is returned
// with the PyObject pointer of the extension to make sure the method will