Skip to content

Commit 8b27eaf

Browse files
authored
Update test_atexit.py to 3.14.3 (#7217)
1 parent f1511c4 commit 8b27eaf

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

Lib/test/_test_atexit.py

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def assert_raises_unraisable(self, exc_type, func, *args):
1919
atexit.register(func, *args)
2020
atexit._run_exitfuncs()
2121

22-
self.assertEqual(cm.unraisable.object, func)
22+
self.assertIsNone(cm.unraisable.object)
23+
self.assertEqual(cm.unraisable.err_msg,
24+
f'Exception ignored in atexit callback {func!r}')
2325
self.assertEqual(cm.unraisable.exc_type, exc_type)
2426
self.assertEqual(type(cm.unraisable.exc_value), exc_type)
2527

@@ -45,19 +47,22 @@ def func2(*args, **kwargs):
4547
('func2', (), {}),
4648
('func1', (1, 2), {})])
4749

50+
@unittest.expectedFailure # TODO: RUSTPYTHON
4851
def test_badargs(self):
4952
def func():
5053
pass
5154

5255
# func() has no parameter, but it's called with 2 parameters
5356
self.assert_raises_unraisable(TypeError, func, 1 ,2)
5457

58+
@unittest.expectedFailure # TODO: RUSTPYTHON
5559
def test_raise(self):
5660
def raise_type_error():
5761
raise TypeError
5862

5963
self.assert_raises_unraisable(TypeError, raise_type_error)
6064

65+
@unittest.expectedFailure # TODO: RUSTPYTHON
6166
def test_raise_unnormalized(self):
6267
# bpo-10756: Make sure that an unnormalized exception is handled
6368
# properly.
@@ -66,6 +71,7 @@ def div_zero():
6671

6772
self.assert_raises_unraisable(ZeroDivisionError, div_zero)
6873

74+
@unittest.expectedFailure # TODO: RUSTPYTHON
6975
def test_exit(self):
7076
self.assert_raises_unraisable(SystemExit, sys.exit)
7177

@@ -116,6 +122,7 @@ def test_bound_methods(self):
116122
atexit._run_exitfuncs()
117123
self.assertEqual(l, [5])
118124

125+
@unittest.expectedFailure # TODO: RUSTPYTHON
119126
def test_atexit_with_unregistered_function(self):
120127
# See bpo-46025 for more info
121128
def func():
@@ -125,12 +132,63 @@ def func():
125132
try:
126133
with support.catch_unraisable_exception() as cm:
127134
atexit._run_exitfuncs()
128-
self.assertEqual(cm.unraisable.object, func)
135+
self.assertIsNone(cm.unraisable.object)
136+
self.assertEqual(cm.unraisable.err_msg,
137+
f'Exception ignored in atexit callback {func!r}')
129138
self.assertEqual(cm.unraisable.exc_type, ZeroDivisionError)
130139
self.assertEqual(type(cm.unraisable.exc_value), ZeroDivisionError)
131140
finally:
132141
atexit.unregister(func)
133142

143+
@unittest.skip("TODO: RUSTPYTHON; Hangs")
144+
def test_eq_unregister_clear(self):
145+
# Issue #112127: callback's __eq__ may call unregister or _clear
146+
class Evil:
147+
def __eq__(self, other):
148+
action(other)
149+
return NotImplemented
150+
151+
for action in atexit.unregister, lambda o: atexit._clear():
152+
with self.subTest(action=action):
153+
atexit.register(lambda: None)
154+
atexit.unregister(Evil())
155+
atexit._clear()
156+
157+
@unittest.skip("TODO: RUSTPYTHON; Hangs")
158+
def test_eq_unregister(self):
159+
# Issue #112127: callback's __eq__ may call unregister
160+
def f1():
161+
log.append(1)
162+
def f2():
163+
log.append(2)
164+
def f3():
165+
log.append(3)
166+
167+
class Pred:
168+
def __eq__(self, other):
169+
nonlocal cnt
170+
cnt += 1
171+
if cnt == when:
172+
atexit.unregister(what)
173+
if other is f2:
174+
return True
175+
return False
176+
177+
for what, expected in (
178+
(f1, [3]),
179+
(f2, [3, 1]),
180+
(f3, [1]),
181+
):
182+
for when in range(1, 4):
183+
with self.subTest(what=what.__name__, when=when):
184+
cnt = 0
185+
log = []
186+
for f in (f1, f2, f3):
187+
atexit.register(f)
188+
atexit.unregister(Pred())
189+
atexit._run_exitfuncs()
190+
self.assertEqual(log, expected)
191+
134192

135193
if __name__ == "__main__":
136194
unittest.main()

Lib/test/test_atexit.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import atexit
22
import os
3+
import subprocess
34
import textwrap
45
import unittest
6+
from test.support import os_helper
57
from test import support
6-
from test.support import script_helper
7-
8+
from test.support import SuppressCrashReport, script_helper
9+
from test.support import threading_helper
810

911
class GeneralTest(unittest.TestCase):
1012
def test_general(self):
@@ -46,6 +48,40 @@ def test_atexit_instances(self):
4648
self.assertEqual(res.out.decode().splitlines(), ["atexit2", "atexit1"])
4749
self.assertFalse(res.err)
4850

51+
@unittest.skip("TODO: RUSTPYTHON; Flakey on CI")
52+
@threading_helper.requires_working_threading()
53+
@support.requires_resource("cpu")
54+
@unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful without the GIL")
55+
def test_atexit_thread_safety(self):
56+
# GH-126907: atexit was not thread safe on the free-threaded build
57+
source = """
58+
from threading import Thread
59+
60+
def dummy():
61+
pass
62+
63+
64+
def thready():
65+
for _ in range(100):
66+
atexit.register(dummy)
67+
atexit._clear()
68+
atexit.register(dummy)
69+
atexit.unregister(dummy)
70+
atexit._run_exitfuncs()
71+
72+
73+
threads = [Thread(target=thready) for _ in range(10)]
74+
for thread in threads:
75+
thread.start()
76+
77+
for thread in threads:
78+
thread.join()
79+
"""
80+
81+
# atexit._clear() has some evil side effects, and we don't
82+
# want them to affect the rest of the tests.
83+
script_helper.assert_python_ok("-c", textwrap.dedent(source))
84+
4985

5086
@support.cpython_only
5187
class SubinterpreterTest(unittest.TestCase):
@@ -100,6 +136,37 @@ def callback():
100136
self.assertEqual(os.read(r, len(expected)), expected)
101137
os.close(r)
102138

139+
# Python built with Py_TRACE_REFS fail with a fatal error in
140+
# _PyRefchain_Trace() on memory allocation error.
141+
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
142+
def test_atexit_with_low_memory(self):
143+
# gh-140080: Test that setting low memory after registering an atexit
144+
# callback doesn't cause an infinite loop during finalization.
145+
code = textwrap.dedent("""
146+
import atexit
147+
import _testcapi
148+
149+
def callback():
150+
print("hello")
151+
152+
atexit.register(callback)
153+
# Simulate low memory condition
154+
_testcapi.set_nomemory(0)
155+
""")
156+
157+
with os_helper.temp_dir() as temp_dir:
158+
script = script_helper.make_script(temp_dir, 'test_atexit_script', code)
159+
with SuppressCrashReport():
160+
with script_helper.spawn_python(script,
161+
stderr=subprocess.PIPE) as proc:
162+
proc.wait()
163+
stdout = proc.stdout.read()
164+
stderr = proc.stderr.read()
165+
166+
self.assertIn(proc.returncode, (0, 1))
167+
self.assertNotIn(b"hello", stdout)
168+
self.assertIn(b"MemoryError", stderr)
169+
103170

104171
if __name__ == "__main__":
105172
unittest.main()

0 commit comments

Comments
 (0)