Exemplo n.º 1
0
    def close(self):
        pool = NSAutoreleasePool.alloc().init()
        self.window.orderOut_(None)

        # quit app mainloop and hide window
        if NSApp().isRunning() == objc.YES:
            print time.ctime(), "Cocoa: Pausing app..."
            NSApp().stop_(None)

        # needs a pseudo-event before it'll be correctly stopped
        e = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(\
         NSApplicationDefined, NSMakePoint(0, 0), 0, 0.0, 0, None, 0, 0, 0)
        NSApp().postEvent_atStart_(e, True)
        Debugging.removeExceptionHandler()
        del pool
Exemplo n.º 2
0
	def close(self):
		pool = NSAutoreleasePool.alloc().init()
		self.window.orderOut_(None)

		# quit app mainloop and hide window
		if NSApp().isRunning() == objc.YES:
			print time.ctime(), "Cocoa: Pausing app..."
			NSApp().stop_(None)

		# needs a pseudo-event before it'll be correctly stopped
		e = NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(\
			NSApplicationDefined, NSMakePoint(0, 0), 0, 0.0, 0, None, 0, 0, 0)
		NSApp().postEvent_atStart_(e, True)
		Debugging.removeExceptionHandler()
		del pool
Exemplo n.º 3
0
    def testInstallExceptionHandler(self):
        self.assertFalse(Debugging.handlerInstalled())
        try:
            Debugging.installExceptionHandler(
                verbosity=Debugging.LOGSTACKTRACE,
                mask=Debugging.EVERYTHINGMASK)
            self.assertTrue(Debugging.handlerInstalled())

            try:
                cls = objc.lookUpClass("NSException")
                cls.exceptionWithName_reason_userInfo_("FooBar", "hello world",
                                                       None).raise__()
            except Exception as exc:
                self.assertFalse(Debugging.isPythonException(exc))

            else:
                self.fail("Exception not raised")

            try:
                cls = objc.lookUpClass("NSArray")

                def test(value, idx, stop):
                    raise ValueError("42")

                a = cls.alloc().initWithArray_([1, 2, 3, 4])
                a.indexOfObjectPassingTest_(test)

            except Exception as exc:
                self.assertTrue(Debugging.isPythonException(exc))

            else:
                self.fail("Exception not raised")

        finally:
            Debugging.removeExceptionHandler()
            self.assertFalse(Debugging.handlerInstalled())
Exemplo n.º 4
0
    def testHandlerBasic(self):
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installVerboseExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installPythonExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())
Exemplo n.º 5
0
    def testHandlerBasic(self):
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installVerboseExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())

        Debugging.installPythonExceptionHandler()
        self.assertTrue(Debugging.handlerInstalled())

        Debugging.removeExceptionHandler()
        self.assertFalse(Debugging.handlerInstalled())
Exemplo n.º 6
0
def runEventLoop(
    argv=None,
    unexpectedErrorAlert=None,
    installInterrupt=None,
    pdb=None,
    main=NSApplicationMain,
):
    """Run the event loop, ask the user if we should continue if an
    exception is caught. Use this function instead of NSApplicationMain().
    """
    if argv is None:
        argv = sys.argv

    if pdb is None:
        pdb = "USE_PDB" in os.environ

    if pdb:
        from PyObjCTools import Debugging

        Debugging.installVerboseExceptionHandler()
        # bring it to the front, starting from terminal
        # often won't
        activator = PyObjCAppHelperApplicationActivator.alloc().init()
        NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
            activator, "activateNow:", NSApplicationDidFinishLaunchingNotification, None
        )
    else:
        Debugging = None

    if installInterrupt is None and pdb:
        installInterrupt = True

    if unexpectedErrorAlert is None:
        if pdb:
            unexpectedErrorAlert = unexpectedErrorAlertPdb
        else:
            unexpectedErrorAlert = unexpectedErrorAlertPanel

    runLoop = NSRunLoop.currentRunLoop()
    stopper = PyObjCAppHelperRunLoopStopper.alloc().init()
    PyObjCAppHelperRunLoopStopper.addRunLoopStopper_toRunLoop_(stopper, runLoop)

    firstRun = NSApp() is None
    try:

        while stopper.shouldRun():
            try:
                if firstRun:
                    firstRun = False
                    if installInterrupt:
                        installMachInterrupt()
                    main(argv)
                else:
                    NSApp().run()
            except RAISETHESE:
                traceback.print_exc()
                break
            except:  # noqa: E722, B001
                exctype, e, tb = sys.exc_info()
                if isinstance(e, objc.error):
                    error_str = str(e)

                    NSLog("%@", error_str)
                elif not unexpectedErrorAlert():
                    NSLog("%@", "An exception has occured:")
                    traceback.print_exc()
                    sys.exit(0)
                else:
                    NSLog("%@", "An exception has occured:")
                    traceback.print_exc()
            else:
                break

    finally:
        if Debugging is not None:
            Debugging.removeExceptionHandler()
        PyObjCAppHelperRunLoopStopper.removeRunLoopStopperFromRunLoop_(runLoop)
Exemplo n.º 7
0
def runEventLoop(argv=None, unexpectedErrorAlert=None, installInterrupt=None, pdb=None, main=NSApplicationMain):
    """Run the event loop, ask the user if we should continue if an
    exception is caught. Use this function instead of NSApplicationMain().
    """
    if argv is None:
        argv = sys.argv

    if pdb is None:
        pdb = 'USE_PDB' in os.environ

    if pdb:
        from PyObjCTools import Debugging
        Debugging.installVerboseExceptionHandler()
        # bring it to the front, starting from terminal
        # often won't
        activator = PyObjCAppHelperApplicationActivator.alloc().init()
        NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(
            activator,
            'activateNow:',
            NSApplicationDidFinishLaunchingNotification,
            None,
        )
    else:
        Debugging = None

    if installInterrupt is None and pdb:
        installInterrupt = True

    if unexpectedErrorAlert is None:
        if pdb:
            unexpectedErrorAlert = unexpectedErrorAlertPdb
        else:
            unexpectedErrorAlert = unexpectedErrorAlertPanel

    runLoop = NSRunLoop.currentRunLoop()
    stopper = PyObjCAppHelperRunLoopStopper.alloc().init()
    PyObjCAppHelperRunLoopStopper.addRunLoopStopper_toRunLoop_(stopper, runLoop)

    firstRun = NSApp() is None
    try:

        while stopper.shouldRun():
            try:
                if firstRun:
                    firstRun = False
                    if installInterrupt:
                        installMachInterrupt()
                    main(argv)
                else:
                    NSApp().run()
            except RAISETHESE:
                traceback.print_exc()
                break
            except:
                exctype, e, tb = sys.exc_info()
                objc_exception = False
                if isinstance(e, objc.error):
                    NSLog("%@", unicode(str(e), 'utf-8', 'replace'))
                elif not unexpectedErrorAlert():
                    NSLog("%@", "An exception has occured:")
                    traceback.print_exc()
                    sys.exit(0)
                else:
                    NSLog("%@", "An exception has occured:")
                    traceback.print_exc()
            else:
                break

    finally:
        if Debugging is not None:
            Debugging.removeExceptionHandler()
        PyObjCAppHelperRunLoopStopper.removeRunLoopStopperFromRunLoop_(runLoop)