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:
parent
7c2ed7eae5
commit
92b88db7ab
2 changed files with 28 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue