Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Test atexit shutdown mechanism in a subprocess
  • Loading branch information
pitrou committed Dec 13, 2017
commit d304da069bd19e02522a5c458749801a18f28bad
16 changes: 16 additions & 0 deletions Lib/test/test_atexit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
import atexit
from test import support
from test.support import script_helper

### helpers
def h1():
Expand Down Expand Up @@ -152,6 +153,21 @@ def test_bound_methods(self):
atexit._run_exitfuncs()
self.assertEqual(l, [5])

def test_shutdown(self):
# Actually test the shutdown mechanism in a subprocess
code = """if 1:
import atexit

def f(msg):
print(msg)

atexit.register(f, "one")
atexit.register(f, "two")
"""
res = script_helper.assert_python_ok("-c", code)
self.assertEqual(res.out.decode().splitlines(), ["two", "one"])
self.assertFalse(res.err)


@support.cpython_only
class SubinterpreterTest(unittest.TestCase):
Expand Down
2 changes: 2 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,8 @@ _Py_FatalInitError(_PyInitError err)
/* For the atexit module. */
void _Py_PyAtExit(void (*func)(void))
{
/* Guard against API misuse */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add "bpo-17852: " prefix. Otherwise, the purpose of the assertion is not obvious.

Or explain that calling this function to register a new callback replaces the previous callback, which is unlikely to be the expected behaviour.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, adding a bpo references sounds good to me.

assert(_PyRuntime.pyexitfunc == NULL || _PyRuntime.pyexitfunc == func);
_PyRuntime.pyexitfunc = func;
}

Expand Down