| 1 | import atexit, sys |
| 2 | |
| 3 | |
| 4 | def test_args(): |
| 5 | import io |
| 6 | stdout, stderr = sys.stdout, sys.stderr |
| 7 | nhooks = atexit._ncallbacks() |
| 8 | try: |
| 9 | sys.stdout = sys.stderr = capture = io.StringIO() |
| 10 | def h1(): |
| 11 | print("h1") |
| 12 | def h2(): |
| 13 | print("h2") |
| 14 | atexit.register(h1) |
| 15 | atexit.register(h2) |
| 16 | assert atexit._ncallbacks() == nhooks + 2 |
| 17 | atexit._run_exitfuncs() |
| 18 | assert atexit._ncallbacks() == 0 |
| 19 | assert capture.getvalue() == 'h2\nh1\n' |
| 20 | finally: |
| 21 | sys.stdout = stdout |
| 22 | sys.stderr = stderr |
| 23 | |
| 24 | |
| 25 | def test_atexit_uses_unraisablehook(): |
| 26 | nhooks = atexit._ncallbacks() |
| 27 | |
| 28 | l = [] |
| 29 | def ownhook(hookargs): |
| 30 | l.append(hookargs) |
| 31 | |
| 32 | sys.unraisablehook = ownhook |
| 33 | try: |
| 34 | def r(): |
| 35 | 1/0 |
| 36 | atexit.register(r) |
| 37 | atexit._run_exitfuncs() |
| 38 | assert atexit._ncallbacks() == nhooks |
| 39 | ua = l[0] |
| 40 | assert ua.exc_type is ZeroDivisionError |
| 41 | assert ua.object is r |
| 42 | finally: |
| 43 | sys.unraisablehook = sys.__unraisablehook__ |
| 44 | |
| 45 | |
| 46 |