Python atexit._exithandlers() Examples

The following are 18 code examples of atexit._exithandlers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module atexit , or try the search function .
Example #1
Source File: test_atexit.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_raise(self):
        # be sure raises are handled properly
        s = StringIO.StringIO()
        sys.stdout = sys.stderr = s
        save_handlers = atexit._exithandlers
        atexit._exithandlers = []
        try:
            atexit.register(self.raise1)
            atexit.register(self.raise2)
            self.assertRaises(TypeError, atexit._run_exitfuncs)
        finally:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            atexit._exithandlers = save_handlers

    ### helpers 
Example #2
Source File: test_atexit.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_sys_override(self):
        # be sure a preset sys.exitfunc is handled properly
        s = StringIO.StringIO()
        sys.stdout = sys.stderr = s
        save_handlers = atexit._exithandlers
        atexit._exithandlers = []
        exfunc = sys.exitfunc
        sys.exitfunc = self.h1
        reload(atexit)
        try:
            atexit.register(self.h2)
            atexit._run_exitfuncs()
        finally:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            atexit._exithandlers = save_handlers
            sys.exitfunc = exfunc
        self.assertEqual(s.getvalue(), "h2\nh1\n") 
Example #3
Source File: test_atexit.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_order(self):
        # be sure handlers are executed in reverse order
        s = StringIO.StringIO()
        sys.stdout = sys.stderr = s
        save_handlers = atexit._exithandlers
        atexit._exithandlers = []
        try:
            atexit.register(self.h1)
            atexit.register(self.h2)
            atexit.register(self.h3)
            atexit._run_exitfuncs()
        finally:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            atexit._exithandlers = save_handlers
        self.assertEqual(s.getvalue(), "h3\nh2\nh1\n") 
Example #4
Source File: test_atexit.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_args(self):
        # be sure args are handled properly
        s = StringIO.StringIO()
        sys.stdout = sys.stderr = s
        save_handlers = atexit._exithandlers
        atexit._exithandlers = []
        try:
            atexit.register(self.h1)
            atexit.register(self.h4)
            atexit.register(self.h4, 4, kw="abc")
            atexit._run_exitfuncs()
        finally:
            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__
            atexit._exithandlers = save_handlers
        self.assertEqual(s.getvalue(), "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") 
Example #5
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #6
Source File: test_atexit.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #7
Source File: test_atexit.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        s = StringIO.StringIO()
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        sys.stdout = sys.stderr = self.subst_io = s
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #8
Source File: test_atexit.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #9
Source File: test_atexit.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        s = StringIO.StringIO()
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        sys.stdout = sys.stderr = self.subst_io = s
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #10
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def setUp(self):
        s = StringIO.StringIO()
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        sys.stdout = sys.stderr = self.subst_io = s
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #11
Source File: sct_utils.py    From spinalcordtoolbox with MIT License 5 votes vote down vote up
def init_error_client():
    """ Send traceback to neuropoly servers

    :return:
    """
    if os.getenv('SENTRY_DSN'):
        logger.debug('Configuring sentry report')
        try:
            client = raven.Client(
             release=__version__,
             processors=(
              'raven.processors.RemoveStackLocalsProcessor',
              'raven.processors.SanitizePasswordsProcessor'),
            )
            server_log_handler(client)
            traceback_to_server(client)

            old_exitfunc = sys.exitfunc
            def exitfunc():
                sent_something = False
                try:
                    # implementation-specific
                    import atexit
                    for handler, args, kw in atexit._exithandlers:
                        if handler.__module__.startswith("raven."):
                            sent_something = True
                except:
                    pass
                old_exitfunc()
                if sent_something:
                    print("Note: you can opt out of Sentry reporting by editing the file ${SCT_DIR}/bin/sct_launcher and delete the line starting with \"export SENTRY_DSN\"")

            sys.exitfunc = exitfunc
        except raven.exceptions.InvalidDsn:
            # This could happen if sct staff change the dsn
            logger.debug('Sentry DSN not valid anymore, not reporting errors') 
Example #12
Source File: test_atexit.py    From oss-ftp with MIT License 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #13
Source File: test_atexit.py    From oss-ftp with MIT License 5 votes vote down vote up
def setUp(self):
        s = StringIO.StringIO()
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        sys.stdout = sys.stderr = self.subst_io = s
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #14
Source File: test_atexit.py    From BinderFilter with MIT License 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #15
Source File: test_atexit.py    From BinderFilter with MIT License 5 votes vote down vote up
def setUp(self):
        s = StringIO.StringIO()
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        sys.stdout = sys.stderr = self.subst_io = s
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #16
Source File: test_atexit.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        sys.stdout = self.save_stdout
        sys.stderr = self.save_stderr
        atexit._exithandlers = self.save_handlers 
Example #17
Source File: test_atexit.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.save_stdout = sys.stdout
        self.save_stderr = sys.stderr
        self.stream = StringIO.StringIO()
        sys.stdout = sys.stderr = self.subst_io = self.stream
        self.save_handlers = atexit._exithandlers
        atexit._exithandlers = [] 
Example #18
Source File: httpserver.py    From mishkal with GNU General Public License v3.0 4 votes vote down vote up
def shutdown(self, force_quit_timeout=0):
        """
        Shutdown the queue (after finishing any pending requests).
        """
        self.logger.info('Shutting down threadpool')
        # Add a shutdown request for every worker
        for i in range(len(self.workers)):
            self.queue.put(ThreadPool.SHUTDOWN)
        # Wait for each thread to terminate
        hung_workers = []
        for worker in self.workers:
            worker.join(0.5)
            if worker.isAlive():
                hung_workers.append(worker)
        zombies = []
        for thread_id in self.dying_threads:
            if self.thread_exists(thread_id):
                zombies.append(thread_id)
        if hung_workers or zombies:
            self.logger.info("%s workers didn't stop properly, and %s zombies",
                             len(hung_workers), len(zombies))
            if hung_workers:
                for worker in hung_workers:
                    self.kill_worker(worker.thread_id)
                self.logger.info('Workers killed forcefully')
            if force_quit_timeout:
                hung = []
                timed_out = False
                need_force_quit = bool(zombies)
                for workers in self.workers:
                    if not timed_out and worker.isAlive():
                        timed_out = True
                        worker.join(force_quit_timeout)
                    if worker.isAlive():
                        print "Worker %s won't die" % worker
                        need_force_quit = True
                if need_force_quit:
                    import atexit
                    # Remove the threading atexit callback
                    for callback in list(atexit._exithandlers):
                        func = getattr(callback[0], 'im_func', None)
                        if not func:
                            continue
                        globs = getattr(func, 'func_globals', {})
                        mod = globs.get('__name__')
                        if mod == 'threading':
                            atexit._exithandlers.remove(callback)
                    atexit._run_exitfuncs()
                    print 'Forcefully exiting process'
                    os._exit(3)
                else:
                    self.logger.info('All workers eventually killed')
        else:
            self.logger.info('All workers stopped')