aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-11-11 17:45:16 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2010-11-11 18:13:50 -0200
commit655219636b1500e82d543914045f4cc7ba7db95f (patch)
tree631b9309a21711d69f912fc8ec7a82b706e67e99
parent7013bd760e1ad46b31c019e0a11df504d6e2563e (diff)
Fix bug#436 - "Using a custom QValidator generates a segfault"
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--PySide/QtGui/typesystem_gui_common.xml57
-rw-r--r--tests/QtGui/CMakeLists.txt1
-rw-r--r--tests/QtGui/qvalidator_test.py85
3 files changed, 123 insertions, 20 deletions
diff --git a/PySide/QtGui/typesystem_gui_common.xml b/PySide/QtGui/typesystem_gui_common.xml
index 7dd9aaeb4..59b8aa14b 100644
--- a/PySide/QtGui/typesystem_gui_common.xml
+++ b/PySide/QtGui/typesystem_gui_common.xml
@@ -1588,16 +1588,7 @@
<!--### Obsolete in 4.3-->
</object-type>
<object-type name="QInputContextFactory"/>
- <object-type name="QIntValidator">
- <modify-function signature="validate(QString &amp;, int &amp;)const">
- <modify-argument index="return">
- <replace-type modified-type="PyObject"/>
- </modify-argument>
- <inject-code class="target" position="end">
- <insert-template name="return_tuple_QValidator_QString_int"/>
- </inject-code>
- </modify-function>
- </object-type>
+ <object-type name="QIntValidator" />
<object-type name="QItemDelegate">
<modify-function signature="doLayout(QStyleOptionViewItem,QRect*,QRect*,QRect*,bool)const" remove="all" />
<modify-function signature="drawCheck(QPainter*,QStyleOptionViewItem,QRect,Qt::CheckState)const">
@@ -1938,16 +1929,7 @@
<enum-type name="Direction"/>
</object-type>
<object-type name="QPushButton" />
- <object-type name="QRegExpValidator">
- <modify-function signature="validate(QString &amp;, int &amp;)const">
- <modify-argument index="return">
- <replace-type modified-type="PyObject"/>
- </modify-argument>
- <inject-code class="target" position="end">
- <insert-template name="return_tuple_QValidator_QString_int"/>
- </inject-code>
- </modify-function>
- </object-type>
+ <object-type name="QRegExpValidator" />
<object-type name="QScrollArea">
<modify-function signature="setWidget(QWidget*)">
<modify-argument index="1">
@@ -2287,6 +2269,41 @@
<modify-function signature="validate(QString &amp;, int &amp;)const">
<modify-argument index="return">
<replace-type modified-type="PyObject"/>
+ <conversion-rule class="native">
+ QValidator::State %out;
+
+ if (PySequence_Check(%PYARG_0)) {
+ Shiboken::AutoDecRef seq(PySequence_Fast(%PYARG_0, 0));
+ int size = PySequence_Fast_GET_SIZE(seq.object());
+
+ if (size > 1) {
+ if (Shiboken::Converter&lt;QString>::isConvertible(PySequence_Fast_GET_ITEM(seq.object(), 1)))
+ %1 = %CONVERTTOCPP[QString](PySequence_Fast_GET_ITEM(seq.object(), 1));
+ else
+ qWarning("%TYPE::%FUNCTION_NAME: Second tuple element is not convertible to unicode.");
+ }
+
+ if (size > 2) {
+ if (Shiboken::Converter&lt;int>::isConvertible(PySequence_Fast_GET_ITEM(seq.object(), 2)))
+ %2 = %CONVERTTOCPP[int](PySequence_Fast_GET_ITEM(seq.object(), 2));
+ else
+ qWarning("%TYPE::%FUNCTION_NAME: Second tuple element is not convertible to int.");
+ }
+ %PYARG_0 = PySequence_Fast_GET_ITEM(seq.object(), 0);
+ Py_INCREF(%PYARG_0); // we need to incref, because "%PYARG_0 = ..." will decref the tuple and the tuple will be decrefed again at the end of this scope.
+ }
+
+ // check retrun value
+ if (Shiboken::Converter&lt;QValidator::State>::isConvertible(%PYARG_0)) {
+ %out = %CONVERTTOCPP[QValidator::State](%PYARG_0);
+ } else {
+ PyErr_Format(PyExc_TypeError, "Invalid return value in function %s, expected %s, got %s.",
+ "QValidator.validate",
+ "PySide.QtGui.QValidator.State, (PySide.QtGui.QValidator.State,), (PySide.QtGui.QValidator.State, unicode) or (PySide.QtGui.QValidator.State, unicode, int)",
+ pyResult->ob_type->tp_name);
+ return QValidator::State();
+ }
+ </conversion-rule>
</modify-argument>
<inject-code class="target" position="end">
<insert-template name="return_tuple_QValidator_QString_int"/>
diff --git a/tests/QtGui/CMakeLists.txt b/tests/QtGui/CMakeLists.txt
index 7ff2f7e4b..9086829f8 100644
--- a/tests/QtGui/CMakeLists.txt
+++ b/tests/QtGui/CMakeLists.txt
@@ -71,6 +71,7 @@ PYSIDE_TEST(qtextedit_signal_test.py)
PYSIDE_TEST(qtoolbar_test.py)
PYSIDE_TEST(qtoolbox_test.py)
PYSIDE_TEST(qvariant_test.py)
+PYSIDE_TEST(qvalidator_test.py)
PYSIDE_TEST(qwidget_setlayout_test.py)
PYSIDE_TEST(qwidget_test.py)
PYSIDE_TEST(reference_count_test.py)
diff --git a/tests/QtGui/qvalidator_test.py b/tests/QtGui/qvalidator_test.py
new file mode 100644
index 000000000..52dbc8b0e
--- /dev/null
+++ b/tests/QtGui/qvalidator_test.py
@@ -0,0 +1,85 @@
+from PySide.QtCore import *
+from PySide.QtGui import *
+
+import unittest
+from helper import UsesQApplication
+
+class MyValidator1(QValidator):
+ def fixUp(self, input):
+ return "fixed"
+
+ def validate(self, input, pos):
+ return (QValidator.Acceptable, "fixed", 1)
+
+class MyValidator2(QValidator):
+ def fixUp(self, input):
+ return "fixed"
+
+ def validate(self, input, pos):
+ return (QValidator.Acceptable, "fixed")
+
+class MyValidator3(QValidator):
+ def fixUp(self, input):
+ return "fixed"
+
+ def validate(self, input, pos):
+ return (QValidator.Acceptable,)
+
+class MyValidator4(QValidator):
+ def fixUp(self, input):
+ return "fixed"
+
+ def validate(self, input, pos):
+ return QValidator.Acceptable
+
+class QValidatorTest(UsesQApplication):
+ def testValidator1(self):
+ line = QLineEdit()
+ line.setValidator(MyValidator1())
+ line.show()
+ line.setText("foo")
+
+ QTimer.singleShot(0, line.close)
+ self.app.exec_()
+
+ self.assertEqual(line.text(), "fixed")
+ self.assertEqual(line.cursorPosition(), 1)
+
+ def testValidator2(self):
+ line = QLineEdit()
+ line.setValidator(MyValidator2())
+ line.show()
+ line.setText("foo")
+
+ QTimer.singleShot(0, line.close)
+ self.app.exec_()
+
+ self.assertEqual(line.text(), "fixed")
+ self.assertEqual(line.cursorPosition(), 3)
+
+ def testValidator3(self):
+ line = QLineEdit()
+ line.setValidator(MyValidator3())
+ line.show()
+ line.setText("foo")
+
+ QTimer.singleShot(0, line.close)
+ self.app.exec_()
+
+ self.assertEqual(line.text(), "foo")
+ self.assertEqual(line.cursorPosition(), 3)
+
+ def testValidator4(self):
+ line = QLineEdit()
+ line.setValidator(MyValidator4())
+ line.show()
+ line.setText("foo")
+
+ QTimer.singleShot(0, line.close)
+ self.app.exec_()
+
+ self.assertEqual(line.text(), "foo")
+ self.assertEqual(line.cursorPosition(), 3)
+
+if __name__ == '__main__':
+ unittest.main()