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

View File

@ -84,11 +84,13 @@ template<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(Base::BaseClass *pcObject, PyTypeObject *T)
: FeaturePyT(reinterpret_cast<typename FeaturePyT::PointerType>(pcObject), T)
{
dict_methods = PyDict_New();
}
template<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT()
{
Py_DECREF(dict_methods);
}
template<class FeaturePyT>
@ -113,27 +115,22 @@ int FeaturePythonPyT<FeaturePyT>::_setattr(char *attr, PyObject *value)
{
int returnValue = FeaturePyT::_setattr(attr, value);
if (returnValue == -1) {
if (value) {
if (PyFunction_Check(value)) {
std::map<std::string, PyObject*>::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<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();
}
returnValue = _PyObject_GenericSetAttrWithDict(this, s, dict_item, dict_methods);
}
Py_XDECREF(s);
PyErr_Clear();
}
return returnValue;
}
@ -162,8 +159,7 @@ PyObject *FeaturePythonPyT<FeaturePyT>::_getattr(char *attr)
dict = PyDict_Copy(dict_old);
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_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<FeaturePyT>::_getattr(char *attr)
return dict;
}
PyObject *rvalue = NULL;
std::map<std::string, PyObject*>::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);

View File

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