diff --git a/src/App/FeaturePythonPyImp.h b/src/App/FeaturePythonPyImp.h index 058866477..a9ab3a352 100644 --- a/src/App/FeaturePythonPyImp.h +++ b/src/App/FeaturePythonPyImp.h @@ -23,10 +23,8 @@ #ifndef APP_FEATUREPYTHONPYIMP_H #define APP_FEATUREPYTHONPYIMP_H -#include -#include -#include #include +#include namespace App { @@ -52,7 +50,7 @@ public: int _setattr(char *attr, PyObject *value); // __setattr__ function protected: - std::map dyn_methods; + PyObject * dict_methods; private: }; diff --git a/src/App/FeaturePythonPyImp.inl b/src/App/FeaturePythonPyImp.inl index ba5694f4f..071a4ba0d 100644 --- a/src/App/FeaturePythonPyImp.inl +++ b/src/App/FeaturePythonPyImp.inl @@ -84,11 +84,13 @@ template FeaturePythonPyT::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T) : FeaturePyT(reinterpret_cast(pcObject), T) { + dict_methods = PyDict_New(); } template FeaturePythonPyT::~FeaturePythonPyT() { + Py_DECREF(dict_methods); } template @@ -113,27 +115,22 @@ int FeaturePythonPyT::_setattr(char *attr, PyObject *value) { int returnValue = FeaturePyT::_setattr(attr, value); if (returnValue == -1) { - if (value) { - if (PyFunction_Check(value)) { - std::map::iterator it = dyn_methods.find(attr); - if (it != dyn_methods.end()) { - Py_XDECREF(it->second); - } - dyn_methods[attr] = PyMethod_New(value, this, 0); - returnValue = 0; - PyErr_Clear(); - } + PyObject *s; + s = PyString_InternFromString(attr); + if (s == NULL) + return -1; + PyObject* dict_item = value; + if (value && PyFunction_Check(value)) { + dict_item = PyMethod_New(value, this, 0); + returnValue = _PyObject_GenericSetAttrWithDict(this, s, dict_item, dict_methods); + Py_XDECREF(dict_item); } else { - // delete - std::map::iterator it = dyn_methods.find(attr); - if (it != dyn_methods.end()) { - Py_XDECREF(it->second); - dyn_methods.erase(it); - returnValue = 0; - PyErr_Clear(); - } + returnValue = _PyObject_GenericSetAttrWithDict(this, s, dict_item, dict_methods); } + + Py_XDECREF(s); + PyErr_Clear(); } return returnValue; } @@ -162,8 +159,7 @@ PyObject *FeaturePythonPyT::_getattr(char *attr) dict = PyDict_Copy(dict_old); Py_DECREF(dict_old); // delete old dict - for (std::map::const_iterator it = dyn_methods.begin(); it != dyn_methods.end(); ++it) - PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString("")); + PyDict_Merge(dict, dict_methods, 0); if (PyErr_Occurred()) { Py_DECREF(dict); dict = 0; @@ -172,14 +168,15 @@ PyObject *FeaturePythonPyT::_getattr(char *attr) return dict; } - PyObject *rvalue = NULL; - std::map::iterator it = dyn_methods.find(attr); - if (it != dyn_methods.end()) { - Py_INCREF(it->second); - rvalue = it->second; - PyErr_Clear(); - return rvalue; - } + PyObject *s; + s = PyString_InternFromString(attr); + if (s == NULL) + return NULL; + PyObject *dict_item = NULL; + dict_item = _PyObject_GenericGetAttrWithDict(this, s, dict_methods); + Py_XDECREF(s); + if (dict_item) + return dict_item; PyErr_Clear(); return FeaturePyT::_getattr(attr); diff --git a/src/Mod/Part/App/Part2DObject.cpp b/src/Mod/Part/App/Part2DObject.cpp index 84ffd8726..e5858a253 100644 --- a/src/Mod/Part/App/Part2DObject.cpp +++ b/src/Mod/Part/App/Part2DObject.cpp @@ -44,6 +44,7 @@ #endif +#include #include #include #include @@ -53,7 +54,7 @@ #include "DatumFeature.h" #include -#include "Part2DObjectPy.h" +#include using namespace Part;