Skip to content

Commit

Permalink
issue #2902: replace members of PyObjectBase with a PyDictObject
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Feb 14, 2017
1 parent b72aa9f commit 4f23b56
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 42 deletions.
74 changes: 37 additions & 37 deletions src/Base/PyObjectBase.cpp
Expand Up @@ -37,7 +37,7 @@ PyObject* Base::BaseExceptionFreeCADError = 0;

// Constructor
PyObjectBase::PyObjectBase(void* p,PyTypeObject *T)
: _pcTwinPointer(p), parent(0), attribute(0)
: _pcTwinPointer(p), attrDict(0)
{
this->ob_type = T;
_Py_NewReference(this);
Expand All @@ -53,10 +53,7 @@ PyObjectBase::~PyObjectBase()
#ifdef FC_LOGPYOBJECTS
Base::Console().Log("PyO-: %s (%p)\n",this->ob_type->tp_name, this);
#endif
if (this->parent)
this->parent->DecRef();
if (this->attribute)
free(this->attribute); /* it's a strdup */
Py_XDECREF(attrDict);
}

/*------------------------------
Expand Down Expand Up @@ -278,48 +275,51 @@ PyObject *PyObjectBase::_repr(void)

void PyObjectBase::resetAttribute()
{
if (this->attribute) {
free(this->attribute);
this->attribute = 0;
}
if (this->parent) {
Py_DECREF(this->parent);
this->parent = 0;
if (attrDict) {
// This is the attribute name to the parent structure
// which we search for in the dict
PyObject* key = PyString_FromString("__attribute_of_parent__");
PyObject* attr = PyDict_GetItem(attrDict, key);
if (attr) {
PyObject* parent = PyDict_GetItem(attrDict, attr);
if (parent) {
PyDict_DelItem(attrDict, attr);
}
PyDict_DelItem(attrDict, key);
}
Py_DECREF(key);
}
}

void PyObjectBase::setAttributeOf(const char* attr, const PyObjectBase* par)
{
if (this->parent != par) {
Py_XDECREF(this->parent);
this->parent = const_cast<PyObjectBase*>(par);
Py_XINCREF(this->parent);
if (!attrDict) {
attrDict = PyDict_New();
}

if (this->attribute) {
if (strcmp(this->attribute, attr) != 0) {
free(this->attribute);
#if defined (__GNUC__)
this->attribute = strdup(attr);
#else
this->attribute = _strdup(attr);
#endif
}
}
else {
#if defined (__GNUC__)
this->attribute = strdup(attr);
#else
this->attribute = _strdup(attr);
#endif
}
PyObject* key = PyString_FromString("__attribute_of_parent__");
PyObject* attro = PyString_FromString(attr);
PyDict_SetItem(attrDict, key, attro);
PyDict_SetItem(attrDict, attro, const_cast<PyObjectBase*>(par));
Py_DECREF(attro);
Py_DECREF(key);
}

void PyObjectBase::startNotify()
{
if (this->attribute && this->parent) {
__setattr(this->parent, this->attribute, this);
if (PyErr_Occurred())
PyErr_Clear();
if (attrDict) {
// This is the attribute name to the parent structure
// which we search for in the dict
PyObject* key = PyString_FromString("__attribute_of_parent__");
PyObject* attr = PyDict_GetItem(attrDict, key);
if (attr) {
PyObject* parent = PyDict_GetItem(attrDict, attr);
if (parent) {
__setattr(parent, PyString_AsString(attr), this);
if (PyErr_Occurred())
PyErr_Clear();
}
}
Py_DECREF(key);
}
}
13 changes: 8 additions & 5 deletions src/Base/PyObjectBase.h
Expand Up @@ -196,7 +196,6 @@ class BaseExport PyObjectBase : public PyObject
protected:
/// destructor
virtual ~PyObjectBase();
void resetAttribute();

public:
/** Constructor
Expand Down Expand Up @@ -292,17 +291,21 @@ class BaseExport PyObjectBase : public PyObject
return StatusBits.test(1);
}

void setAttributeOf(const char* attr, const PyObjectBase* par);
void startNotify();

typedef void* PointerType ;
typedef void* PointerType;

private:
void setAttributeOf(const char* attr, const PyObjectBase* par);
void resetAttribute();

protected:
std::bitset<32> StatusBits;
/// pointer to the handled class
void * _pcTwinPointer;
PyObjectBase* parent;
char* attribute;

private:
PyObject* attrDict;
};


Expand Down

0 comments on commit 4f23b56

Please sign in to comment.