From 54b69c3595cadfbfa5f65849d4dd106718920976 Mon Sep 17 00:00:00 2001 From: Luciano Wolf Date: Fri, 18 Jun 2010 19:37:21 -0300 Subject: Adding qInstallMsgHandler() method support. Reviewer: Marcelo Lira Hugo Parente Lima --- PySide/QtCore/typesystem_core.xml | 32 ++++++++++++++++++++ tests/QtCore/CMakeLists.txt | 1 + tests/QtCore/qinstallmsghandler_test.py | 53 +++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 tests/QtCore/qinstallmsghandler_test.py diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml index 8b1bab61d..337697304 100644 --- a/PySide/QtCore/typesystem_core.xml +++ b/PySide/QtCore/typesystem_core.xml @@ -718,6 +718,38 @@ #include <pyside.h> + + // Define a global variable to handle qInstallMsgHandler callback + static PyObject* qtmsghandler = 0; + + void + msghandlercallback(QtMsgType type, const char* msg) + { + Shiboken::AutoDecRef arglist(Shiboken::makeTuple(type, msg)); + Shiboken::AutoDecRef ret(PyObject_CallObject(qtmsghandler, arglist)); + } + + + + if (%PYARG_1 == Py_None) { + qInstallMsgHandler(0); + %PYARG_0 = qtmsghandler ? qtmsghandler : Py_None; + qtmsghandler = 0; + } else if (!PyCallable_Check(%PYARG_1)) { + PyErr_SetString(PyExc_TypeError, "parameter must be callable"); + } else { + %PYARG_0 = qtmsghandler ? qtmsghandler : Py_None; + Py_INCREF(%PYARG_1); + qtmsghandler = %PYARG_1; + qInstallMsgHandler(msghandlercallback); + } + + if (%PYARG_0 == Py_None) + Py_INCREF(%PYARG_0); + + + + diff --git a/tests/QtCore/CMakeLists.txt b/tests/QtCore/CMakeLists.txt index 35fa7b97d..b5f59d474 100644 --- a/tests/QtCore/CMakeLists.txt +++ b/tests/QtCore/CMakeLists.txt @@ -22,6 +22,7 @@ PYSIDE_TEST(qfileinfo_test.py) PYSIDE_TEST(qfile_test.py) PYSIDE_TEST(qflags_test.py) PYSIDE_TEST(qhandle_test.py) +PYSIDE_TEST(qinstallmsghandler_test.py) PYSIDE_TEST(qlinef_test.py) PYSIDE_TEST(qlocale_test.py) PYSIDE_TEST(qmetaobject_test.py) diff --git a/tests/QtCore/qinstallmsghandler_test.py b/tests/QtCore/qinstallmsghandler_test.py new file mode 100644 index 000000000..d76c6ceec --- /dev/null +++ b/tests/QtCore/qinstallmsghandler_test.py @@ -0,0 +1,53 @@ + +'''Test cases for qInstallMsgHandler''' + +import unittest +import sys + +from PySide.QtCore import * + +param = [] + +def handler(msgt, msg): + global param + param = [msgt, msg] + +def handleruseless(msgt, msg): + pass + +class QInstallMsgHandlerTest(unittest.TestCase): + + def tearDown(self): + # Ensure that next test will have a clear environment + qInstallMsgHandler(None) + + def testNone(self): + ret = qInstallMsgHandler(None) + self.assertEqual(ret, None) + + def testRet(self): + ret = qInstallMsgHandler(None) + self.assertEqual(ret, None) + refcount = sys.getrefcount(handleruseless) + retNone = qInstallMsgHandler(handleruseless) + self.assertEqual(sys.getrefcount(handleruseless), refcount + 1) + rethandler = qInstallMsgHandler(None) + self.assertEqual(rethandler, handleruseless) + del rethandler + self.assertEqual(sys.getrefcount(handleruseless), refcount) + + def testHandler(self): + rethandler = qInstallMsgHandler(handler) + qDebug("Test Debug") + self.assertEqual(param[0], QtDebugMsg) + self.assertEqual(param[1], "Test Debug") + qWarning("Test Warning") + self.assertEqual(param[0], QtWarningMsg) + self.assertEqual(param[1], "Test Warning") + qCritical("Test Critical") + self.assertEqual(param[0], QtCriticalMsg) + self.assertEqual(param[1], "Test Critical") + +if __name__ == '__main__': + unittest.main() + -- cgit v1.2.3