From 8d8c4cf308c6e066a4e625ff2a05c11e7b9fba0b Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Thu, 28 Jul 2011 16:37:12 -0300 Subject: Fix bug 923 - "Make QScriptValue (or QScriptValueIterator) implement the Python iterator protocol" Reviewer: Marcelo Lira Luciano Wolf --- PySide/QtScript/typesystem_script.xml | 23 ++++++++++++++++++++++- tests/QtScript/qscriptvalue_test.py | 16 +++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/PySide/QtScript/typesystem_script.xml b/PySide/QtScript/typesystem_script.xml index 738c44bda..05afba262 100644 --- a/PySide/QtScript/typesystem_script.xml +++ b/PySide/QtScript/typesystem_script.xml @@ -72,7 +72,28 @@ } + + + %PYARG_0 = Shiboken::createWrapper(new QScriptValueIterator(*%CPPSELF), true, true); + + - + + + + + + + + + if (%CPPSELF.hasNext()) { + %CPPSELF.next(); + %PYARG_0 = Shiboken::makeTuple(%CPPSELF.name(), %CPPSELF.value().toVariant()); + } else { + PyErr_SetNone(PyExc_StopIteration); + } + + + diff --git a/tests/QtScript/qscriptvalue_test.py b/tests/QtScript/qscriptvalue_test.py index d029bf9fc..e5b6a637f 100644 --- a/tests/QtScript/qscriptvalue_test.py +++ b/tests/QtScript/qscriptvalue_test.py @@ -1,6 +1,6 @@ import unittest import PySide -from PySide.QtScript import QScriptEngine, QScriptValue +from PySide.QtScript import * from helper import UsesQApplication @@ -19,6 +19,20 @@ class TestQScriptValue (UsesQApplication): value = QScriptValue("somePerson = { firstName: 'John', lastName: 'Doe' }") value2 = eval(repr(value)) 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__': unittest.main() -- cgit v1.2.3