Skip to content

Commit 83d5204

Browse files
authored
bpo-42639: Cleanup atexitmodule.c (GH-23770)
* Rename "atexitmodule_state" to "struct atexit_state". * Rename "modstate" to "state". * Rename "self" parameter to "module". * test_atexit uses textwrap.dedent(). * Remove _Py_PyAtExit() function: inline it into atexit_exec(). * PyInterpreterState: rename pyexitfunc to atexit_func, rename pyexitmodule to atexit_module.
1 parent fdb9efc commit 83d5204

File tree

5 files changed

+125
-158
lines changed

5 files changed

+125
-158
lines changed

Include/cpython/pylifecycle.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ PyAPI_FUNC(int) Py_RunMain(void);
3535

3636
PyAPI_FUNC(void) _Py_NO_RETURN Py_ExitStatusException(PyStatus err);
3737

38-
/* Py_PyAtExit is for the atexit module, Py_AtExit is for low-level
39-
* exit functions.
40-
*/
41-
PyAPI_FUNC(void) _Py_PyAtExit(void (*func)(PyObject *), PyObject *);
42-
4338
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL. */
4439
PyAPI_FUNC(void) _Py_RestoreSignals(void);
4540

Include/internal/pycore_interp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ struct _is {
234234
PyObject *after_forkers_child;
235235
#endif
236236
/* AtExit module */
237-
void (*pyexitfunc)(PyObject *);
238-
PyObject *pyexitmodule;
237+
void (*atexit_func)(PyObject *);
238+
PyObject *atexit_module;
239239

240240
uint64_t tstate_next_unique_id;
241241

Lib/test/test_atexit.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import sys
2-
import unittest
3-
import io
41
import atexit
2+
import io
53
import os
4+
import sys
5+
import textwrap
6+
import unittest
67
from test import support
78
from test.support import script_helper
89

@@ -156,15 +157,15 @@ def test_bound_methods(self):
156157

157158
def test_shutdown(self):
158159
# Actually test the shutdown mechanism in a subprocess
159-
code = """if 1:
160+
code = textwrap.dedent("""
160161
import atexit
161162
162163
def f(msg):
163164
print(msg)
164165
165166
atexit.register(f, "one")
166167
atexit.register(f, "two")
167-
"""
168+
""")
168169
res = script_helper.assert_python_ok("-c", code)
169170
self.assertEqual(res.out.decode().splitlines(), ["two", "one"])
170171
self.assertFalse(res.err)
@@ -178,13 +179,13 @@ def test_callbacks_leak(self):
178179
# take care to free callbacks in its per-subinterpreter module
179180
# state.
180181
n = atexit._ncallbacks()
181-
code = r"""if 1:
182+
code = textwrap.dedent(r"""
182183
import atexit
183184
def f():
184185
pass
185186
atexit.register(f)
186187
del atexit
187-
"""
188+
""")
188189
ret = support.run_in_subinterp(code)
189190
self.assertEqual(ret, 0)
190191
self.assertEqual(atexit._ncallbacks(), n)
@@ -193,13 +194,13 @@ def test_callbacks_leak_refcycle(self):
193194
# Similar to the above, but with a refcycle through the atexit
194195
# module.
195196
n = atexit._ncallbacks()
196-
code = r"""if 1:
197+
code = textwrap.dedent(r"""
197198
import atexit
198199
def f():
199200
pass
200201
atexit.register(f)
201202
atexit.__atexit = atexit
202-
"""
203+
""")
203204
ret = support.run_in_subinterp(code)
204205
self.assertEqual(ret, 0)
205206
self.assertEqual(atexit._ncallbacks(), n)
@@ -210,13 +211,13 @@ def test_callback_on_subinterpreter_teardown(self):
210211
expected = b"The test has passed!"
211212
r, w = os.pipe()
212213

213-
code = r"""if 1:
214+
code = textwrap.dedent(r"""
214215
import os
215216
import atexit
216217
def callback():
217218
os.write({:d}, b"The test has passed!")
218219
atexit.register(callback)
219-
""".format(w)
220+
""".format(w))
220221
ret = support.run_in_subinterp(code)
221222
os.close(w)
222223
self.assertEqual(os.read(r, len(expected)), expected)

0 commit comments

Comments
 (0)