Make __dict__ accessible for Python builtin classes

Attribute set within instance of a SWIG Python wrapped class are
stored in SwigPyObject->dict, which tp_dictoffset slot is pointing to.

However, SWIG wrapped classes did not have a __dict__ attribute.
Inheriting subclasses did not get the attribute either because the
SWIG wrapped classes initialize the tp_dictoffset slot:

From http://bugs.python.org/issue16272:

"If a type defines a nonzero tp_dictoffset, that type is responsible for
defining a `__dict__` slot as part of the tp_getset structures. Failure to
do so will result in the dict being inaccessible from Python via
`obj.__dict__` from instances of the type or subtypes."

Provide a SwigPyObject_get___dict__() function to retrieve the dict
attribute or create it when it does not exist yet (it is normally
created when setting attribute set), and a PyGetSetDef entry pointing
to this function.
This commit is contained in:
Yoann Vandoorselaere 2015-02-02 11:15:31 +01:00
commit 92b88db7ab
2 changed files with 28 additions and 0 deletions

View file

@ -3057,6 +3057,17 @@ public:
}
/* If this is a builtin type, create a PyGetSetDef entry for this member variable. */
if (builtin) {
const char *memname = "__dict__";
Hash *h = Getattr(builtin_getset, memname);
if (!h) {
h = NewHash();
Setattr(builtin_getset, memname, h);
Delete(h);
}
Setattr(h, "getter", "SwigPyObject_get___dict__");
}
if (builtin_getter) {
String *memname = Getattr(n, "membervariableHandler:sym:name");
if (!memname)