PySide Bug Reproduction: Final version of threading that shows the pyside bug
[gc-dialer] / hand_tests / threading.py
index e27d560..22084ed 100755 (executable)
@@ -3,15 +3,60 @@
 from __future__ import with_statement
 from __future__ import division
 
+import functools
 import time
 
-import sys
-sys.path.insert(0,"./src")
-from util import qt_compat
-from util import qore_utils
 
+FORCE_PYQT = False
+DECORATE = True
 
-class QThread44(qt_compat.QtCore.QThread):
+
+try:
+       if FORCE_PYQT:
+               raise ImportError()
+       import PySide.QtCore as _QtCore
+       QtCore = _QtCore
+       USES_PYSIDE = True
+except ImportError:
+       import sip
+       sip.setapi('QString', 2)
+       sip.setapi('QVariant', 2)
+       import PyQt4.QtCore as _QtCore
+       QtCore = _QtCore
+       USES_PYSIDE = False
+
+
+if USES_PYSIDE:
+       Signal = QtCore.Signal
+       Slot = QtCore.Slot
+       Property = QtCore.Property
+else:
+       Signal = QtCore.pyqtSignal
+       Slot = QtCore.pyqtSlot
+       Property = QtCore.pyqtProperty
+
+
+def log_exception():
+
+       def log_exception_decorator(func):
+
+               @functools.wraps(func)
+               def wrapper(*args, **kwds):
+                       try:
+                               return func(*args, **kwds)
+                       except Exception:
+                               print "Exception", func.__name__
+                               raise
+
+               if DECORATE:
+                       return wrapper
+               else:
+                       return func
+
+       return log_exception_decorator
+
+
+class QThread44(QtCore.QThread):
        """
        This is to imitate QThread in Qt 4.4+ for when running on older version
        See http://labs.trolltech.com/blogs/2010/06/17/youre-doing-it-wrong
@@ -19,21 +64,22 @@ class QThread44(qt_compat.QtCore.QThread):
        """
 
        def __init__(self, parent = None):
-               qt_compat.QtCore.QThread.__init__(self, parent)
+               QtCore.QThread.__init__(self, parent)
 
        def run(self):
                self.exec_()
 
 
-class Producer(qt_compat.QtCore.QObject):
+class Producer(QtCore.QObject):
 
-       data = qt_compat.Signal(int)
-       done = qt_compat.Signal()
+       data = Signal(int)
+       done = Signal()
 
        def __init__(self):
-               qt_compat.QtCore.QObject.__init__(self)
+               QtCore.QObject.__init__(self)
 
-       @qt_compat.Slot()
+       @Slot()
+       @log_exception()
        def process(self):
                print "Starting producer"
                for i in xrange(10):
@@ -42,33 +88,36 @@ class Producer(qt_compat.QtCore.QObject):
                self.done.emit()
 
 
-class Consumer(qt_compat.QtCore.QObject):
+class Consumer(QtCore.QObject):
 
        def __init__(self):
-               qt_compat.QtCore.QObject.__init__(self)
+               QtCore.QObject.__init__(self)
 
-       @qt_compat.Slot()
+       @Slot()
+       @log_exception()
        def process(self):
                print "Starting consumer"
 
-       @qt_compat.Slot()
+       @Slot()
+       @log_exception()
        def print_done(self):
                print "Done"
 
-       @qt_compat.Slot(int)
+       @Slot(int)
+       @log_exception()
        def print_data(self, i):
                print i
 
 
 def run_producer_consumer():
-       app = qt_compat.QtCore.QCoreApplication([])
+       app = QtCore.QCoreApplication([])
 
-       producerThread = qore_utils.QThread44()
+       producerThread = QThread44()
        producer = Producer()
        producer.moveToThread(producerThread)
        producerThread.started.connect(producer.process)
 
-       consumerThread = qore_utils.QThread44()
+       consumerThread = QThread44()
        consumer = Consumer()
        consumer.moveToThread(consumerThread)
        consumerThread.started.connect(consumer.process)
@@ -76,7 +125,8 @@ def run_producer_consumer():
        producer.data.connect(consumer.print_data)
        producer.done.connect(consumer.print_done)
 
-       @qt_compat.Slot()
+       @Slot()
+       @log_exception()
        def producer_done():
                print "Shutting down"
                producerThread.quit()
@@ -86,7 +136,8 @@ def run_producer_consumer():
 
        count = [0]
 
-       @qt_compat.Slot()
+       @Slot()
+       @log_exception()
        def thread_done():
                print "Thread done"
                count[0] += 1
@@ -102,37 +153,5 @@ def run_producer_consumer():
        print "Status %s" % app.exec_()
 
 
-def run_task():
-       app = qt_compat.QtCore.QCoreApplication([])
-
-       bright = qore_utils.FutureThread()
-       def on_failure(*args):
-               print "Failure", args
-
-       def on_success(*args):
-               print "Success", args
-
-       def task(*args):
-               print "Task", args
-
-       timer = qt_compat.QtCore.QTimer()
-       timeouts = [0]
-       @qt_compat.Slot()
-       def on_timeout():
-               timeouts[0] += 1
-               print timeouts[0]
-               bright.add_task(task, (timeouts[0], ), {}, on_success, on_failure)
-               if timeouts[0] == 5:
-                       timer.stop()
-                       bright.stop()
-                       app.exit(0)
-       timer.timeout.connect(on_timeout)
-       timer.start(10)
-       bright.start()
-
-       print "Status %s" % app.exec_()
-
-
 if __name__ == "__main__":
-       #run_producer_consumer()
-       run_task()
+       run_producer_consumer()