#1255 Python property setter fails due to _swig_setattr

open
nobody
python (259)
5
2012-12-21
2012-07-09
Anonymous
No

A python property to a python proxy class (not using -builtin) has calls to the set method (if any) intercepted by the _swig_setattr function in a way that never results in the setter being called:

...somemodule containing C++ SomeClass with attribute x, int getX(), void setX(int)...
%extend SomeClass {
%pythoncode {
x = property(_somemodule.SomeClass_getX, _somemodule.SomeClass_setX)
}
}

A readonly property works as expected:

>>> import somemodule
>>> sc = SomeClass()
>>> sc.x #calls SomeClass_getX(self)

But setting the property doesn't

>>> sc.x = 5 # SomeClass_setX(self, 5) isn't called, since _swig_setattr intercepts the call.

However, if the code in _swig_setattr_nondynamic is modified a bit it works as expected:

if (not static):
- self.__dict__[name] = value
+ object.__setattr__(self, name, value) #or use super(...) if any of the base classes might want their own, different setattr logic
else:

(Sorry - not sure where in the swig source tree the prototype for this function resides).

I don't *think* this should have wider consequences but it's possible it might interact with, eg, directors.

Discussion


Log in to post a comment.