| 1 |
import atexit |
| 2 |
import os |
| 3 |
import subprocess |
| 4 |
import textwrap |
| 5 |
import unittest |
| 6 |
from test.support import os_helper |
| 7 |
from test import support |
| 8 |
from test.support import SuppressCrashReport, script_helper |
| 9 |
from test.support import threading_helper |
| 10 |
|
| 11 |
class GeneralTest(unittest.TestCase): |
| 12 |
def test_general(self): |
| 13 |
# Run _test_atexit.py in a subprocess since it calls atexit._clear() |
| 14 |
script = support.findfile("_test_atexit.py") |
| 15 |
script_helper.run_test_script(script) |
| 16 |
|
| 17 |
class FunctionalTest(unittest.TestCase): |
| 18 |
def test_shutdown(self): |
| 19 |
# Actually test the shutdown mechanism in a subprocess |
| 20 |
code = textwrap.dedent(""" |
| 21 |
import atexit |
| 22 |
|
| 23 |
def f(msg): |
| 24 |
print(msg) |
| 25 |
|
| 26 |
atexit.register(f, "one") |
| 27 |
atexit.register(f, "two") |
| 28 |
""") |
| 29 |
res = script_helper.assert_python_ok("-c", code) |
| 30 |
self.assertEqual(res.out.decode().splitlines(), ["two", "one"]) |
| 31 |
self.assertFalse(res.err) |
| 32 |
|
| 33 |
def test_atexit_instances(self): |
| 34 |
# bpo-42639: It is safe to have more than one atexit instance. |
| 35 |
code = textwrap.dedent(""" |
| 36 |
import sys |
| 37 |
import atexit as atexit1 |
| 38 |
del sys.modules['atexit'] |
| 39 |
import atexit as atexit2 |
| 40 |
del sys.modules['atexit'] |
| 41 |
|
| 42 |
assert atexit2 is not atexit1 |
| 43 |
|
| 44 |
atexit1.register(print, "atexit1") |
| 45 |
atexit2.register(print, "atexit2") |
| 46 |
""") |
| 47 |
res = script_helper.assert_python_ok("-c", code) |
| 48 |
self.assertEqual(res.out.decode().splitlines(), ["atexit2", "atexit1"]) |
| 49 |
self.assertFalse(res.err) |
| 50 |
|
| 51 |
@threading_helper.requires_working_threading() |
| 52 |
@support.requires_resource("cpu") |
| 53 |
@unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful without the GIL") |
| 54 |
def test_atexit_thread_safety(self): |
| 55 |
# GH-126907: atexit was not thread safe on the free-threaded build |
| 56 |
source = """ |
| 57 |
from threading import Thread |
| 58 |
|
| 59 |
def dummy(): |
| 60 |
pass |
| 61 |
|
| 62 |
|
| 63 |
def thready(): |
| 64 |
for _ in range(100): |
| 65 |
atexit.register(dummy) |
| 66 |
atexit._clear() |
| 67 |
atexit.register(dummy) |
| 68 |
atexit.unregister(dummy) |
| 69 |
atexit._run_exitfuncs() |
| 70 |
|
| 71 |
|
| 72 |
threads = [Thread(target=thready) for _ in range(10)] |
| 73 |
for thread in threads: |
| 74 |
thread.start() |
| 75 |
|
| 76 |
for thread in threads: |
| 77 |
thread.join() |
| 78 |
""" |
| 79 |
|
| 80 |
# atexit._clear() has some evil side effects, and we don't |
| 81 |
# want them to affect the rest of the tests. |
| 82 |
script_helper.assert_python_ok("-c", textwrap.dedent(source)) |
| 83 |
|
| 84 |
|
| 85 |
@support.cpython_only |
| 86 |
class SubinterpreterTest(unittest.TestCase): |
| 87 |
|
| 88 |
def test_callbacks_leak(self): |
| 89 |
# This test shows a leak in refleak mode if atexit doesn't |
| 90 |
# take care to free callbacks in its per-subinterpreter module |
| 91 |
# state. |
| 92 |
n = atexit._ncallbacks() |
| 93 |
code = textwrap.dedent(r""" |
| 94 |
import atexit |
| 95 |
def f(): |
| 96 |
pass |
| 97 |
atexit.register(f) |
| 98 |
del atexit |
| 99 |
""") |
| 100 |
ret = support.run_in_subinterp(code) |
| 101 |
self.assertEqual(ret, 0) |
| 102 |
self.assertEqual(atexit._ncallbacks(), n) |
| 103 |
|
| 104 |
def test_callbacks_leak_refcycle(self): |
| 105 |
# Similar to the above, but with a refcycle through the atexit |
| 106 |
# module. |
| 107 |
n = atexit._ncallbacks() |
| 108 |
code = textwrap.dedent(r""" |
| 109 |
import atexit |
| 110 |
def f(): |
| 111 |
pass |
| 112 |
atexit.register(f) |
| 113 |
atexit.__atexit = atexit |
| 114 |
""") |
| 115 |
ret = support.run_in_subinterp(code) |
| 116 |
self.assertEqual(ret, 0) |
| 117 |
self.assertEqual(atexit._ncallbacks(), n) |
| 118 |
|
| 119 |
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()") |
| 120 |
def test_callback_on_subinterpreter_teardown(self): |
| 121 |
# This tests if a callback is called on |
| 122 |
# subinterpreter teardown. |
| 123 |
expected = b"The test has passed!" |
| 124 |
r, w = os.pipe() |
| 125 |
|
| 126 |
code = textwrap.dedent(r""" |
| 127 |
import os |
| 128 |
import atexit |
| 129 |
def callback(): |
| 130 |
os.write({:d}, b"The test has passed!") |
| 131 |
atexit.register(callback) |
| 132 |
""".format(w)) |
| 133 |
ret = support.run_in_subinterp(code) |
| 134 |
os.close(w) |
| 135 |
self.assertEqual(os.read(r, len(expected)), expected) |
| 136 |
os.close(r) |
| 137 |
|
| 138 |
# Python built with Py_TRACE_REFS fail with a fatal error in |
| 139 |
# _PyRefchain_Trace() on memory allocation error. |
| 140 |
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') |
| 141 |
def test_atexit_with_low_memory(self): |
| 142 |
# gh-140080: Test that setting low memory after registering an atexit |
| 143 |
# callback doesn't cause an infinite loop during finalization. |
| 144 |
code = textwrap.dedent(""" |
| 145 |
import atexit |
| 146 |
import _testcapi |
| 147 |
|
| 148 |
def callback(): |
| 149 |
print("hello") |
| 150 |
|
| 151 |
atexit.register(callback) |
| 152 |
# Simulate low memory condition |
| 153 |
_testcapi.set_nomemory(0) |
| 154 |
""") |
| 155 |
|
| 156 |
with os_helper.temp_dir() as temp_dir: |
| 157 |
script = script_helper.make_script(temp_dir, 'test_atexit_script', code) |
| 158 |
with SuppressCrashReport(): |
| 159 |
with script_helper.spawn_python(script, |
| 160 |
stderr=subprocess.PIPE) as proc: |
| 161 |
proc.wait() |
| 162 |
stdout = proc.stdout.read() |
| 163 |
stderr = proc.stderr.read() |
| 164 |
|
| 165 |
self.assertIn(proc.returncode, (0, 1)) |
| 166 |
self.assertNotIn(b"hello", stdout) |
| 167 |
self.assertIn(b"MemoryError", stderr) |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == "__main__": |
| 171 |
unittest.main() |