use only Python API for FeaturePythonPyT

This commit is contained in:
wmayer 2016-11-10 13:37:30 +01:00
parent 26daea0c88
commit 53815e7aed
3 changed files with 29 additions and 33 deletions

View File

@ -23,10 +23,8 @@
#ifndef APP_FEATUREPYTHONPYIMP_H #ifndef APP_FEATUREPYTHONPYIMP_H
#define APP_FEATUREPYTHONPYIMP_H #define APP_FEATUREPYTHONPYIMP_H
#include <map>
#include <string>
#include <Base/Console.h>
#include <Base/BaseClass.h> #include <Base/BaseClass.h>
#include <Base/PyObjectBase.h>
namespace App namespace App
{ {
@ -52,7 +50,7 @@ public:
int _setattr(char *attr, PyObject *value); // __setattr__ function int _setattr(char *attr, PyObject *value); // __setattr__ function
protected: protected:
std::map<std::string, PyObject*> dyn_methods; PyObject * dict_methods;
private: private:
}; };

View File

@ -84,11 +84,13 @@ template<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T) FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T)
: FeaturePyT(reinterpret_cast<typename FeaturePyT::PointerType>(pcObject), T) : FeaturePyT(reinterpret_cast<typename FeaturePyT::PointerType>(pcObject), T)
{ {
dict_methods = PyDict_New();
} }
template<class FeaturePyT> template<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT() FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT()
{ {
Py_DECREF(dict_methods);
} }
template<class FeaturePyT> template<class FeaturePyT>
@ -113,27 +115,22 @@ int FeaturePythonPyT<FeaturePyT>::_setattr(char *attr, PyObject *value)
{ {
int returnValue = FeaturePyT::_setattr(attr, value); int returnValue = FeaturePyT::_setattr(attr, value);
if (returnValue == -1) { if (returnValue == -1) {
if (value) { PyObject *s;
if (PyFunction_Check(value)) { s = PyString_InternFromString(attr);
std::map<std::string, PyObject*>::iterator it = dyn_methods.find(attr); if (s == NULL)
if (it != dyn_methods.end()) { return -1;
Py_XDECREF(it->second); PyObject* dict_item = value;
} if (value && PyFunction_Check(value)) {
dyn_methods[attr] = PyMethod_New(value, this, 0); dict_item = PyMethod_New(value, this, 0);
returnValue = 0; returnValue = _PyObject_GenericSetAttrWithDict(this, s, dict_item, dict_methods);
PyErr_Clear(); Py_XDECREF(dict_item);
}
} }
else { else {
// delete returnValue = _PyObject_GenericSetAttrWithDict(this, s, dict_item, dict_methods);
std::map<std::string, PyObject*>::iterator it = dyn_methods.find(attr);
if (it != dyn_methods.end()) {
Py_XDECREF(it->second);
dyn_methods.erase(it);
returnValue = 0;
PyErr_Clear();
}
} }
Py_XDECREF(s);
PyErr_Clear();
} }
return returnValue; return returnValue;
} }
@ -162,8 +159,7 @@ PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(char *attr)
dict = PyDict_Copy(dict_old); dict = PyDict_Copy(dict_old);
Py_DECREF(dict_old); // delete old dict Py_DECREF(dict_old); // delete old dict
for (std::map<std::string, PyObject*>::const_iterator it = dyn_methods.begin(); it != dyn_methods.end(); ++it) PyDict_Merge(dict, dict_methods, 0);
PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString(""));
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
Py_DECREF(dict); Py_DECREF(dict);
dict = 0; dict = 0;
@ -172,14 +168,15 @@ PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(char *attr)
return dict; return dict;
} }
PyObject *rvalue = NULL; PyObject *s;
std::map<std::string, PyObject*>::iterator it = dyn_methods.find(attr); s = PyString_InternFromString(attr);
if (it != dyn_methods.end()) { if (s == NULL)
Py_INCREF(it->second); return NULL;
rvalue = it->second; PyObject *dict_item = NULL;
PyErr_Clear(); dict_item = _PyObject_GenericGetAttrWithDict(this, s, dict_methods);
return rvalue; Py_XDECREF(s);
} if (dict_item)
return dict_item;
PyErr_Clear(); PyErr_Clear();
return FeaturePyT::_getattr(attr); return FeaturePyT::_getattr(attr);

View File

@ -44,6 +44,7 @@
#endif #endif
#include <Base/Console.h>
#include <Base/Exception.h> #include <Base/Exception.h>
#include <Base/Reader.h> #include <Base/Reader.h>
#include <App/Property.h> #include <App/Property.h>
@ -53,7 +54,7 @@
#include "DatumFeature.h" #include "DatumFeature.h"
#include <App/FeaturePythonPyImp.h> #include <App/FeaturePythonPyImp.h>
#include "Part2DObjectPy.h" #include <Mod/Part/App/Part2DObjectPy.h>
using namespace Part; using namespace Part;