Avoid read the property in the QObject constructor.

Create unit test to verify if the python property is setted during the constructor.

Reviewer: Hugo Parente <hugo.lima@openbossa.org>
          Luciano Wolf <luciano.wolf@openbossa.org>
This commit is contained in:
Renato Filho 2010-08-16 12:07:12 -03:00
commit 2d24d300d8
2 changed files with 21 additions and 11 deletions

View file

@ -231,12 +231,14 @@ const char* qproperty_get_type(PyObject* self)
PyObject* qproperty_get_object(PyObject* source, PyObject* name) PyObject* qproperty_get_object(PyObject* source, PyObject* name)
{ {
if (PyObject_HasAttr(source, name)) { PyObject* attr = PyObject_GenericGetAttr(source, name);
PyObject* attr = PyObject_GenericGetAttr(source, name); if (attr && isQPropertyType(attr))
if (isQPropertyType(attr)) return attr;
return attr;
if (!attr)
PyErr_Clear(); //Clear possible error caused by PyObject_GenericGetAttr
else
Py_DECREF(attr); Py_DECREF(attr);
}
return 0; return 0;
} }

View file

@ -13,8 +13,17 @@ class MySize(QSize):
'''Extended class''' '''Extended class'''
pass pass
class ExtQObject(QObject): class ExQObject(QObject):
registeredproperty = QProperty(int) def __init__(self, *args, **kargs):
QObject.__init__(self, *args, **kargs)
def setProperty(self, value):
self._value = value
def getProperty(self):
return self._value
registeredproperty = QProperty(int, getProperty, setProperty)
class MyObject(QObject): class MyObject(QObject):
'''Test Property''' '''Test Property'''
@ -105,7 +114,6 @@ class PropertyCase(unittest.TestCase):
self.assertTrue(obj.property('foo') is mysize) self.assertTrue(obj.property('foo') is mysize)
class PropertyWithConstructorCase(unittest.TestCase): class PropertyWithConstructorCase(unittest.TestCase):
'''Test case for QObject properties set using named arguments in the constructor.''' '''Test case for QObject properties set using named arguments in the constructor.'''
@ -118,17 +126,17 @@ class PropertyWithConstructorCase(unittest.TestCase):
self.assertRaises(AttributeError, QObject, dummy=42) self.assertRaises(AttributeError, QObject, dummy=42)
def testPythonDeclaredProperty(self): def testPythonDeclaredProperty(self):
obj = ExtQObject(registeredproperty=123) obj = ExQObject(registeredproperty=123)
self.assertEqual(obj.registeredproperty, 123)
def testConstructorPropertyInQObjectDerived(self): def testConstructorPropertyInQObjectDerived(self):
#QTimer(property=value) for existing C++ property #QTimer(property=value) for existing C++ property
obj = QTimer(objectName='dummy') obj = QTimer(objectName='dummy')
self.assertEqual(obj.objectName(), 'dummy') self.assertEqual(obj.objectName(), 'dummy')
def testPythonProperty(self): def testReadOnlyPythonProperty(self):
o = MyObject() o = MyObject()
self.assertEqual(o.pp, 42) self.assertEqual(o.pp, 42)
o.pp = 0
self.assertRaises(AttributeError, o.trySetPP) self.assertRaises(AttributeError, o.trySetPP)
if __name__ == '__main__': if __name__ == '__main__':