From 327d7c968d5eab272ef5ebd1cba3129b95418145 Mon Sep 17 00:00:00 2001 From: Yoann Vandoorselaere Date: Fri, 6 Feb 2015 16:24:43 +0100 Subject: [PATCH] Attribute of SWIG wrapped classes instances were overwritten on __init__() When a SWIG classes instances is initialized, its internal dictionary was reset to NULL, which result in the loss of any attribute that might have been set for the instance. Only initialize the internal dictionary on actual PyObject creation. class Test(MySwigWrappedClass): def __init__(self): self.val = "Random Value" MySwigWrappedClass.__init__(self) p = Test() print hasattr(p, "val") # Should return True, but used to return False --- Lib/python/pyrun.swg | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index daa0b7eef..7ef8d5e39 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1437,18 +1437,21 @@ SWIG_Python_NewPointerObj(PyObject *self, void *ptr, swig_type_info *type, int f newobj = (SwigPyObject *) newobj->next; newobj->next = next_self; newobj = (SwigPyObject *)next_self; +#ifdef SWIGPYTHON_BUILTIN + newobj->dict = 0; +#endif } } else { newobj = PyObject_New(SwigPyObject, clientdata->pytype); +#ifdef SWIGPYTHON_BUILTIN + newobj->dict = 0; +#endif } if (newobj) { newobj->ptr = ptr; newobj->ty = type; newobj->own = own; newobj->next = 0; -#ifdef SWIGPYTHON_BUILTIN - newobj->dict = 0; -#endif return (PyObject*) newobj; } return SWIG_Py_Void();