Fix bug 923 - "Make QScriptValue (or QScriptValueIterator) implement the Python iterator protocol"
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org>
Luciano Wolf <luciano.wolf@openbossa.org>
This commit is contained in:
parent
d38ba8cbb8
commit
20af27cac5
2 changed files with 37 additions and 2 deletions
|
|
@ -72,7 +72,28 @@
|
||||||
}
|
}
|
||||||
</inject-code>
|
</inject-code>
|
||||||
</add-function>
|
</add-function>
|
||||||
|
<add-function signature="__iter__()" return-type="PyObject*">
|
||||||
|
<inject-code>
|
||||||
|
%PYARG_0 = Shiboken::createWrapper(new QScriptValueIterator(*%CPPSELF), true, true);
|
||||||
|
</inject-code>
|
||||||
|
</add-function>
|
||||||
</value-type>
|
</value-type>
|
||||||
<object-type name="QScriptValueIterator"/>
|
<object-type name="QScriptValueIterator">
|
||||||
|
<add-function signature="__iter__()" return-type="PyObject*">
|
||||||
|
<inject-code>
|
||||||
|
<insert-template name="__iter__" />
|
||||||
|
</inject-code>
|
||||||
|
</add-function>
|
||||||
|
<add-function signature="__next__()" return-type="PyObject*">
|
||||||
|
<inject-code>
|
||||||
|
if (%CPPSELF.hasNext()) {
|
||||||
|
%CPPSELF.next();
|
||||||
|
%PYARG_0 = Shiboken::makeTuple(%CPPSELF.name(), %CPPSELF.value().toVariant());
|
||||||
|
} else {
|
||||||
|
PyErr_SetNone(PyExc_StopIteration);
|
||||||
|
}
|
||||||
|
</inject-code>
|
||||||
|
</add-function>
|
||||||
|
</object-type>
|
||||||
</typesystem>
|
</typesystem>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import unittest
|
import unittest
|
||||||
import PySide
|
import PySide
|
||||||
from PySide.QtScript import QScriptEngine, QScriptValue
|
from PySide.QtScript import *
|
||||||
|
|
||||||
from helper import UsesQApplication
|
from helper import UsesQApplication
|
||||||
|
|
||||||
|
|
@ -19,6 +19,20 @@ class TestQScriptValue (UsesQApplication):
|
||||||
value = QScriptValue("somePerson = { firstName: 'John', lastName: 'Doe' }")
|
value = QScriptValue("somePerson = { firstName: 'John', lastName: 'Doe' }")
|
||||||
value2 = eval(repr(value))
|
value2 = eval(repr(value))
|
||||||
self.assertEqual(value.toString(), value2.toString())
|
self.assertEqual(value.toString(), value2.toString())
|
||||||
|
self.assertEqual(value.toVariant(), value2.toVariant())
|
||||||
|
|
||||||
|
def testIteratorProtocol(self):
|
||||||
|
engine = QScriptEngine()
|
||||||
|
value = engine.evaluate('x = {"a": 1, "b":2}')
|
||||||
|
d = {}
|
||||||
|
for k, v in QScriptValueIterator(value):
|
||||||
|
d[k] = v
|
||||||
|
self.assertEqual(d, {'a': 1, 'b': 2})
|
||||||
|
|
||||||
|
d = {}
|
||||||
|
for k, v in value:
|
||||||
|
d[k] = v
|
||||||
|
self.assertEqual(d, {'a': 1, 'b': 2})
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue