Fix bug 924 - "Allow QScriptValue to be accessed via []"
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org>
Renato Araújo <renato.filho@openbossa.org>
This commit is contained in:
parent
48a1b539a4
commit
17e3d0da85
3 changed files with 34 additions and 0 deletions
|
|
@ -49,6 +49,19 @@
|
||||||
<enum-type name="PropertyFlag" flags="PropertyFlags"/>
|
<enum-type name="PropertyFlag" flags="PropertyFlags"/>
|
||||||
<enum-type name="ResolveFlag" flags="ResolveFlags"/>
|
<enum-type name="ResolveFlag" flags="ResolveFlags"/>
|
||||||
<enum-type name="SpecialValue"/>
|
<enum-type name="SpecialValue"/>
|
||||||
|
<add-function signature="__mgetitem__">
|
||||||
|
<inject-code>
|
||||||
|
Shiboken::AutoDecRef key(PyObject_Str(_key));
|
||||||
|
QVariant res = %CPPSELF.property(PyString_AS_STRING(key.object())).toVariant();
|
||||||
|
if (res.isValid()) {
|
||||||
|
return %CONVERTTOPYTHON[QVariant](res);
|
||||||
|
} else {
|
||||||
|
PyObject* errorType = PyInt_Check(_key) ? PyExc_IndexError : PyExc_KeyError;
|
||||||
|
PyErr_SetString(errorType, "Key not found.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
</inject-code>
|
||||||
|
</add-function>
|
||||||
</value-type>
|
</value-type>
|
||||||
<object-type name="QScriptValueIterator"/>
|
<object-type name="QScriptValueIterator"/>
|
||||||
</typesystem>
|
</typesystem>
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
PYSIDE_TEST(base_test.py)
|
PYSIDE_TEST(base_test.py)
|
||||||
PYSIDE_TEST(engine_test.py)
|
PYSIDE_TEST(engine_test.py)
|
||||||
PYSIDE_TEST(property_test.py)
|
PYSIDE_TEST(property_test.py)
|
||||||
|
PYSIDE_TEST(qscriptvalue_test.py)
|
||||||
|
|
|
||||||
20
tests/QtScript/qscriptvalue_test.py
Normal file
20
tests/QtScript/qscriptvalue_test.py
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
import unittest
|
||||||
|
from PySide.QtCore import *
|
||||||
|
from PySide.QtScript import *
|
||||||
|
|
||||||
|
class TestQScriptValue (unittest.TestCase):
|
||||||
|
|
||||||
|
def testOperator(self):
|
||||||
|
app = QCoreApplication([])
|
||||||
|
|
||||||
|
engine = QScriptEngine()
|
||||||
|
value = engine.evaluate('x = {"a": 1, "b":2}')
|
||||||
|
self.assertEqual(value['a'], 1)
|
||||||
|
self.assertRaises(KeyError, value.__getitem__, 'c')
|
||||||
|
value = engine.evaluate('x = ["x", "y", "z"]')
|
||||||
|
self.assertEqual(value[2], 'z')
|
||||||
|
self.assertRaises(IndexError, value.__getitem__, 23)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue