diff --git a/src/Base/PyObjectBase.cpp b/src/Base/PyObjectBase.cpp index 20336866fb72..dd55b4369e8e 100644 --- a/src/Base/PyObjectBase.cpp +++ b/src/Base/PyObjectBase.cpp @@ -148,6 +148,22 @@ PyObject* PyObjectBase::__getattr(PyObject * obj, char *attr) if (!static_cast(value)->isConst()) static_cast(value)->setAttributeOf(attr, pyObj); } + else if (value && PyCFunction_Check(value)) { + // ExtensionContainerPy::initialization() transfers the methods of an + // extension object by creating PyCFunction objects. + // At this point no 'self' object is passed but is handled and determined + // in ExtensionContainerPy::getCustomAttributes(). + // So, if we come through this section then it's an indication that + // something is wrong with the Python types. For example, a C++ class + // that adds an extension uses the same Python type as a wrapper than + // another C++ class without this extension. + PyCFunctionObject* cfunc = reinterpret_cast(value); + if (!cfunc->m_self) { + Py_DECREF(cfunc); + value = 0; + PyErr_Format(PyExc_AttributeError, "", attr); + } + } #endif return value; }