Commits

Yuya Nishihara committed 5d212c9

qtapp: move ExceptionCatcher near other exception handling functions

Comments (0)

Files changed (1)

tortoisehg/hgqt/qtapp.py

     dlg = bugreport.BugReport(opts)
     dlg.exec_()
 
-class GarbageCollector(QObject):
-    '''
-    Disable automatic garbage collection and instead collect manually
-    every INTERVAL milliseconds.
-
-    This is done to ensure that garbage collection only happens in the GUI
-    thread, as otherwise Qt can crash.
-    '''
-
-    INTERVAL = 5000
-
-    def __init__(self, ui, parent):
-        QObject.__init__(self, parent)
-        self._ui = ui
-
-        self.timer = QTimer(self)
-        self.timer.timeout.connect(self.check)
-
-        self.threshold = gc.get_threshold()
-        gc.disable()
-        self.timer.start(self.INTERVAL)
-        #gc.set_debug(gc.DEBUG_SAVEALL)
-
-    def check(self):
-        l0, l1, l2 = gc.get_count()
-        if l0 > self.threshold[0]:
-            num = gc.collect(0)
-            self._ui.debug('GarbageCollector.check: %d %d %d\n' % (l0, l1, l2))
-            self._ui.debug('collected gen 0, found %d unreachable\n' % num)
-            if l1 > self.threshold[1]:
-                num = gc.collect(1)
-                self._ui.debug('collected gen 1, found %d unreachable\n' % num)
-                if l2 > self.threshold[2]:
-                    num = gc.collect(2)
-                    self._ui.debug('collected gen 2, found %d unreachable\n'
-                                   % num)
-
-    def debug_cycles(self):
-        gc.collect()
-        for obj in gc.garbage:
-            self._ui.debug('%s, %r, %s\n' % (obj, obj, type(obj)))
-
-
-def allowSetForegroundWindow(processid=-1):
-    """Allow a given process to set the foreground window"""
-    # processid = -1 means ASFW_ANY (i.e. allow any process)
-    if os.name == 'nt':
-        # on windows we must explicitly allow bringing the main window to
-        # the foreground. To do so we must use ctypes
-        try:
-            from ctypes import windll
-            windll.user32.AllowSetForegroundWindow(processid)
-        except ImportError:
-            pass
-
-def connectToExistingWorkbench(root=None):
-    """
-    Connect and send data to an existing workbench server
-
-    For the connection to be successful, the server must loopback the data
-    that we send to it.
-
-    Normally the data that is sent will be a repository root path, but we can
-    also send "echo" to check that the connection works (i.e. that there is a
-    server)
-    """
-    if root:
-        data = root
-    else:
-        data = '[echo]'
-    socket = QLocalSocket()
-    socket.connectToServer(QApplication.applicationName() + '-' + util.getuser(),
-        QIODevice.ReadWrite)
-    if socket.waitForConnected(10000):
-        # Momentarily let any process set the foreground window
-        # The server process with revoke this permission as soon as it gets
-        # the request
-        allowSetForegroundWindow()
-        socket.write(QByteArray(data))
-        socket.flush()
-        socket.waitForReadyRead(10000)
-        reply = socket.readAll()
-        if data == reply:
-            return True
-    return False
-
-
 class ExceptionCatcher(QObject):
     """Catch unhandled exception raised inside Qt event loop"""
 
         for args in self.errors:
             traceback.print_exception(*args)
 
+
+class GarbageCollector(QObject):
+    '''
+    Disable automatic garbage collection and instead collect manually
+    every INTERVAL milliseconds.
+
+    This is done to ensure that garbage collection only happens in the GUI
+    thread, as otherwise Qt can crash.
+    '''
+
+    INTERVAL = 5000
+
+    def __init__(self, ui, parent):
+        QObject.__init__(self, parent)
+        self._ui = ui
+
+        self.timer = QTimer(self)
+        self.timer.timeout.connect(self.check)
+
+        self.threshold = gc.get_threshold()
+        gc.disable()
+        self.timer.start(self.INTERVAL)
+        #gc.set_debug(gc.DEBUG_SAVEALL)
+
+    def check(self):
+        l0, l1, l2 = gc.get_count()
+        if l0 > self.threshold[0]:
+            num = gc.collect(0)
+            self._ui.debug('GarbageCollector.check: %d %d %d\n' % (l0, l1, l2))
+            self._ui.debug('collected gen 0, found %d unreachable\n' % num)
+            if l1 > self.threshold[1]:
+                num = gc.collect(1)
+                self._ui.debug('collected gen 1, found %d unreachable\n' % num)
+                if l2 > self.threshold[2]:
+                    num = gc.collect(2)
+                    self._ui.debug('collected gen 2, found %d unreachable\n'
+                                   % num)
+
+    def debug_cycles(self):
+        gc.collect()
+        for obj in gc.garbage:
+            self._ui.debug('%s, %r, %s\n' % (obj, obj, type(obj)))
+
+
+def allowSetForegroundWindow(processid=-1):
+    """Allow a given process to set the foreground window"""
+    # processid = -1 means ASFW_ANY (i.e. allow any process)
+    if os.name == 'nt':
+        # on windows we must explicitly allow bringing the main window to
+        # the foreground. To do so we must use ctypes
+        try:
+            from ctypes import windll
+            windll.user32.AllowSetForegroundWindow(processid)
+        except ImportError:
+            pass
+
+def connectToExistingWorkbench(root=None):
+    """
+    Connect and send data to an existing workbench server
+
+    For the connection to be successful, the server must loopback the data
+    that we send to it.
+
+    Normally the data that is sent will be a repository root path, but we can
+    also send "echo" to check that the connection works (i.e. that there is a
+    server)
+    """
+    if root:
+        data = root
+    else:
+        data = '[echo]'
+    socket = QLocalSocket()
+    socket.connectToServer(QApplication.applicationName() + '-' + util.getuser(),
+        QIODevice.ReadWrite)
+    if socket.waitForConnected(10000):
+        # Momentarily let any process set the foreground window
+        # The server process with revoke this permission as soon as it gets
+        # the request
+        allowSetForegroundWindow()
+        socket.write(QByteArray(data))
+        socket.flush()
+        socket.waitForReadyRead(10000)
+        reply = socket.readAll()
+        if data == reply:
+            return True
+    return False
+
+
 class QtRunner(QObject):
     """Run Qt app and hold its windows
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.