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
|
|
@ -381,6 +381,23 @@ typedef struct {
|
||||||
#endif
|
#endif
|
||||||
} SwigPyObject;
|
} SwigPyObject;
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef SWIGPYTHON_BUILTIN
|
||||||
|
|
||||||
|
SWIGRUNTIME PyObject *
|
||||||
|
SwigPyObject_get___dict__(PyObject *v, PyObject *SWIGUNUSEDPARM(args))
|
||||||
|
{
|
||||||
|
SwigPyObject *sobj = (SwigPyObject *)v;
|
||||||
|
|
||||||
|
if (!sobj->dict)
|
||||||
|
sobj->dict = PyDict_New();
|
||||||
|
|
||||||
|
Py_INCREF(sobj->dict);
|
||||||
|
return sobj->dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
SWIGRUNTIME PyObject *
|
SWIGRUNTIME PyObject *
|
||||||
SwigPyObject_long(SwigPyObject *v)
|
SwigPyObject_long(SwigPyObject *v)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3057,6 +3057,17 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If this is a builtin type, create a PyGetSetDef entry for this member variable. */
|
/* 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) {
|
if (builtin_getter) {
|
||||||
String *memname = Getattr(n, "membervariableHandler:sym:name");
|
String *memname = Getattr(n, "membervariableHandler:sym:name");
|
||||||
if (!memname)
|
if (!memname)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue