Python atexit._run_exitfuncs() Examples

The following are 30 code examples of atexit._run_exitfuncs(). 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 android_universal with MIT License 6 votes vote down vote up
def test_print_tracebacks(self):
        # Issue #18776: the tracebacks should be printed when errors occur.
        def f():
            1/0  # one
        def g():
            1/0  # two
        def h():
            1/0  # three
        atexit.register(f)
        atexit.register(g)
        atexit.register(h)

        self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
        stderr = self.stream.getvalue()
        self.assertEqual(stderr.count("ZeroDivisionError"), 3)
        self.assertIn("# one", stderr)
        self.assertIn("# two", stderr)
        self.assertIn("# three", stderr) 
Example #2
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 #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_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 #5
Source File: subproc.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def handle(self, signum=0, sframe=None):
        self.received += 1

        # code snippet adapted from atexit._run_exitfuncs
        exc_info = None
        for i in xrange(self.received).__reversed__():
            for handler in self.handlers.get(i, []).__reversed__():
                try:
                    handler(signum, sframe)
                except SystemExit:
                    exc_info = sys.exc_info()
                except:
                    import traceback
                    print >> sys.stderr, "Error in SignalHandler.handle:"
                    traceback.print_exc()
                    exc_info = sys.exc_info()

        if exc_info is not None:
            raise exc_info[0], exc_info[1], exc_info[2] 
Example #6
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 #7
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
def test_print_tracebacks(self):
        # Issue #18776: the tracebacks should be printed when errors occur.
        def f():
            1/0  # one
        def g():
            1/0  # two
        def h():
            1/0  # three
        atexit.register(f)
        atexit.register(g)
        atexit.register(h)

        self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
        stderr = self.stream.getvalue()
        self.assertEqual(stderr.count("ZeroDivisionError"), 3)
        self.assertIn("# one", stderr)
        self.assertIn("# two", stderr)
        self.assertIn("# three", stderr) 
Example #8
Source File: test_atexit.py    From android_universal with MIT License 5 votes vote down vote up
def test_raise(self):
        # be sure raises are handled properly
        atexit.register(raise1)
        atexit.register(raise2)

        self.assertRaises(TypeError, atexit._run_exitfuncs) 
Example #9
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_bound_methods(self):
        l = []
        atexit.register(l.append, 5)
        atexit._run_exitfuncs()
        self.assertEqual(l, [5])

        atexit.unregister(l.append)
        atexit._run_exitfuncs()
        self.assertEqual(l, [5]) 
Example #10
Source File: test_atexit.py    From android_universal with MIT License 5 votes vote down vote up
def test_unregister(self):
        a = [0]
        def inc():
            a[0] += 1
        def dec():
            a[0] -= 1

        for i in range(4):
            atexit.register(inc)
        atexit.register(dec)
        atexit.unregister(inc)
        atexit._run_exitfuncs()

        self.assertEqual(a[0], -1) 
Example #11
Source File: __init__.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def exit():
    """
    Causes python to exit without garbage-collecting any objects, and thus avoids
    calling object destructor methods. This is a sledgehammer workaround for 
    a variety of bugs in PyQt and Pyside that cause crashes on exit.
    
    This function does the following in an attempt to 'safely' terminate
    the process:
    
    * Invoke atexit callbacks
    * Close all open file handles
    * os._exit()
    
    Note: there is some potential for causing damage with this function if you
    are using objects that _require_ their destructors to be called (for example,
    to properly terminate log files, disconnect from devices, etc). Situations
    like this are probably quite rare, but use at your own risk.
    """
    
    ## first disable our own cleanup function; won't be needing it.
    setConfigOptions(exitCleanup=False)
    
    ## invoke atexit callbacks
    atexit._run_exitfuncs()
    
    ## close file handles
    if sys.platform == 'darwin':
        for fd in range(3, 4096):
            if fd not in [7]:  # trying to close 7 produces an illegal instruction on the Mac.
                os.close(fd)
    else:
        os.closerange(3, 4096) ## just guessing on the maximum descriptor count..

    os._exit(0)
    


## Convenience functions for command-line use 
Example #12
Source File: test_atexit.py    From android_universal with MIT License 5 votes vote down vote up
def test_raise_unnormalized(self):
        # Issue #10756: Make sure that an unnormalized exception is
        # handled properly
        atexit.register(lambda: 1 / 0)

        self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
        self.assertIn("ZeroDivisionError", self.stream.getvalue()) 
Example #13
Source File: test_atexit.py    From android_universal with MIT License 5 votes vote down vote up
def test_stress(self):
        a = [0]
        def inc():
            a[0] += 1

        for i in range(128):
            atexit.register(inc)
        atexit._run_exitfuncs()

        self.assertEqual(a[0], 128) 
Example #14
Source File: test_atexit.py    From android_universal with MIT License 5 votes vote down vote up
def test_clear(self):
        a = [0]
        def inc():
            a[0] += 1

        atexit.register(inc)
        atexit._clear()
        atexit._run_exitfuncs()

        self.assertEqual(a[0], 0) 
Example #15
Source File: test_atexit.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_args(self):
        atexit.register(self.h1)
        atexit.register(self.h4)
        atexit.register(self.h4, 4, kw="abc")
        atexit._run_exitfuncs()
        self.assertEqual(self.subst_io.getvalue(),
                         "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") 
Example #16
Source File: test_atexit.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_order(self):
        atexit.register(self.h1)
        atexit.register(self.h2)
        atexit.register(self.h3)
        atexit._run_exitfuncs()
        self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n") 
Example #17
Source File: test_atexit.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_badargs(self):
        atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
        self.assertRaises(TypeError, atexit._run_exitfuncs) 
Example #18
Source File: test_atexit.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_badargs(self):
        atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
        self.assertRaises(TypeError, atexit._run_exitfuncs) 
Example #19
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_unregister(self):
        a = [0]
        def inc():
            a[0] += 1
        def dec():
            a[0] -= 1

        for i in range(4):
            atexit.register(inc)
        atexit.register(dec)
        atexit.unregister(inc)
        atexit._run_exitfuncs()

        self.assertEqual(a[0], -1) 
Example #20
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_clear(self):
        a = [0]
        def inc():
            a[0] += 1

        atexit.register(inc)
        atexit._clear()
        atexit._run_exitfuncs()

        self.assertEqual(a[0], 0) 
Example #21
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_stress(self):
        a = [0]
        def inc():
            a[0] += 1

        for i in range(128):
            atexit.register(inc)
        atexit._run_exitfuncs()

        self.assertEqual(a[0], 128) 
Example #22
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_raise_unnormalized(self):
        # Issue #10756: Make sure that an unnormalized exception is
        # handled properly
        atexit.register(lambda: 1 / 0)

        self.assertRaises(ZeroDivisionError, atexit._run_exitfuncs)
        self.assertIn("ZeroDivisionError", self.stream.getvalue()) 
Example #23
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_raise(self):
        # be sure raises are handled properly
        atexit.register(raise1)
        atexit.register(raise2)

        self.assertRaises(TypeError, atexit._run_exitfuncs) 
Example #24
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_order(self):
        # be sure handlers are executed in reverse order
        atexit.register(h1)
        atexit.register(h2)
        atexit.register(h3)
        atexit._run_exitfuncs()

        self.assertEqual(self.stream.getvalue(), "h3\nh2\nh1\n") 
Example #25
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_badargs(self):
        atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
        self.assertRaises(TypeError, atexit._run_exitfuncs) 
Example #26
Source File: test_atexit.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_args(self):
        # be sure args are handled properly
        atexit.register(h1)
        atexit.register(h4)
        atexit.register(h4, 4, kw="abc")
        atexit._run_exitfuncs()

        self.assertEqual(self.stream.getvalue(),
                            "h4 (4,) {'kw': 'abc'}\nh4 () {}\nh1\n") 
Example #27
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_raise(self):
        atexit.register(self.raise1)
        atexit.register(self.raise2)
        self.assertRaises(TypeError, atexit._run_exitfuncs)

    ### helpers 
Example #28
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_sys_override(self):
        # be sure a preset sys.exitfunc is handled properly
        exfunc = sys.exitfunc
        sys.exitfunc = self.h1
        reload(atexit)
        try:
            atexit.register(self.h2)
            atexit._run_exitfuncs()
        finally:
            sys.exitfunc = exfunc
        self.assertEqual(self.subst_io.getvalue(), "h2\nh1\n") 
Example #29
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_order(self):
        atexit.register(self.h1)
        atexit.register(self.h2)
        atexit.register(self.h3)
        atexit._run_exitfuncs()
        self.assertEqual(self.subst_io.getvalue(), "h3\nh2\nh1\n") 
Example #30
Source File: test_atexit.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_badargs(self):
        atexit.register(lambda: 1, 0, 0, (x for x in (1,2)), 0, 0)
        self.assertRaises(TypeError, atexit._run_exitfuncs)